Overview
The Portal Self-Service Backend API uses Azure Active Directory (Azure AD) for authentication via JWT tokens. This guide walks through the complete setup process.Prerequisites
- An Azure AD tenant
- Administrative access to Azure Portal
- Backend API deployed or running locally
Azure AD App Registration
Create App Registration
- Navigate to Azure Portal
- Go to Azure Active Directory > App registrations
- Click New registration
- Enter a name for your API (e.g., “Portal Self-Service Backend API”)
- Select Accounts in this organizational directory only
- Click Register
Configure API Permissions
- In your app registration, go to API permissions
- Add Microsoft Graph delegated permissions:
User.Reademailprofile
- Grant admin consent for your organization
Expose an API
- Go to Expose an API
- Click Add an Application ID URI
- Accept the default value (e.g.,
api://<client-id>) or set a custom URI - Click Save
The Application ID URI will be used as the
AZURE_AUDIENCE environment variable.Environment Variables
After registering your app in Azure AD, configure the following environment variables in your.env file:
.env
Replace
<tenant-id> with your Azure AD tenant ID and <client-id> with your application’s client ID.Finding Your Values
Tenant ID
Found in Azure Portal under Azure Active Directory > Overview > Tenant ID
Client ID
Found in your app registration under Overview > Application (client) ID
JWT Verification Process
The API uses theexpress-oauth2-jwt-bearer library to verify JWT tokens issued by Azure AD.
Authentication Middleware
The base JWT verification is configured insrc/middleware/authMiddleware.js:
src/middleware/authMiddleware.js
How It Works
Issuer Verification
Verifies the token was issued by Microsoft (
iss claim matches AZURE_ISSUER_BASE_URL).Audience Verification
Ensures the token was intended for this API (
aud claim matches AZURE_AUDIENCE).Testing Your Setup
1. Obtain a Token
Use your Azure AD client application to obtain a JWT token with the correct audience.2. Make an Authenticated Request
3. Verify Token Claims
Successful authentication will extract user information from the token claims:oid- Azure Object ID (unique user identifier)upnorpreferred_username- User emailname- User’s display name
Troubleshooting
401 - Token inválido, expirado o faltante
401 - Token inválido, expirado o faltante
Possible causes:
- Token is expired
- Token signature is invalid
- Token audience doesn’t match
AZURE_AUDIENCE - Token issuer doesn’t match
AZURE_ISSUER_BASE_URL
Token validation takes too long
Token validation takes too long
The first request fetches Azure’s public keys (JWKS). Subsequent requests use cached keys.This is normal behavior and improves performance over time.
User email not extracted
User email not extracted
Ensure your Azure AD token includes the
upn or preferred_username claim. Check your app’s token configuration in Azure Portal.Next Steps
JWT Token Structure
Learn about token claims and the authenticateAndExtract middleware
Roles & Permissions
Configure role-based access control for your API