middlewares

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultJWT = &JWT{}

DefaultJWT is global JWT instance.

Functions

func Authenticate

func Authenticate(queryKeys ...string) gohttputil.Middleware

Authenticate creates a middleware to verify and parse jwt. By default it will checj Bearer token from Authorization header. But user may specify URL search query keys in queryKeys parameter from which jwt can be collected, verified and parsed. If authentication fails an unauthorized response will be sent to the client. If authentication succeeds request's context key will contain JWT payload if payload type is specified in setup stage.

func Authorize

func Authorize(f AuthorizeFunc) gohttputil.Middleware

Authorize checks if the request is permitted to reach the handler. If AuthorizeFunc f returns true the request reaches the handler else it responds unauthorized to the client.

func FormPayload

func FormPayload(r *http.Request) any

FormPayload returns the validated form payload stored in request's context.

func JSONPayload

func JSONPayload(r *http.Request) any

JSONPayload returns the validated JSON payload stored in request's context.

func JWTPayload

func JWTPayload(r *http.Request) any

JWTPayload returns the context value containing the JWT payload found in authentication stage.

func Logger

func Logger(next http.Handler) http.Handler

Logger logs request and response statistics via slog.

func PathValuePayload added in v1.3.0

func PathValuePayload(r *http.Request) any

PathValuePayload returns the validated path parameters stored in request's context.

func QueryPayload

func QueryPayload(r *http.Request) any

QueryPayload returns the validated form payload stored in request's context.

func Recover

func Recover(next http.Handler) http.Handler

func SetupJWT

func SetupJWT(setupFuncs ...JWTSetupFunc)

SetupJWT sets up DefaultJWT. Application should setup this once in it's lifetime.

func ValidateForm

func ValidateForm(dto any) gohttputil.Middleware

ValidateForm validates request body and stores validated payload in the request's context.

func ValidateJSON

func ValidateJSON(dto any) gohttputil.Middleware

ValidateJSON validates JSON body and stores validated payload in the request's context.

func ValidatePathValue added in v1.3.0

func ValidatePathValue(dto any) gohttputil.Middleware

ValidatePathValue validates request path parameters and stores validated payload in the request's context.

func ValidateQuery

func ValidateQuery(dto any) gohttputil.Middleware

ValidateQuery validates request search query and stores validated payload in the request's context.

Types

type AuthorizeFunc

type AuthorizeFunc func(*http.Request) bool

AuthorizeFunc is the signature for request authorizing function.

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

JWT provides interface for setting up JWT token parsing and payload retrieval.

func (*JWT) Sign

func (j *JWT) Sign(method jwt.SigningMethod, payload jwt.Claims) (string, error)

Sign creates a JWT using the secret key set in setup stage.

func (*JWT) Verify

func (j *JWT) Verify(tokenStr string) (*jwt.Token, error)

type JWTSetupFunc

type JWTSetupFunc func(*JWT) *JWT

JWTSetupFunc is the signature for setting up JWT via builder function.

func JWTWithPayloadType

func JWTWithPayloadType(t any) JWTSetupFunc

JWTWithPayloadType sets the payload data type for JWT paylod. Upon successfull verification and parsing, request context will have a pointer of type t filled with data found from JWT payload.

func JWTWithSecret

func JWTWithSecret(secret string) JWTSetupFunc

JWTWithSecret sets JWT Secret key

Jump to

Keyboard shortcuts

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