authrust
Framework-agnostic authentication and authorization for Rust, built on http and tower.
OAuth 2.0 / OpenID Connect (Google, Azure AD, …), credentials and LDAP, JWT access/refresh tokens, PostgreSQL sessions with optional Redis caching, cookies, CSRF protection, and role-based access control — all as tower layers and plain http handlers.
Author: Izet Molla
Works with: axum · actix-web · tonic · hyper · any tower stack
Features
| Area | What you get |
|---|---|
| OAuth / OIDC | Discovery, PKCE, state, and nonce checks |
| Providers | Google, Azure AD / Entra ID, credentials, LDAP / Active Directory |
| JWT | HS256 / HS384 / HS512 access + refresh pairs ("60s", "15m", "7d", "1y", …) |
| Sessions | sqlx + PostgreSQL, optional Redis cache |
| Protection | Bearer JWT for APIs · session cookie + redirect for web pages |
| RBAC | name:perms grants ("admin:rw", "hr:r") |
| Connect flow | Link extra OAuth scopes or accounts to an existing user |
| Security | Signed double-submit CSRF, __Host- / __Secure- cookies, cross-subdomain SSO, PBKDF2-SHA256 |
Installation
[]
= "0.1"
The axum feature is enabled by default and adds Authorization::handler() (returns an axum::Router). Disable it when you are not using axum:
= { = "0.1", = false }
Quick start
use Arc;
use ;
async
Expected database schema: examples/schema.sql.
HTTP endpoints
auth.handler() mounts routes under authrust::DEFAULT_BASE_PATH (/api/authorization):
| Method | Path | Handler | Description |
|---|---|---|---|
GET |
{base}/providers |
get_providers |
JSON list of providers (Auth.js-compatible) |
ANY |
{base}/provider/{provider} |
handle_sign_in |
Start sign-in (OAuth redirect or credentials POST) |
ANY |
{base}/provider/{provider}/callback |
handle_callback |
OAuth/OIDC callback → session + callback page |
You can also register the three handlers yourself, or use auth.route(request) as a catch-all dispatcher (see the actix-web example).
Sign-in options
| Query / header | Effect |
|---|---|
?json=true · X-Auth-Return-Redirect: 1 · Accept: application/json |
Return the authorization URL as JSON instead of a 302 |
?flow=token · X-Auth-Flow: token |
Token flow for mobile / API clients |
?connect=1&resource_id=<uuid> |
Connect flow — link OAuth account/scopes to an existing user; runs Config::on_provider_connect when done |
Middlewares
All middlewares are tower layers:
# use Authorization;
#
Reading auth data in a handler
# use ;
# async
Configuration
# use Duration;
# use ;
Config ;
Cross-subdomain SSO
Share sessions across example.com, admin.example.com, etc. All apps must use the same jwt_secret:
let cookies = cross_subdomain_cookies;
// Config { cookies, .. }
Tokens, sessions & roles
| API | Purpose |
|---|---|
auth.authorize(opts) |
Create a session and sign an access/refresh pair (OAuth callback does this; call it yourself for custom flows such as LDAP) |
auth.check_session(&mut w, &r) |
Validate refresh token, refresh access token with up-to-date roles, re-set session cookie |
auth.get_role(endpoint_roles, user_roles) |
Check name:perms grants (r / w / rw) → (has_role, can_read, can_write) |
hash_password / check_password |
PBKDF2-SHA256 ($pbkdf2-sha256$...) for credentials storage |
Framework integration
authrust depends on no web framework. Examples are runnable workspace members:
| Framework | Example | Adapter |
|---|---|---|
| axum | examples/axum |
None — auth.handler() is a Router |
| actix-web | examples/actix |
Two small helpers (included in the example) |
hyper / tonic / any tower stack |
— | None |
How to run and test: examples/README.md.
Providers
| Provider | Module | Notes | Docs |
|---|---|---|---|
providers::google |
OIDC — discovery, PKCE + state + nonce | docs | |
| Azure AD / Entra ID | providers::azuread |
OIDC v2.0 + Microsoft Graph profile | docs |
| Credentials | providers::credentials |
Username/password with a custom authorize callback |
docs |
| LDAP / Active Directory | providers::ldap |
Directory bind + attribute and role mapping | docs |
Overview and custom providers: docs/providers/README.md.
Any other OAuth/OIDC service works as a plain OAuthProvider. The Provider trait is public if you need a custom one.
Releasing
Releases are automated with release-plz (same pattern used across the Rust ecosystem).
| Conventional commit | SemVer bump |
|---|---|
fix: … |
patch — 0.1.0 → 0.1.1 |
feat: … |
minor — 0.1.0 → 0.2.0 |
feat!: … or BREAKING CHANGE: |
major — 0.1.0 → 1.0.0 |
- Merge work to
mainwith conventional commits. - GitHub Actions opens/updates a release PR (
Cargo.toml+CHANGELOG.md). - Merge that PR → tag
vX.Y.Z, GitHub Release, andcargo publishto crates.io.
One-time setup: allow Actions to create PRs, and add the CARGO_REGISTRY_TOKEN repository secret (crates.io API token).
License
Licensed under either of
at your option.
Copyright (c) 2026 Izet Molla