Authentication and authorization middleware for HTTP endpoints.
This middleware provides:
Authentication and authorization middleware for HTTP endpoints. This middleware provides: - JWT token validation - Session-based authentication - Role-based authorization - Request context enrichment with user information
(create-forbidden-response message reason)(create-forbidden-response message reason request)Creates standardized 403 Forbidden response.
For web UI requests (path starts with /web), redirects to login page. For API requests, returns JSON error response.
Args: message: Error message string reason: Keyword reason code request: Optional request map to determine response type
Returns: Ring response map
Creates standardized 403 Forbidden response. For web UI requests (path starts with /web), redirects to login page. For API requests, returns JSON error response. Args: message: Error message string reason: Keyword reason code request: Optional request map to determine response type Returns: Ring response map
(create-unauthorized-response message reason)(create-unauthorized-response message reason request)Creates standardized 401 Unauthorized response.
For web UI requests (path starts with /web), redirects to login page. For API requests, returns JSON error response.
Args: message: Error message string reason: Keyword reason code request: Optional request map to determine response type
Returns: Ring response map
Creates standardized 401 Unauthorized response. For web UI requests (path starts with /web), redirects to login page. For API requests, returns JSON error response. Args: message: Error message string reason: Keyword reason code request: Optional request map to determine response type Returns: Ring response map
(extract-bearer-token request)Extracts Bearer token from Authorization header.
Args: request: HTTP request map
Returns: String token or nil if not found
Extracts Bearer token from Authorization header. Args: request: HTTP request map Returns: String token or nil if not found
(extract-session-token request)Extracts session token from request headers or cookies.
Looks for token in:
Args: request: HTTP request map
Returns: String token or nil if not found
Extracts session token from request headers or cookies. Looks for token in: 1. X-Session-Token header 2. session-token cookie Args: request: HTTP request map Returns: String token or nil if not found
(flexible-authentication-middleware user-service)(flexible-authentication-middleware user-service handler)Middleware that accepts either JWT or session token authentication.
Tries JWT first (from Authorization header), then falls back to session token. Adds user information to request on successful authentication.
This is a middleware factory function compatible with Reitit. Call with user-service to get a middleware function.
Args: user-service: User service instance for session validation handler: (optional, for direct wrapping) Handler function
Returns: Middleware function that takes handler and returns wrapped handler
Example: ;; In Reitit routes: {:middleware [[flexible-authentication-middleware user-service]]}
Middleware that accepts either JWT or session token authentication.
Tries JWT first (from Authorization header), then falls back to session token.
Adds user information to request on successful authentication.
This is a middleware factory function compatible with Reitit.
Call with user-service to get a middleware function.
Args:
user-service: User service instance for session validation
handler: (optional, for direct wrapping) Handler function
Returns:
Middleware function that takes handler and returns wrapped handler
Example:
;; In Reitit routes:
{:middleware [[flexible-authentication-middleware user-service]]}(jwt-authentication-middleware handler)Middleware that validates JWT tokens from Authorization header.
Extracts Bearer token, validates it, and adds user information to request. On validation failure, returns 401 response.
Args: handler: Next handler in the chain
Returns: Wrapped handler function
Middleware that validates JWT tokens from Authorization header. Extracts Bearer token, validates it, and adds user information to request. On validation failure, returns 401 response. Args: handler: Next handler in the chain Returns: Wrapped handler function
(protect-admin-only user-service handler)Wraps handler with admin-only access control.
Args: user-service: User service for session validation handler: Handler to protect
Returns: Protected handler with authentication and admin authorization
Wraps handler with admin-only access control. Args: user-service: User service for session validation handler: Handler to protect Returns: Protected handler with authentication and admin authorization
(protect-with-flexible-auth user-service handler)Wraps handler with flexible authentication (JWT or session).
Args: user-service: User service for session validation handler: Handler to protect
Returns: Protected handler
Wraps handler with flexible authentication (JWT or session). Args: user-service: User service for session validation handler: Handler to protect Returns: Protected handler
(protect-with-jwt handler)Wraps handler with JWT authentication.
Args: handler: Handler to protect
Returns: Protected handler
Wraps handler with JWT authentication. Args: handler: Handler to protect Returns: Protected handler
(protect-with-session user-service handler)Wraps handler with session authentication.
Args: user-service: User service for session validation handler: Handler to protect
Returns: Protected handler
Wraps handler with session authentication. Args: user-service: User service for session validation handler: Handler to protect Returns: Protected handler
(require-admin-middleware handler)Middleware that requires admin role.
Args: handler: Next handler in the chain
Returns: Wrapped handler function
Middleware that requires admin role. Args: handler: Next handler in the chain Returns: Wrapped handler function
(require-role-middleware required-roles handler)Middleware that requires user to have specific role(s).
Must be used after authentication middleware that sets :user in request.
Args: required-roles: Set of required roles (keywords) handler: Next handler in the chain
Returns: Wrapped handler function
Middleware that requires user to have specific role(s). Must be used after authentication middleware that sets :user in request. Args: required-roles: Set of required roles (keywords) handler: Next handler in the chain Returns: Wrapped handler function
(session-authentication-middleware user-service handler)Middleware that validates session tokens and updates session access time.
Extracts session token, validates it through user service, and adds user information to request. On validation failure, returns 401 response.
Args: user-service: User service instance for session validation handler: Next handler in the chain
Returns: Wrapped handler function
Middleware that validates session tokens and updates session access time. Extracts session token, validates it through user service, and adds user information to request. On validation failure, returns 401 response. Args: user-service: User service instance for session validation handler: Next handler in the chain Returns: Wrapped handler function
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |