TL;DR: A purpose-built identity platform can handle sign-in, session handling, token issuance, and key rotation for a server while protecting endpoints with a sealed cookie session and FastAPI dependency injection, according to WorkOS. The practical lesson is that application teams should stop treating auth as custom code and govern it as an identity control plane boundary.
At a glance
What this is: NHI Mgmt Group's analysis, drawing on WorkOS's blog post: Delegated identity can protect API endpoints with hosted sign-in, sealed sessions, and dependency-based request checks.
Why it matters: It matters because application teams need to treat authentication as an identity control boundary, not as bespoke code that expands risk with every new endpoint.
Context
FastAPI auth is often fragile when teams build it directly into application code. Once an API exposes protected endpoints, the real problem is not just sign-in but the lifecycle around sessions, cookies, token validation, and key rotation.
This article frames delegated identity as the cleaner control boundary for a FastAPI server. Rather than scattering auth logic across handlers, the application asks an identity service to handle session creation and verification while the API focuses on business logic.
For identity teams, the issue is governance as much as implementation. The pattern applies to human sign-in today, but it also hints at the same boundary decisions teams will face when APIs later serve machine clients or AI agents.
Key questions
Q: How should teams structure FastAPI authentication so it does not spread across route handlers?
A: Use a single identity boundary for sign-in and session validation, then make protected routes depend on one reusable auth check. That keeps the application focused on business logic, makes review simpler, and reduces the chance that one endpoint drifts from the intended policy.
Q: Why do custom auth implementations create more risk in API applications?
A: Custom auth creates more risk because session handling, token validation, cookie protection, and key rotation all become separate failure points inside application code. Each one must be maintained, reviewed, and tested over time, which increases the chance of inconsistent enforcement and hidden auth debt.
Q: What are the signs that API authentication has become too embedded in application logic?
A: The warning signs are duplicated login checks, inconsistent expiry handling, different cookie or token behaviours across endpoints, and auth code that developers must update whenever session rules change. At that point, access control is no longer a clean control boundary.
Q: Should teams treat browser sessions and service-to-service access as the same control problem?
A: No. Browser sessions are user-facing and depend on hosted sign-in, cookie handling, and session validation, while service-to-service access usually needs machine credentials and different trust assumptions. The common requirement is governed identity, but the operational model should match the actor type.
Technical breakdown
Why custom FastAPI auth becomes hard to govern
A hand-rolled auth layer looks simple until it has to survive production conditions. JWT verification, JWKS rotation, secure cookie storage, session renewal, and logout semantics all create separate failure points, and each one becomes a governance obligation once the code ships. In FastAPI, the danger is not just broken login, but auth logic spread across route handlers, middleware, and utility code with no central lifecycle. Delegated identity moves those responsibilities into a defined session and token service so the app can verify identity at the boundary instead of reconstructing trust in every endpoint.
Practical implication: treat auth logic as a shared control plane concern, not per-endpoint application code.
How sealed sessions change API trust
A sealed session is an encrypted session payload stored in a browser cookie, which means the application can preserve state without exposing raw refresh-token material to the client. The FastAPI server later loads that sealed session and re-validates it on each request, so endpoint access depends on an authenticated session rather than a bearer token copied into application code. The important design shift is that the browser carries a sealed identity artefact, while the server remains responsible for deciding whether that artefact is still valid. That reduces token-handling complexity inside the API, but it also makes cookie protection and session validation non-negotiable.
Practical implication: protect the cookie boundary and make every protected route depend on session re-validation.
Why FastAPI dependency injection is the enforcement point
FastAPI dependencies let the framework run authentication checks before the route handler executes. In this pattern, Depends(get_current_user) turns identity verification into a reusable gate that either returns authenticated user context or stops the request with a 401 response. That is more than a coding convenience. It creates an explicit enforcement point where every protected endpoint inherits the same auth policy, which is much easier to review than duplicated login checks. The app’s business functions remain clean, but the dependency now carries governance responsibility for access decisions.
Practical implication: place identity enforcement in a single dependency path and reuse it across all protected endpoints.
NHI Mgmt Group analysis
Delegated identity is the right boundary when auth state becomes a lifecycle problem: FastAPI auth stops being a local code concern the moment teams have to manage token issuance, cookie security, logout, and key rotation together. Those are identity governance responsibilities, not just framework features. The more endpoints you add, the more dangerous custom auth becomes as a maintenance surface. The practitioner conclusion is to centralise session trust instead of distributing it across application code.
Auth logic in application handlers creates hidden identity debt: When sign-in, verification, and expiry checks are implemented ad hoc, teams inherit inconsistent enforcement and harder audits. That inconsistency matters more than the code volume itself, because identity controls lose reviewability when they are duplicated. This article reinforces a familiar governance pattern: once auth is embedded in business code, access control becomes harder to prove, not just harder to maintain. The practitioner conclusion is to keep authentication decisions at a single boundary.
Human auth patterns now set the template for later machine-client access: The same server-side trust boundary that protects a browser session also prepares the API for later non-human consumers. Once teams understand session trust, token validation, and request gating in FastAPI, they are better positioned to extend the model to service calls or agent-driven access without rewriting the core business layer. The practitioner conclusion is to design the auth boundary for future delegation, not just today’s user login.
Delegated identity reduces code risk, but it increases governance clarity only if the session model is explicit: A hosted sign-in flow does not remove accountability, it relocates it. Teams still need to know where the session originates, how it is stored, how it is validated, and when it is revoked. That makes the control model easier to reason about than custom auth, but only if the organisation treats the identity layer as part of the architecture review. The practitioner conclusion is to document the session lifecycle as a governed dependency.
Session-based API protection belongs in the same conversation as workload and NHI access design: Today this tutorial covers human login, but the underlying issue is who or what is allowed to call the API and under what trust conditions. As organisations add machine clients, AI agents, and delegated workflows, the same pattern of centralised identity decisions becomes the baseline. The practitioner conclusion is to align application authentication with broader identity governance before the access model fragments.
What this signals
FastAPI teams should read this pattern as a governance decision, not just a coding shortcut. Once authentication is delegated, the architecture becomes easier to review because the trust boundary is explicit, the session lifecycle is centralised, and protected routes inherit the same enforcement path.
Delegated identity boundary: the key design choice is deciding where authentication ends and application logic begins. That boundary matters more as APIs grow into broader service ecosystems, because identity drift usually starts when auth is rebuilt differently in every service.
This pattern also helps teams prepare for the next access model. The same separation between identity and business logic can support human login today and later extend to service or agent access without turning the API into a custom auth stack.
For practitioners
- Centralise authentication at the identity boundary Move sign-in, session handling, token issuance, and key rotation out of FastAPI handlers so the application only verifies a trusted session at request time.
- Use a single dependency for protected routes Apply one request dependency such as Depends(get_current_user) to every protected endpoint so enforcement stays consistent and reviewable.
- Harden the browser session cookie Keep the session cookie HttpOnly, secure in production, and backed by sealed session encryption so the browser never handles raw refresh-token material.
- Separate public health checks from authenticated APIs Keep endpoints such as /health unauthenticated for uptime checks while making business endpoints require validated identity context.
Key takeaways
- Custom authentication in FastAPI is risky because session management, token validation, cookie handling, and key rotation all become application-maintained control points.
- Delegated identity makes the auth boundary easier to review by pushing enforcement into a reusable session check instead of scattering it across endpoints.
- The main architectural lesson is to separate identity governance from business logic so the API can scale without accumulating auth debt.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Non-Human Identity Top 10 addresses the attack and risk surface, while NIST CSF 2.0 and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-04 — Insecure Authentication | The article centres on replacing custom API auth with delegated session validation. |
| NHI-07 — Long-Lived Secrets | Cookie-stored session material and token lifecycle management are central to the tutorial. | |
| NHI-10 — Human Use of NHI | The pattern shows a human browser session depending on non-human session infrastructure. | |
| Recommendation — Move authentication checks to a governed identity layer and verify sessions at the API boundary. Minimise secret lifetime in session flows and rotate credentials used to seal browser sessions. Separate human-facing login from the non-human session mechanisms that enforce access. | ||
| NIST CSF 2.0 | PR.AA-05 — Access Permissions, Entitlements and Authorizations | Protected endpoints depend on explicit request-time authorisation checks. |
| Recommendation — Apply PR.AA-05 so every protected FastAPI route evaluates access before execution. | ||
| NIST SP 800-53 Rev 5 | IA-5 — Authenticator Management | The article discusses token issuance, key rotation, and session lifecycle control. |
| Recommendation — Use IA-5 to govern authenticator issuance, rotation, and revocation for API sessions. | ||
Key terms
- Delegated Identity: Delegated identity is when one actor acts on behalf of another with explicit permission and bounded authority. In AI-assisted commerce, it requires clear consent, limited scope, and traceable records so the retailer can distinguish authorised delegation from unauthorised automation.
- Sealed Session: A sealed session is encrypted session state stored by the application in a browser cookie in a form that cannot be read or altered without the server-side secret. It preserves user continuity while reducing exposure of raw session material, but it still depends on correct validation, rotation, and expiry handling.
- FastAPI Dependency Injection: A FastAPI mechanism that runs a reusable function before a route handler and passes its result into the endpoint. For authentication, it creates a consistent enforcement point so protected endpoints fail closed before business logic executes.
- Authenticator Lifecycle Management: Authenticator lifecycle management is the governance of a credential from issuance to renewal, replacement, and retirement. For human identity programmes, it ensures that keys, smart cards, and certificates stay tied to the right user and are removed when the user, role, or device is no longer trusted.
Deepen your knowledge
NHI governance, agentic AI identity, and machine identity lifecycle are core topics in our NHI Foundation Level course, the industry's only accredited NHI security programme. If you are responsible for identity security strategy or NHI governance in your organisation, it is worth exploring.
Published by the NHIMG editorial team on June 6, 2026.
Updated on October 7, 2026.
NHI Mgmt Group, the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org