Documentation
¶
Index ¶
- func Chain(middlewares ...func(http.Handler) http.Handler) func(http.Handler) http.Handler
- func ExtractBearerToken(r *http.Request) (string, error)
- func GetClaims(ctx context.Context) (*tokens.Claims, bool)
- func GetUserEmail(ctx context.Context) (string, bool)
- func GetUserID(ctx context.Context) (string, bool)
- func GetUserRoles(ctx context.Context) ([]string, bool)
- type AuthMiddleware
- func (m *AuthMiddleware) Authenticate(next http.Handler) http.Handler
- func (m *AuthMiddleware) RequireAllPermissions(permissionNames ...string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireAllRoles(roleNames ...string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireAnyPermission(permissionNames ...string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireAnyRole(roleNames ...string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireOwnerOrPermission(getUserIDFromRequest func(*http.Request) string, permissionName string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireOwnerOrRole(getUserIDFromRequest func(*http.Request) string, roleNames ...string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequirePermission(permissionName string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireRole(roleName string) func(http.Handler) http.Handler
- type ContextKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractBearerToken ¶
ExtractBearerToken extracts the bearer token from the Authorization header
func GetUserEmail ¶
GetUserEmail extracts the user email 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
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 ¶
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 ¶
RequirePermission returns a middleware that checks if the user has a specific permission
func (*AuthMiddleware) RequireRole ¶
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" )