Authentication
TrailBase provides a complete authentication system with JWT tokens, OAuth integration, email verification, and flexible access control.Overview
The authentication system supports multiple flows:- Email/Password Authentication: Traditional username and password login
- OAuth Providers: Google, GitHub, Discord, Microsoft, and more
- JWT Tokens: Stateless authentication with EdDSA signatures
- Refresh Tokens: Long-lived tokens stored in secure cookies
- Email Verification: Account verification workflow
- Password Reset: Secure password recovery
Auth Module
All authentication logic is implemented in
crates/core/src/auth/.User Model
Database Schema
Users are stored in the_user table:
Authenticated User
Once authenticated, users are represented by theUser struct:
The
User type can be extracted from Axum request handlers using FromRequestParts.JWT Tokens
JWT Helper
JWT token generation and validation using EdDSA (Ed25519) signatures.
Token Structure
Key Management
JWT keys are automatically generated on first startup:- Private Key:
traildepot/secrets/key/private_key.pem(EdDSA) - Public Key:
traildepot/secrets/key/public_key.pem
Token Lifetimes
Fromconstants.rs:
- Auth Token: Short-lived (60 minutes), sent in headers
- Refresh Token: Long-lived (30 days), stored in secure HTTP-only cookie
Authentication Flows
Email/Password Registration
Users can log in even without email verification, but some features may require
verified=true.Email/Password Login
auth/api/login.rs using login_with_password() from auth/util.rs.
OAuth Flow
OAuth Integration
Supports multiple OAuth providers with PKCE for security.
- GitHub
- GitLab
- Discord
- Microsoft
- Twitch
- Yandex
- Generic OIDC (OpenID Connect)
Token Refresh
auth/api/refresh.rs.
Password Security
Password Hashing
Passwords are hashed using Argon2id with secure parameters.
- Minimum length
- Complexity rules
- Common password prevention
Email Verification
Verification Flow
Re-send Verification
Password Reset
Reset Flow
Sessions
Sessions are stored in the_session table:
Session Management
- Creation: New session created on login
- Validation: Checked on token refresh
- Logout: Deletes session and invalidates refresh token
- Expiration: Sessions auto-expire based on
expirestimestamp
Access Control
Table-Level ACLs
Access Control Lists define base permissions for unauthenticated and authenticated users.
records/mod.rs:103-110:
Row-Level Access Rules
Access Rules
SQL expressions that control access to individual records.
_USER_: Current authenticated user (or NULL)_ROW_: The record being accessed_REQ_: The request payload (for CREATE/UPDATE)
CSRF Protection
CSRF protection flow:- Auth token includes
csrf_tokenin claims - Client extracts token from auth token
- Client sends token in
x-csrf-tokenheader - Server validates token matches user’s token
Rate Limiting
Rate Limiter
IP-based rate limiting prevents authentication abuse.
- Burst: 5 requests (50 in debug mode)
- Replenish: 1 request every 2 seconds
- Scope: POST methods only (login, register, etc.)
- Key: Client IP address from headers or connection
Configuration
Auth Options
OAuth Provider Setup
Google OAuth
- Create project in Google Cloud Console
- Enable Google+ API
- Create OAuth 2.0 credentials
- Set redirect URI:
<site_url>/api/auth/v1/oauth/callback/google
GitHub OAuth
- Go to Settings > Developer settings > OAuth Apps
- Register new application
- Set callback URL:
<site_url>/api/auth/v1/oauth/callback/github - Copy client ID and secret
Best Practices
Use HTTPS
Always use TLS in production to protect tokens in transit
Short Auth Tokens
Keep auth token TTL short (minutes), rely on refresh tokens
Validate Email
Require email verification for sensitive operations
Implement Logout
Always provide logout to invalidate sessions
Security Considerations
- Token Storage: Store refresh tokens in HTTP-only, secure cookies
- Token Rotation: Refresh tokens are rotated on each use
- Session Tracking: All sessions are tracked in database
- IP Validation: Consider validating IP for sensitive operations
- Admin Separation: Admin endpoints require both auth + CSRF
Next Steps
APIs
Learn how to make authenticated API requests
Data Model
Understand how access control integrates with data