axum-authentik-auth
Axum extractor and middleware for authentik Proxy Provider forward authentication.
What it solves
When you use authentik's Proxy Provider with forward auth (single application), authentik sits in front of your app behind Nginx. After authenticating the user, it forwards identity information via HTTP headers:
X-authentik-username: alice
X-authentik-email: alice@example.com
X-authentik-name: Alice
X-authentik-uid: abc-123
X-authentik-groups: admin|users
This crate parses those headers into a typed AuthentikUser and exposes it as an
axum extractor, so your handler code is clean and type-safe.
Prerequisites
Nginx configuration
This crate assumes you have authentik's Proxy Provider set up with forward auth. A minimal Nginx config looks like this:
server {
listen 443 ssl;
server_name app.example.com;
# Forward authentication to authentik
auth_request /outpost.goauthentik.io/auth/nginx;
error_page 401 = @goauthentik_proxy_signin;
# Pass authentik headers to the backend
auth_request_set $authentik_username $upstream_http_x_authentik_username;
auth_request_set $authentik_email $upstream_http_x_authentik_email;
auth_request_set $authentik_name $upstream_http_x_authentik_name;
auth_request_set $authentik_uid $upstream_http_x_authentik_uid;
auth_request_set $authentik_groups $upstream_http_x_authentik_groups;
proxy_set_header X-authentik-username $authentik_username;
proxy_set_header X-authentik-email $authentik_email;
proxy_set_header X-authentik-name $authentik_name;
proxy_set_header X-authentik-uid $authentik_uid;
proxy_set_header X-authentik-groups $authentik_groups;
location / {
proxy_pass http://localhost:3000;
}
# authentik sign-in redirect
location @goauthentik_proxy_signin {
return 302 /outpost.goauthentik.io/signin?rd=$scheme://$http_host$request_uri;
}
# authentik proxy endpoints
location /outpost.goauthentik.io {
proxy_pass http://authentik-server:9000/outpost.goauthentik.io;
}
}
Important: This crate reads headers, it does not perform authentication. Your reverse proxy must be configured to reject unauthenticated requests before they reach your application.
Installation
[]
= "0.1"
For custom header prefix support:
[]
= { = "0.1", = ["layer"] }
Quick Start
use ;
use AuthentikUser;
async
async
API Overview
AuthentikUser
The core struct extracted from authentik headers:
Extractor usage:
| Pattern | Behavior |
|---|---|
user: AuthentikUser |
Requires auth, returns 401 if missing |
user: Option<AuthentikUser> |
Optional auth, returns None if missing |
Group check methods:
user.has_group // → bool
user.has_all_groups // → bool
user.has_any_group // → bool
Group Guards
Handler wrappers for declarative group-based access control:
use ;
async
let app = new
.route;
Also available:
require_all_groups(&["admin", "editor"], handler)— must be in all groupsrequire_any_group(&["admin", "moderator"], handler)— must be in any group
Error Responses
Errors are returned as structured JSON:
// 401 Unauthorized
// 403 Forbidden
Error codes:
| Variant | Code | HTTP Status |
|---|---|---|
AuthentikError::Unauthenticated |
1000 | 401 |
AuthentikError::Forbidden |
1001 | 403 |
Custom Header Prefix
If your proxy uses a different header prefix (e.g. X-MyProxy-*), enable the
layer feature and use AuthentikLayer:
use ;
let app = new
.route
.layer;
Feature Flags
| Feature | Description |
|---|---|
layer |
Enables AuthentikLayer / tower middleware for custom header prefixes |
Version Compatibility
| axum-authentik-auth | axum | Rust |
|---|---|---|
| 0.1 | 0.8 | 1.70+ |
License
MIT OR Apache-2.0