A Traefik middleware plugin that provides token-based authentication with secure session management.
- Token-based authentication via query parameters
- Secure session cookies with SHA-256 hashed tokens
- Enforced token strength (minimum 32 characters)
- Token parameter takes priority over stored cookies
- Automatic URL cleanup (removes token from URL after authentication)
- HttpOnly, Secure, and SameSite cookie protection
- Configurable error handling: redirect to custom page or return 403 Forbidden
- Constant-time token comparison to prevent timing attacks
Add the plugin to your Traefik static configuration:
experimental:
plugins:
tokenauth:
moduleName: github.com/flohoss/tokenauth
version: v0.4.0Or with Docker labels:
labels:
- 'traefik.experimental.plugins.tokenauth.modulename=github.com/flohoss/tokenauth'
- 'traefik.experimental.plugins.tokenauth.version=v0.4.0'Configure the middleware in your dynamic configuration:
# config.yml
http:
middlewares:
my-tokenauth:
plugin:
tokenauth:
tokenParam: 'token'
cookie:
name: 'auth_session'
httpOnly: true
secure: true
sameSite: 'Strict'
maxAge: 0
allowedTokens:
- 'your-secret-token-abcdefghij1234'
- 'your-secret-token-abcdefghij5678'Or with Docker labels:
labels:
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.tokenParam=token'
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.cookie.name=auth_session'
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.cookie.httpOnly=true'
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.cookie.secure=true'
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.cookie.sameSite=Strict'
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.cookie.maxAge=0'
- 'traefik.http.middlewares.tokenauth.plugin.tokenauth.allowedTokens[0]=your-secret-token-abcdefghij1234'Minimum Token Length: 32 characters
The middleware enforces a minimum token length of 32 characters to prevent weak tokens from being used. This ensures tokens are strong enough to resist brute-force attacks.
Examples of valid tokens:
✓ abcdefghijklmnopqrstuvwxyz012345 (exactly 32 characters)
✓ a-super-long-secure-token-with-many-characters-for-extra-security
Invalid tokens:
✗ short-token (too short)
✗ mytoken (too short)
Generate tokens using cryptographically secure methods:
# Generate 32 random bytes as hex (64 characters)
openssl rand -hex 32http:
routers:
my-router:
rule: 'Host(`example.com`)'
service: my-service
middlewares:
- my-tokenauthOr with Docker:
labels:
- 'traefik.http.routers.my-router.rule=Host(`example.com`)'
- 'traefik.http.routers.my-router.middlewares=tokenauth'| Parameter | Type | Default | Description |
|---|---|---|---|
tokenParam |
string | "token" |
Query parameter name for the authentication token |
errorRedirectURL |
string | empty | Absolute URL for auth failures (must include scheme like https:// or http://, e.g., https://example.com/error) |
cookie.name |
string | "auth_session" |
Name of the session cookie |
cookie.httpOnly |
bool | true |
Set HttpOnly flag on cookies |
cookie.secure |
bool | true |
Set Secure flag on cookies (requires HTTPS) |
cookie.sameSite |
string | "Strict" |
SameSite attribute: "Strict", "Lax", or "None" |
cookie.maxAge |
int | 0 |
Cookie max age in seconds (0 = session cookie) |
allowedTokens |
[]string | [] |
List of valid authentication tokens |
Tokens are enforced to be at least 32 characters long, preventing weak tokens from being used. Configuration with shorter tokens will fail at startup with a clear error message.
When a request fails authentication (invalid token or missing cookie), the middleware either:
- Redirects to the configured
errorRedirectURLwithHTTP 303 See Other, or - Returns
HTTP 403 Forbiddenwith plain text response (if no redirect URL configured)
The response does not reveal whether the failure was due to invalid token format, token not found, or expired session, preventing attackers from learning about your authentication mechanism.
When authentication fails, the middleware handles it in one of two ways:
Option 1: Custom Error Redirect (Recommended)
Redirect to an absolute URL with full scheme and host:
middlewares:
my-tokenauth:
plugin:
tokenauth:
tokenParam: token
errorRedirectURL: https://example.com/access-denied
allowedTokens:
- your-secret-token-abcdefghij1234Or with port:
errorRedirectURL: http://localhost:8080/errorUsers are redirected with HTTP 303 See Other to your custom error page. The errorRedirectURL must be an absolute URL (e.g., https://example.com/error or http://localhost:8080/error) - relative URLs like /error are not allowed.
Option 2: Plain Text 403 Response (Default)
If errorRedirectURL is not configured, users receive:
- Status Code:
HTTP 403 Forbidden - Body: Plain text "Forbidden"
- No HTML page, minimal response footprint
- Use HTTPS: The middleware sets the
Secureflag on cookies, requiring HTTPS - Strong Tokens: Generate tokens with cryptographically secure randomness (minimum 32 characters, recommended 64+)
- Token Storage: Never commit tokens to version control - use environment variables
- Token Rotation: Regularly rotate authentication tokens
- Monitoring: Monitor for unusual authentication patterns
- Rate Limiting: Consider implementing external rate limiting (e.g., Traefik middleware) for additional protection
Token validation uses constant-time comparison to prevent timing-based attacks that could leak token information.
- User visits:
https://example.com?token=your-secret-token-abcdefghij1234 - Middleware validates the token
- Token is hashed (SHA-256) and stored in a secure cookie
- User is redirected to:
https://example.com(clean URL)
- The middleware checks for the session cookie
- If valid, access is granted immediately
- No token in URL is needed for authenticated sessions
- Duration: Configurable via
cookieMaxAge(default: session cookie that expires when browser closes) - Security: Configurable via
cookieHttpOnly,cookieSecure,cookieSameSite(defaults: HttpOnly=true, Secure=true, SameSite=Strict) - Storage: SHA-256 hash of the token (not plaintext)
Example: Persistent cookie for 30 days
middlewares:
my-tokenauth:
plugin:
tokenauth:
cookie:
maxAge: 2592000 # 30 days in seconds
# ... other configThe middleware implements the following authentication priority:
-
Token Parameter (Highest Priority): If a token is provided in the query parameter:
- Validates the token against the allowed tokens list
- On success: Sets the session cookie with hashed token and redirects with
HTTP 307 Temporary Redirect(clean URL) - On failure: Redirects to
errorRedirectURLwithHTTP 303 See Other, or returnsHTTP 403 Forbidden(if no redirect URL)
-
Session Cookie: If no token is in the query:
- Checks for a valid session cookie
- On success: Grants access to the protected resource
- On failure: Redirects to
errorRedirectURLwithHTTP 303 See Other, or returnsHTTP 403 Forbidden(if no redirect URL)
This ensures that providing a token always validates it, even if a previous valid cookie exists.
- Use HTTPS: The middleware sets the
Secureflag on cookies, requiring HTTPS - Strong Tokens: Use cryptographically random tokens (e.g., 32+ characters)
- Token Storage: Never expose
allowedTokensin public repositories - Environment Variables: Consider loading tokens from environment variables
- Token Rotation: Regularly rotate authentication tokens