interceptors

package
v0.0.0-...-cb92cc7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Authorization interceptor.PreHandler = func(_ context.Context, request *http.Request, header *http.Header) *toktik.RequestInterruptError {
	if !needAuthorization(request.URL) {
		return nil
	}
	params := request.URL.Query()

	if !params.Has("token") {
		return &toktik.RequestInterruptError{
			Cause:  errors.New("authorize failed: token not found"),
			Status: http.StatusUnauthorized,
		}
	}

	success, _, err := middleware.ValidateToken(params.Get("token"))
	if !success && err == nil {
		err = errors.New("authorize failed: invalid token")
	}
	if err != nil {
		return &toktik.RequestInterruptError{
			Cause:  err,
			Status: http.StatusUnauthorized,
		}
	}

	return nil
}
View Source
var XForwardInterceptor interceptor.PreHandler = func(ctx context.Context, request *http.Request, _ *http.Header) (_ *errors.RequestInterruptError) {
	request.Header.Set("X-Forwarded-Proto", request.URL.Scheme)
	request.Header.Set("X-Forwarded-Host", request.URL.Host)
	clientIP, _, err := net.SplitHostPort(request.RemoteAddr)
	if err != nil {
		return
	}
	prior, ok := request.Header["X-Forwarded-For"]
	if !(ok && prior == nil) {
		return
	}
	if len(prior) > 0 {
		clientIP = strings.Join(prior, ", ") + ", " + clientIP
	}
	request.Header.Set("X-Forwarded-For", clientIP)
	return
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL