Authentication
Overview
The Rhesis backend implements a native authentication system with a pluggable provider architecture. The system supports email/password authentication, OAuth providers (Google and GitHub), magic link login, and token-based API access.
Authentication Methods
Email/Password Authentication
Users can register and log in with email and password:
- User submits email and password via the frontend
- Backend validates credentials (bcrypt hash comparison)
- Backend issues a short-lived access token (JWT, 15 minutes) and a long-lived refresh token (opaque, 7 days)
- Frontend stores both tokens in an httpOnly cookie via NextAuth
- Access tokens are refreshed automatically before expiry
OAuth Authentication (Google, GitHub)
OAuth login is available when the corresponding provider is configured:
- User clicks “Sign in with Google” or “Sign in with GitHub”
- Backend redirects to the OAuth provider’s authorization page
- Provider redirects back to
/auth/callbackwith an authorization code - Backend exchanges the code, creates or updates the user, and issues tokens
- Tokens are wrapped in a short-lived auth code (60 seconds) and passed via redirect
- Frontend exchanges the auth code for access and refresh tokens
Magic Link Authentication
Passwordless login via email:
- User enters their email address
- Backend sends a single-use magic link (15-minute expiry, JTI-tracked via Redis)
- User clicks the link and is authenticated immediately
Token-Based API Authentication
For programmatic API access:
- Client obtains a JWT token through the
/tokens/endpoint - Client includes the token in the
Authorizationheader with each request - Backend validates the token and extracts user information
Provider Architecture
The authentication system uses a pluggable provider registry:
Each provider implements the AuthProvider interface:
Enabling OAuth Providers
OAuth providers are automatically enabled when their credentials are configured:
If no OAuth credentials are provided, only email/password authentication is available.
Authentication Configuration
Token System
The authentication system uses multiple token types:
- Access token (JWT, 15 minutes): Short-lived token for API requests, refreshed automatically
- Refresh token (opaque, 7 days): Long-lived token stored as a SHA-256 hash in the database, rotated on every use
- Auth code (JWT, 60 seconds): Wraps access and refresh tokens during OAuth redirects
- Email flow tokens (JWT): Used for email verification (24 hours), password reset (1 hour, single-use), and magic link (15 minutes, single-use)
Token Rotation
Refresh tokens implement automatic rotation with reuse detection:
- Each refresh token can only be used once
- On use, the old token is revoked and a new one is issued in the same family
- If a revoked token is reused, the entire token family is revoked (theft detection)
- Expired tokens are cleaned up periodically
Authentication Middleware
The application uses a custom route class to enforce authentication requirements:
Rate Limiting
Authentication endpoints are rate-limited to prevent abuse:
- Login: 20 requests/hour per IP
- Registration: 10 requests/hour per IP
- Password reset: 5 requests/hour per IP
- Magic link: 5 requests/hour per IP
- Email verification resend: 5 requests/hour per IP
Security Considerations
- HTTPS is enforced for all communications
- Access tokens are short-lived (15 minutes) to limit the impact of token theft
- Refresh tokens are stored as SHA-256 hashes (raw token never persisted)
- Passwords are hashed with bcrypt via passlib
- Single-use tokens (magic link, password reset) are enforced via Redis JTI tracking
- All email-sensitive endpoints return HTTP 200 regardless of user existence (prevents email enumeration)
- OAuth redirect URLs are validated against an allowlist of frontend domains
- Session cookies use httpOnly, secure, and sameSite flags
Email Flows
The following email flows require SMTP configuration:
- Email verification: Sent on registration, can be resent
- Password reset: Sent via forgot-password flow
- Magic link: Passwordless login link
- Welcome email: Sent to new users
See Environment Variables for SMTP configuration details.