Overview
The Portal Self-Service Backend API uses JWT (JSON Web Token) authentication with Azure Active Directory (Azure AD) to secure all API endpoints. Every request to protected endpoints must include a valid JWT token issued by Azure AD.Token-Based
Uses industry-standard JWT tokens for stateless authentication
Azure AD Integration
Leverages Microsoft Azure AD for identity management
User Identity
Extracts user information from token claims (oid, upn, name)
Database Validation
Validates users exist in the internal employee database
Authentication Flow
The API implements a two-stage authentication and authorization process:- JWT Validation - Validates the token signature, expiration, issuer, and audience
- User Loading - Verifies the user exists in the internal employee database
Obtaining an Access Token
Before making API requests, you must obtain a JWT access token from Azure AD.Azure AD Configuration
The base URL for the Azure AD tenant issuing tokensFormat:
https://login.microsoftonline.com/{tenant-id}/v2.0The Application ID URI of the backend API registered in Azure ADExample:
api://your-backend-api-client-idToken Request Example
To obtain a token, make a request to Azure AD’s token endpoint:Making Authenticated Requests
Include the access token in theAuthorization header of every API request using the Bearer scheme.
Authorization Header Format
Example Request
Token Validation Process
The API validates tokens using theexpress-oauth2-jwt-bearer library. The validation process checks:
Signature Verification
Verifies the token was signed by Azure AD using JWKS
Issuer Check
Ensures the token was issued by the configured Azure AD tenant
Audience Validation
Confirms the token was issued for this specific API
Expiration Check
Validates the token has not expired
Token Claims Extracted
The API extracts the following claims from validated JWT tokens:Object ID - The unique identifier for the user in Azure ADUsed to look up the employee in the internal database via the
azure_oid fieldUser Principal Name - The user’s email address (primary identifier)Falls back to
preferred_username if upn is not presentPreferred Username - Alternative email/username claimUsed when
upn is not available in the tokenDisplay Name - The user’s full name
User Identity Object
After successful JWT validation, the API creates auserIdentity object attached to the request:
User Database Validation
After JWT validation, thecargarUsuario middleware performs database validation:
- Extracts the
azureIdfromreq.userIdentity - Queries the database for an employee with matching
azure_oid - Includes related data (role, vacation records)
- Attaches the employee object to
req.empleado
Database Query
Employee Object
After successful database validation, the employee object is available to controllers:Error Responses
The authentication system returns specific error codes and messages for different failure scenarios.401 Unauthorized - Invalid or Missing Token
Returned when the JWT token is invalid, expired, or missing from the request.Human-readable error message
Machine-readable error code:
invalid_auth_token401 Unauthorized - Missing Azure ID
Returned when the token is valid but missing the requiredoid claim.
Example Response:
403 Forbidden - User Not Found
Returned when the user’s Azure AD identity (oid) is not found in the employee database.
Example Response:
500 Internal Server Error - Database Error
Returned when there’s an error querying the employee database. Example Response:Error Code Reference
401 - Invalid Token
Token is invalid, expired, or missing
401 - Missing Azure ID
Token lacks required
oid claim403 - User Not Found
User’s Azure AD identity not in database
500 - Database Error
Internal error querying employee data
Complete Example
Here’s a complete example of authenticating and making an API request:Step 1: Obtain Access Token
Step 2: Make Authenticated Request
Step 3: Receive Response
Security Best Practices
Secure Token Storage
Never store tokens in local storage or cookies accessible to JavaScript
HTTPS Only
Always use HTTPS to prevent token interception
Token Expiration
Implement token refresh logic before expiration
Minimal Scopes
Request only the minimum required scopes
Implementation Reference
The authentication middleware is implemented in two files:- JWT Validation:
src/middleware/authMiddleware.js- Validates Azure AD tokens - User Loading:
src/middleware/cargarUsuario.js- Loads employee data from database