Authentication Overview
The authentication process involves two main phases:- OAuth 2.0 Flow - Initial authentication via 42 Intra
- JWT Token Management - Ongoing API authorization
OAuth 2.0 Flow
Step-by-Step Process
Initiate Login
User clicks “Login” and is redirected to The server generates a cryptographically secure state token (32 bytes, hex-encoded) and stores it in a session cookie for CSRF protection.
/oauth/loginState Validation
Server validates the state token to prevent CSRF attacksSource:
internal/oauth/service.go:97-114Token Exchange
System exchanges authorization code for access token with 42 Intra APISource:
internal/oauth/provider42.go:63-70Fetch User Info
System fetches user information from 42 Intra with retry logic:
- Max Retries: 3
- Retry Wait: 1-5 seconds with exponential backoff
- Timeout: 15 seconds
internal/oauth/provider42.go:73-111The retry mechanism ensures reliability when communicating with the 42 Intra API.
Campus Validation
Verify user belongs to Hive Helsinki (Campus ID: 13)Source:
internal/oauth/service.go:75-84Create/Update User
System finds existing user or creates a new database recordUser roles are determined by 42 Intra staff status:
staff? = true→ Staff rolestaff? = false→ Student role
internal/oauth/provider42.go:128-160Generate JWT Token
Server generates a JWT access token containing user informationToken Configuration:
- Algorithm: HS256 (HMAC with SHA-256)
- Expiration: 1 hour
- Token Type: access
internal/auth/auth.go:76-96JWT Token Structure
The JWT token contains the following claims:Custom Claims
| Claim | Description | Source |
|---|---|---|
name | User’s login name from 42 Intra | user.Name |
role | User role: student or staff | user.Role |
sub | User ID (subject) | user.ID |
iss | Token issuer (always “access”) | tokenTypeAccess |
iat | Issued at timestamp | Current time |
exp | Expiration timestamp | Current time + 1 hour |
internal/auth/auth.go:20-25, internal/auth/auth.go:79-88
Using JWT Tokens
Making Authenticated Requests
Include the JWT token in theAuthorization header with the Bearer scheme:
Token Validation
The server validates JWT tokens on protected endpoints:- Extract Bearer Token from Authorization header
- Verify Signature using HS256 algorithm
- Check Expiration - Reject if expired
- Validate Claims - Ensure token type is “access”
- Extract User Info - Load user ID, name, and role into request context
internal/auth/auth.go:99-125, internal/auth/auth.go:128-141
Error Responses
OAuth Errors
| Error | Status Code | Description |
|---|---|---|
invalid or missing oauth code | 400 | Authorization code is missing or invalid |
oauth state mismatch | 403 | CSRF protection - state token mismatch |
access denied: only helsinki campus student allowed | 403 | User is not from Hive Helsinki campus |
oauth request timeout | 504 | Request to 42 Intra API timed out |
oauth token exchange failed | 500 | Failed to exchange code for token |
failed to fetch user info from oauth provider | 500 | Failed to retrieve user data from 42 |
failed to find or create user | 500 | Database error during user creation |
internal/oauth/errors.go:40-81
JWT Authentication Errors
| Error | Status Code | Description |
|---|---|---|
no auth header included in request | 401 | Missing Authorization header |
bearer token is empty | 401 | Empty token after “Bearer ” prefix |
bearer token is incorrect | 401 | Missing “Bearer ” prefix |
invalid token | 401 | Token signature validation failed |
expired token | 401 | Token has expired (> 1 hour old) |
internal/auth/auth.go:49-55
All authentication errors return JSON responses with an
error field containing the error message.Security Features
CSRF Protection
- Cryptographically secure random state tokens (32 bytes)
- State stored in HTTP-only session cookies
- State validation on OAuth callback
internal/oauth/service.go:32-50, internal/oauth/service.go:122-126
Token Security
- JWT tokens signed with HS256 algorithm
- Secret key stored in environment variables
- Token expiration enforced server-side
- HMAC signature prevents token tampering
Session Management
- Session cookies for OAuth state
- Cleanup of stale session data
- Session secrets from environment configuration
Ensure
JWT_SECRET environment variable is set to a strong, random value in production.