Authentication Flow
The BookMe API uses OAuth 2.0 with the 42 Intra provider for user authentication. The flow consists of two endpoints:- Login - Initiates the OAuth flow
- Callback - Handles the OAuth provider response and issues JWT tokens
Flow Diagram
- User clicks “Login” → Redirected to
/oauth/login - System redirects to OAuth provider (42 Intra)
- User authorizes the application
- OAuth provider redirects to
/oauth/callback - System exchanges code for access token
- System fetches user info and creates/updates user in database
- System generates JWT token
- JWT token returned to client
- Client includes JWT in
Authorization: Bearer <token>header for protected routes
GET /oauth/login
Initiates the OAuth 2.0 authentication flow by redirecting the user to the 42 Intra authorization page.Authentication
No authentication required.Rate Limiting
- Rate: 5 requests per 12 seconds per IP
- Status Code on Limit: 429 Too Many Requests
Response
302 Found - Redirects to 42 Intra OAuth authorization URL with:- OAuth state parameter (stored in session for CSRF protection)
- Client ID
- Redirect URI
- Requested scopes
Error Responses
Error message describing what went wrong
Examples
internal/handler/handler_oauth.go:12
GET /oauth/callback
Handles the OAuth provider callback, validates the state parameter, exchanges the authorization code for an access token, fetches user information, and issues a JWT token.Authentication
No authentication required (OAuth code in query parameters).Rate Limiting
- Rate: 5 requests per 12 seconds per IP
- Status Code on Limit: 429 Too Many Requests
Query Parameters
Authorization code from the OAuth provider
CSRF protection state parameter (must match the value stored in session)
Response
302 Found - Redirects to frontend with JWT token and user information:Error Responses
Error message describing what went wrong
Examples
Security Features
- CSRF Protection: State parameter validation
- Campus Restriction: Only Helsinki campus users allowed
- Session Management: OAuth state stored in secure session
- JWT Issuance: Secure token generation for API access
internal/handler/handler_oauth.go:26
Using the JWT Token
Once you receive the JWT token from the callback, include it in theAuthorization header for all protected API requests: