API: Authentication
Relevant source files
The following files were used as context for generating this wiki page:The authentication system for the Traduora API is responsible for user and client registration, identity verification, and securing API endpoints. It uses JSON Web Tokens (JWT) for managing authenticated sessions. The system supports multiple authentication methods, including traditional email/password, API client credentials, and external OAuth providers like Google.
Key functionalities include user signup (with optional invitation-only mode), token generation for both users and API clients, and password management features like "forgot password" and "change password". Access to protected API resources is controlled via a JWT-based strategy, ensuring that only authenticated and authorized entities can perform sensitive operations.
Authentication Architecture
The authentication module is primarily managed by the AuthController, which orchestrates the logic by delegating to various services like UserService, AuthService, and MailService. The system is built around the concept of "grant types" to handle different authentication scenarios.
Sources: api/src/controllers/auth.controller.ts:40-50
High-Level Authentication Flow
The diagram below illustrates the main authentication pathways available in the API.
Authentication Endpoints
The AuthController exposes several endpoints under the /api/v1/auth path to handle all authentication-related operations.
| Method | Endpoint | Description |
|---|---|---|
GET | /providers | Lists available external authentication providers. |
POST | /signup | Creates a new user account using email and password. |
POST | /signup-provider | Creates a new user account using an external provider. |
POST | /token | Requests an authentication token for a user or project client. |
POST | /forgot-password | Initiates the password reset process for a user. |
POST | /reset-password | Resets a user's password using a token. |
POST | /change-password | Allows an authenticated user to change their password. |
Sources: api/src/controllers/auth.controller.ts:53-328
Token Generation
The primary endpoint for authentication is POST /api/v1/auth/token. It issues a JWT access_token upon successful validation of credentials. The type of validation is determined by the grant_type parameter in the request body.
Sources: api/src/controllers/auth.controller.ts:184-232
Grant Types
The grant_type field in the AuthenticateRequest DTO specifies the authentication method.
| Grant Type | Description | Required Fields |
|---|---|---|
password | For user authentication with email and password. | username, password |
client_credentials | For programmatic access using an API client's ID and secret. | client_id, client_secret |
provider | For internal use with external OAuth providers (e.g., Google). | code |
Sources: api/src/domain/http.ts:18-24, api/src/domain/http.ts:318-346
Password Grant Flow
This sequence diagram shows the process for authenticating a user with their email and password.
Client Credentials Grant Flow
This flow is used for authenticating API clients, which are typically used for automation and CI/CD workflows.
User Registration
New users can be created via the POST /api/v1/auth/signup endpoint. The system can be configured to be invitation-only by setting config.signupsEnabled to false. If signups are restricted, a user must have a pending invitation to be able to register. Upon successful registration, the user receives an access token.
Sources: api/src/controllers/auth.controller.ts:79-137
The following diagram shows the user signup process.
JWT Structure and Validation
The authentication system relies on JWTs to secure endpoints. A guard (AuthGuard()) is applied to protected controllers and routes.
JWT Payload
The payload of the JWT contains essential information to identify the authenticated entity.
- sub: The subject of the token, which is the User ID or Project Client ID.
- type: The type of the subject, either
'user'or'client'.
Sources: api/src/domain/http.ts:13-16, api/src/controllers/auth.controller.ts:121, api/src/controllers/auth.controller.ts:260
JWT Validation (JwtStrategy)
The JwtStrategy is a Passport strategy responsible for validating incoming JWTs from the Authorization header.
- It extracts the token from the request.
- It verifies the token's signature and expiration using the secret key defined in
config.jwtSecret. - Upon successful validation, it returns the decoded payload (
{ sub, type }). - This payload is then attached to the request object, making it available to downstream services like
AuthorizationServiceto perform fine-grained access control.
Sources: api/src/services/jwt.strategy.ts
Password Management
The API provides endpoints for users to manage their passwords securely.
Forgot & Reset Password Flow
This process allows users who have forgotten their password to reset it via an email token.
Change Password
Authenticated users can change their own password at the POST /change-password endpoint by providing their old and new passwords. This endpoint is protected and requires a valid access token.
Sources: api/src/controllers/auth.controller.ts:292-305, api/test/auth.e2e-spec.ts:241-262