middleware

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain(middlewares ...func(http.Handler) http.Handler) func(http.Handler) http.Handler

Chain chains multiple middleware functions

func ExtractBearerToken

func ExtractBearerToken(r *http.Request) (string, error)

ExtractBearerToken extracts the bearer token from the Authorization header

func GetClaims

func GetClaims(ctx context.Context) (*tokens.Claims, bool)

GetClaims extracts the JWT claims from the request context

func GetUserEmail

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

GetUserEmail extracts the user email from the request context

func GetUserID

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

GetUserID extracts the user ID from the request context

func GetUserRoles

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

GetUserRoles extracts the user roles from the request context

Types

type AuthMiddleware

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

AuthMiddleware is a middleware that validates JWT tokens

func NewAuthMiddleware

func NewAuthMiddleware(service *auth.Service) *AuthMiddleware

NewAuthMiddleware creates a new auth middleware

func (*AuthMiddleware) Authenticate

func (m *AuthMiddleware) Authenticate(next http.Handler) http.Handler

Authenticate returns a middleware that validates JWT tokens

func (*AuthMiddleware) RequireAllPermissions added in v0.0.3

func (m *AuthMiddleware) RequireAllPermissions(permissionNames ...string) func(http.Handler) http.Handler

RequireAllPermissions returns a middleware that checks if the user has all of the specified permissions

func (*AuthMiddleware) RequireAllRoles added in v0.0.3

func (m *AuthMiddleware) RequireAllRoles(roleNames ...string) func(http.Handler) http.Handler

RequireAllRoles returns a middleware that checks if the user has all of the specified roles

func (*AuthMiddleware) RequireAnyPermission added in v0.0.3

func (m *AuthMiddleware) RequireAnyPermission(permissionNames ...string) func(http.Handler) http.Handler

RequireAnyPermission returns a middleware that checks if the user has any of the specified permissions

func (*AuthMiddleware) RequireAnyRole

func (m *AuthMiddleware) RequireAnyRole(roleNames ...string) func(http.Handler) http.Handler

RequireAnyRole returns a middleware that checks if the user has any of the specified roles

func (*AuthMiddleware) RequireOwnerOrPermission added in v0.0.3

func (m *AuthMiddleware) RequireOwnerOrPermission(
	getUserIDFromRequest func(*http.Request) string,
	permissionName string,
) func(http.Handler) http.Handler

RequireOwnerOrPermission returns a middleware that allows access if: 1. The authenticated user is the owner of the resource (userID matches the resource owner), OR 2. The authenticated user has the specified permission

getUserIDFromRequest is a function that extracts the resource owner's user ID from the request (e.g., from URL parameters like /users/:id)

func (*AuthMiddleware) RequireOwnerOrRole added in v0.0.3

func (m *AuthMiddleware) RequireOwnerOrRole(
	getUserIDFromRequest func(*http.Request) string,
	roleNames ...string,
) func(http.Handler) http.Handler

RequireOwnerOrRole returns a middleware that allows access if: 1. The authenticated user is the owner of the resource, OR 2. The authenticated user has any of the specified roles

func (*AuthMiddleware) RequirePermission

func (m *AuthMiddleware) RequirePermission(permissionName string) func(http.Handler) http.Handler

RequirePermission returns a middleware that checks if the user has a specific permission

func (*AuthMiddleware) RequireRole

func (m *AuthMiddleware) RequireRole(roleName string) func(http.Handler) http.Handler

RequireRole returns a middleware that checks if the user has a specific role

type ContextKey

type ContextKey string

ContextKey is the type for context keys

const (
	// UserIDKey is the context key for user ID
	UserIDKey ContextKey = "user_id"

	// UserEmailKey is the context key for user email
	UserEmailKey ContextKey = "user_email"

	// UserRolesKey is the context key for user roles
	UserRolesKey ContextKey = "user_roles"

	// ClaimsKey is the context key for JWT claims
	ClaimsKey ContextKey = "claims"
)

Jump to

Keyboard shortcuts

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