Protect a DaloyJS API with Microsoft Entra ID (MSAL)
Microsoft Entra ID (formerly Azure Active Directory) is Microsoft's enterprise identity platform. For a backend API, the job is to verify the v2.0 access token in the Authorizationheader against the tenant's public JWKS. The jose library is the modern, runtime-portable choice for that. Use @azure/msal-node on top when your API needs to call another protected service (OAuth 2.0 on-behalf-of, client credentials, etc.).
- 01asyncClient appEntra IDUser signs in; Entra ID mints a v2.0 access token (RS256)aud = api://my-daloy-api
- 02requestClient appDaloyJS APICall API with Authorization: Bearer <access token>
- 03asyncDaloyJS APITenant JWKScreateRemoteJWKSet fetches signing keys (cached)GET /{tenantId}/discovery/v2.0/keys
- 04noteDaloyJS APIDaloyJS APIjwtVerify checks issuer, audience, scp / rolesiss = login.microsoftonline.com/{tenantId}/v2.0
- 05responseDaloyJS APIClient appReturn protected data after requireAuth passes
1. Register the API in Entra ID
- In the Microsoft Entra admin center, go to Entra ID → App registrations → New registration and register your API app.
- Under Expose an API, set an Application ID URI (e.g.
api://my-daloy-api) and add one or more scopes (e.g.access_as_user). - Register your client app separately and grant it permission to the scope above. Note the tenant IDand the API app's Application (client) ID.
- The OIDC discovery document lives at
https://login.microsoftonline.com/{tenantId}/v2.0/.well-known/openid-configurationand references the JWKS athttps://login.microsoftonline.com/{tenantId}/discovery/v2.0/keys.
2. Install
Add @azure/msal-node as well only if the API itself needs to acquire downstream tokens (see below).
3. Environment variables
4. Plugin
createRemoteJWKSet caches keys in memory and refreshes on a missing kid, so key rollover is handled automatically.
5. Guard a route
App roles vs delegated scopes: app-only tokens (client-credentials flow) put granted roles in roles with no scp claim, while user-delegated tokens put granted scopes in scp. Inspect both in requireAuth if you support both shapes.
Acquiring downstream tokens with MSAL Node
If your API needs to call Microsoft Graph or another protected service on behalf of the user, use MSAL Node's ConfidentialClientApplication:
Prefer certificates over client secretsin production, and store credentials in Azure Key Vault or your platform's secret manager.
Notes
- The
issuerfor v2.0 tokens ishttps://login.microsoftonline.com/{tenantId}/v2.0. Multi-tenant apps must validate thetidclaim against an allowlist rather than relying on the issuer alone. - Don't validate tokens you don't own. Microsoft Graph tokens may not be JWTs and aren't meant to be inspected by your app.
- Entra ID rotates signing keys regularly, never pin keys, always resolve them through the JWKS endpoint.
See also AWS Cognito, Auth0, and the auth integrations overview.