Authentication API
The Authentication API provides endpoints for user authentication, token management, and password operations.
Endpoints
Request OTP
Verifies username and password, then sends an OTP if two-factor authentication is enabled. If 2FA is disabled, returns access and refresh tokens directly.
Request Body
Responses
200 OK - Authentication successful
If 2FA is disabled:
If 2FA is enabled:
404 Not Found - Invalid credentials
or403 Forbidden - Inactive account
Resend OTP
Resends the OTP if the previous one has expired.
Request Body
Responses
200 OK - OTP resent
404 Not Found - User not found
403 Forbidden - Inactive account
Verify OTP
Verifies the OTP and returns access and refresh tokens if valid.
Request Body
Responses
200 OK - OTP verified
400 Bad Request - Invalid OTP
400 Bad Request - Expired OTP
404 Not Found - User not found
Refresh Token
Uses a refresh token to obtain a new access token.
Request Body
Responses
200 OK - Token refreshed
401 Unauthorized - Invalid refresh token
401 Unauthorized - Expired refresh token
Reset Password
Resets a user's password and sends the new password via email. Requires the reset_password permission.
Path Parameters
user_id(UUID): ID of the user whose password will be reset
Responses
200 OK - Password reset
404 Not Found - User not found
403 Forbidden - Insufficient permissions
Change Password
Changes the current user's password. Requires the change_password permission.
Request Body
Responses
200 OK - Password changed
400 Bad Request - Incorrect current password
404 Not Found - User not found
403 Forbidden - Insufficient permissions
Authentication Flow
Standard Authentication (without 2FA)
- Client sends username and password to
/authentication/request-otp - Server validates credentials and returns access and refresh tokens
- Client includes the access token in the Authorization header for subsequent requests
- When the access token expires, client uses the refresh token to get a new access token
Two-Factor Authentication (with 2FA)
- Client sends username and password to
/authentication/request-otp - Server validates credentials and sends an OTP to the user's email
- Client prompts user for the OTP and sends it to
/authentication/verify-otp - Server validates the OTP and returns access and refresh tokens
- Client includes the access token in the Authorization header for subsequent requests
- When the access token expires, client uses the refresh token to get a new access token
Token Format
Access Token
The access token is a JWT with the following claims:
sub: User codeuser_id: User IDis_active: Whether the user account is activeexp: Expiration timetype: Token type ("access")
Refresh Token
The refresh token is a JWT with the following claims:
sub: User codeuser_id: User IDexp: Expiration timetype: Token type ("refresh")
Security Considerations
- Access tokens are valid for 30 minutes
- Refresh tokens are valid for 7 days
- OTPs are valid for 3 minutes
- All passwords are hashed using bcrypt
- JWT tokens are signed using HS256 algorithm
- Two-factor authentication is optional but recommended