Skip to main content

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 the User 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
Never commit your private key to version control. It’s automatically excluded via .gitignore.

Token Lifetimes

From constants.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

Implemented in auth/api/login.rs using login_with_password() from auth/util.rs.

OAuth Flow

OAuth Integration

Supports multiple OAuth providers with PKCE for security.
Supported providers:
  • Google
  • GitHub
  • GitLab
  • Discord
  • Microsoft
  • Facebook
  • Twitch
  • Yandex
  • Generic OIDC (OpenID Connect)
OAuth Flow:

Token Refresh

Implemented in auth/api/refresh.rs.

Password Security

Password Hashing

Passwords are hashed using Argon2id with secure parameters.
Password Requirements (configurable):
  • Minimum length
  • Complexity rules
  • Common password prevention

Email Verification

Verification Flow

Re-send Verification

Password Reset

Reset Flow

Reset codes expire after a configured time period (default: 1 hour).

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 expires timestamp

Access Control

Table-Level ACLs

Access Control Lists define base permissions for unauthenticated and authenticated users.
Permissions from records/mod.rs:103-110:

Row-Level Access Rules

Access Rules

SQL expressions that control access to individual records.
Access rules are SQL expressions with special variables:
  • _USER_: Current authenticated user (or NULL)
  • _ROW_: The record being accessed
  • _REQ_: The request payload (for CREATE/UPDATE)
Example Rules:
Complex Example:

CSRF Protection

Admin API endpoints require CSRF token validation to prevent cross-site request forgery.
CSRF protection flow:
  1. Auth token includes csrf_token in claims
  2. Client extracts token from auth token
  3. Client sends token in x-csrf-token header
  4. Server validates token matches user’s token

Rate Limiting

Rate Limiter

IP-based rate limiting prevents authentication abuse.
Auth endpoints have rate limiting:
  • 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

  1. Create project in Google Cloud Console
  2. Enable Google+ API
  3. Create OAuth 2.0 credentials
  4. Set redirect URI: <site_url>/api/auth/v1/oauth/callback/google

GitHub OAuth

  1. Go to Settings > Developer settings > OAuth Apps
  2. Register new application
  3. Set callback URL: <site_url>/api/auth/v1/oauth/callback/github
  4. 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