middleware

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetUserID

func GetUserID(ctx context.Context) (string, bool)

GetUserID retrieves the user ID from the context.

func GetUserResources

func GetUserResources(ctx context.Context) ([]string, bool)

GetUserResources retrieves the user resources from the context.

func GetUserRoles

func GetUserRoles(ctx context.Context) ([]string, bool)

GetUserRoles retrieves the user roles from the context.

func GetUserScopes

func GetUserScopes(ctx context.Context) ([]string, bool)

GetUserScopes retrieves the user scopes from the context.

func HasResource

func HasResource(ctx context.Context, resource string) bool

HasResource checks if the user has access to a specific resource.

func HasRole

func HasRole(ctx context.Context, role string) bool

HasRole checks if the user has a specific role.

func HasScope

func HasScope(ctx context.Context, scope string) bool

HasScope checks if the user has a specific scope.

func IsAuthenticated

func IsAuthenticated(ctx context.Context) bool

IsAuthenticated checks if the user is authenticated.

func IsAuthorized

func IsAuthorized(ctx context.Context, allowedRoles []string) bool

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

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID returns a new context with the user ID.

func WithUserResources

func WithUserResources(ctx context.Context, resources []string) context.Context

WithUserResources returns a new context with the user resources.

func WithUserRoles

func WithUserRoles(ctx context.Context, roles []string) context.Context

WithUserRoles returns a new context with the user roles.

func WithUserScopes

func WithUserScopes(ctx context.Context, scopes []string) context.Context

WithUserScopes returns a new context with the user scopes.

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

func NewMiddleware(jwtService *jwt.Service, config Config, logger *zap.Logger) *Middleware

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.

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler is the HTTP middleware function.

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.

func (*TestMiddleware) Handler

func (m *TestMiddleware) Handler(next http.Handler) http.Handler

Handler is the HTTP middleware function. It mimics the behavior of the real middleware but uses the injected validators.

Jump to

Keyboard shortcuts

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