Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for authentication. It extracts and validates tokens from HTTP requests and adds user information to the request context.
Index ¶
- func GetUserID(ctx context.Context) (string, bool)
- func GetUserResources(ctx context.Context) ([]string, bool)
- func GetUserRoles(ctx context.Context) ([]string, bool)
- func GetUserScopes(ctx context.Context) ([]string, bool)
- func HasResource(ctx context.Context, resource string) bool
- func HasRole(ctx context.Context, role string) bool
- func HasScope(ctx context.Context, scope string) bool
- func IsAuthenticated(ctx context.Context) bool
- func IsAuthorized(ctx context.Context, allowedRoles []string) bool
- func IsAuthorizedWithScopes(ctx context.Context, allowedRoles []string, requiredScopes []string, ...) bool
- func WithUserID(ctx context.Context, userID string) context.Context
- func WithUserResources(ctx context.Context, resources []string) context.Context
- func WithUserRoles(ctx context.Context, roles []string) context.Context
- func WithUserScopes(ctx context.Context, scopes []string) context.Context
- type Config
- type Middleware
- type TestMiddleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetUserResources ¶
GetUserResources retrieves the user resources from the context.
func GetUserRoles ¶
GetUserRoles retrieves the user roles from the context.
func GetUserScopes ¶
GetUserScopes retrieves the user scopes from the context.
func HasResource ¶
HasResource checks if the user has access to a specific resource.
func IsAuthenticated ¶
IsAuthenticated checks if the user is authenticated.
func IsAuthorized ¶
IsAuthorized checks if the user is authorized to perform a specific action based on their roles. It takes a list of allowed roles and returns true if the user has at least one of them.
func IsAuthorizedWithScopes ¶
func IsAuthorizedWithScopes(ctx context.Context, allowedRoles []string, requiredScopes []string, resource string) bool
IsAuthorizedWithScopes checks if the user is authorized to perform a specific action based on their roles and scopes. It takes a list of allowed roles, required scopes, and a resource, and returns true if the user has at least one of the allowed roles and all of the required scopes for the specified resource.
func WithUserID ¶
WithUserID returns a new context with the user ID.
func WithUserResources ¶
WithUserResources returns a new context with the user resources.
func WithUserRoles ¶
WithUserRoles returns a new context with the user roles.
Types ¶
type Config ¶
type Config struct {
// SkipPaths are paths that should skip authentication
SkipPaths []string
// RequireAuth determines if authentication is required for all requests
RequireAuth bool
}
Config holds the configuration for the authentication middleware.
func DefaultConfig ¶ added in v1.5.0
func DefaultConfig() Config
DefaultConfig returns a default configuration for the authentication middleware.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware is a middleware for handling authentication.
func NewMiddleware ¶
NewMiddleware creates a new authentication middleware with JWT support.
func NewMiddlewareWithOIDC ¶
func NewMiddlewareWithOIDC(jwtService *jwt.Service, oidcService *oidc.Service, config Config, logger *zap.Logger) *Middleware
NewMiddlewareWithOIDC creates a new authentication middleware with both JWT and OIDC support.
type TestMiddleware ¶
type TestMiddleware struct {
// contains filtered or unexported fields
}
TestMiddleware is a middleware for testing. It allows us to inject mock services without using reflection.
func NewTestMiddleware ¶
func NewTestMiddleware( jwtValidator func(ctx context.Context, tokenString string) (*jwt.Claims, error), config Config, logger *zap.Logger, ) *TestMiddleware
NewTestMiddleware creates a new test middleware.
func NewTestMiddlewareWithOIDC ¶
func NewTestMiddlewareWithOIDC( jwtValidator func(ctx context.Context, tokenString string) (*jwt.Claims, error), oidcValidator func(ctx context.Context, tokenString string) (*jwt.Claims, error), config Config, logger *zap.Logger, ) *TestMiddleware
NewTestMiddlewareWithOIDC creates a new test middleware with OIDC support.