Audience‑bridged Analytics via BFF (Admin & Dev brief)
What changed
- Added Analytics as a first‑class BFF‑proxied service. The BFF now mints/uses a service token for
analytics_serviceso SPA calls to/api/v1/analytics/*succeed under session auth.
Why
- Backends don’t accept browser cookies. The BFF validates the SPA session and forwards with a service‑scoped access token (audience‑bound to the target backend). Without a token for the target service, the BFF returns
401.
Files and ownership
-
BFF routing (unchanged; for reference)
ServiceConfigs/BFF/config/routes.yaml- Route id:
api-analyticsmapping/api/v1/analytics/*→analytics_service(session auth). - Owner: BFF platform team.
-
IdP configuration (audiences/scopes)
ServiceConfigs/IdP/config/settings.yaml- Added Analytics audience under scope mapping for
application.all.
- Added Analytics audience under scope mapping for
ServiceConfigs/IdP/config/clients.yamlbff-server.allowed_audiencesincludeshttps://analytics.ocg.labs.empowernow.ai.
- Owner: IdP platform team.
-
BFF settings
ms_bff_spike/config/settings.yaml- Ensured login scopes include
application.all.
- Ensured login scopes include
- Owner: BFF platform team.
-
BFF token mapping (temporary V1)
ms_bff_spike/ms_bff/src/api/v1/endpoints/auth.py- Extended service token map with:
analytics_service→ audiencehttps://analytics.ocg.labs.empowernow.ai- required scopes:
api.read | api.write | application.all
- Extended service token map with:
- Owner: BFF platform team (will move to config in V2).
Admin guide (rollout)
-
Pre‑reqs
- Confirm
routes.yamlhasapi-analyticsandtarget_service: analytics_service.
- Confirm
-
Change set
- IdP: add Analytics audience to
application.all, allow forbff-serverclient. - BFF: ensure
auth.oauth_scopesincludesapplication.all. - BFF: add
analytics_serviceto the service token mapping (V1).
- IdP: add Analytics audience to
-
Deploy
- Restart IdP, then restart BFF.
- Ask users to log out/in (new session) and hard refresh SPA.
-
Verify
- DevTools Network:
GET /api/v1/analytics/*returns 200.
- BFF logs:
- Look for
bff_service_tokens_mappedincludinganalytics_service. - Proxy lines show
token_found=Trueforservice=analytics_service.
- Look for
- DevTools Network:
-
Troubleshooting
401and logs showNO_TOKEN_AVAILABLE service=analytics_service:- The service wasn’t in the token map or IdP audience wasn’t permitted.
401withtoken_found=True:- Token audience mismatch; confirm audience exactly equals
https://analytics.ocg.labs.empowernow.ai.
- Token audience mismatch; confirm audience exactly equals
- Old session:
- Log out/in to refresh tokens after config changes.
-
Security notes
- Keep audiences least‑privileged. Prefer
api.readwhere possible;application.allis used here for compatibility and will be narrowed in V2. - BFF is the only component that holds/forwards service tokens.
- Keep audiences least‑privileged. Prefer
Dev guide (SPA teams)
-
Canonical paths (no change)
- Always call Analytics via BFF:
/api/v1/analytics/workflows/api/v1/analytics/authorization-metrics?service=crud/api/v1/analytics/anomalous-access-patterns?...
- Include credentials in fetch; our helper does this.
- Always call Analytics via BFF:
-
Expected responses
200with JSON on success;401means the BFF service token isn’t available (not a front‑end bug).
Snippets
- IdP scope → audience mapping (
settings.yaml)
token:
audience_mappings:
scope_mappings:
- scope: application.all
audiences:
- https://idp.ocg.labs.empowernow.ai/api/admin
- https://idp.ocg.labs.empowernow.ai/api/v1
- https://crud.ocg.labs.empowernow.ai
- empowernow
- https://analytics.ocg.labs.empowernow.ai
- IdP
bff-serverallowed audiences (clients.yaml)
bff-server:
allowed_audiences:
- empowernow
- https://analytics.ocg.labs.empowernow.ai
- BFF login scopes (
settings.yaml)
auth:
oauth_scopes: "openid profile email application.all offline_access"
- BFF service token map (V1,
auth.py)
"analytics_service": {
"audience": "https://analytics.ocg.labs.empowernow.ai",
"scopes": ["api.read", "api.write", "application.all"]
}
See also
services/bff/how-to/session-to-service-token-bridging