Documentation
¶
Index ¶
- func GetClientIP(ctx context.Context) string
- func GetStash(ctx context.Context) *sync.Map
- func NewBasicAuthMiddleware(users map[string]string, realm string) func(next http.Handler) http.Handler
- func NewClientIPMiddleware(provider Provider) func(next http.Handler) http.Handler
- func NewProtectedMiddleware(verifyToken func(token string) error) func(next http.Handler) http.Handler
- func NewStashMiddleware() func(next http.Handler) http.Handler
- type ContextKey
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetClientIP ¶ added in v0.5.3
GetClientIP retrieves the client IP address from the provided context. It takes a single parameter ctx of type context.Context. It returns a string representing the client IP address if found, otherwise an empty string.
func GetStash ¶ added in v0.5.3
GetStash retrieves the stash from the provided context. It takes a single parameter ctx of type context.Context. It returns a pointer to a sync.Map representing the stash if found, otherwise nil.
func NewBasicAuthMiddleware ¶ added in v0.4.0
func NewBasicAuthMiddleware(users map[string]string, realm string) func(next http.Handler) http.Handler
NewBasicAuthMiddleware returns a middleware function that performs basic authentication. It takes a map of users and passwords, and a realm string as input. The returned middleware function checks if the request contains valid basic authentication credentials. If the credentials are valid, it calls the next handler in the chain. If the credentials are invalid or missing, it sends an HTTP 401 Unauthorized response.
func NewClientIPMiddleware ¶
NewClientIPMiddleware returns a middleware function that extracts the client's IP address from the request and adds it to the request's context. The IP address is obtained using the provided IP address provider. The middleware function takes the next http.Handler as input and returns a new http.Handler that wraps the provided handler. When the new handler is called, it first extracts the client's IP address from the request using the provider, adds it to the request's context, and then calls the next handler in the chain. The IP address can be accessed from the request's context using the ClientIP key.
func NewProtectedMiddleware ¶ added in v0.5.0
func NewProtectedMiddleware(verifyToken func(token string) error) func(next http.Handler) http.Handler
NewProtectedMiddleware returns a middleware function that performs verification of the request. It takes a `verifyToken` function as an argument. This function is used to verify the token The token string should be set as as `Authorization` header in the http request If the token is valid, it calls the next handler in the chain. If the token is invalid or missing, it sends an HTTP 401 Unauthorized response.
func NewStashMiddleware ¶
NewStashMiddleware returns a middleware function that adds a stash to the request context. The stash is a synchronized map that can be used to store and retrieve values during the connection lifecycle. The middleware function takes the next http.Handler as input and returns a new http.Handler that wraps the next handler. The returned handler adds the stash to the request context and then calls the next handler to process the request.