Authentication and single sign-on
Authentication gives people and automated clients a trusted way to enter nao and use the right organization and project. It supports browser sessions, organization API keys, OIDC and Microsoft Entra sign-in, and a browser-assisted CLI login while keeping identity setup tied to the deployment mode.
Authentication model
The authentication system has one central identity service and several ways to present its result. Browser users receive a session, API clients use an organization key, and the CLI receives a bearer session token after a browser approval. Protected requests resolve one of these credentials into a user and, where possible, a project context.
The main credential types have different ownership and scope:
| Credential | Main use | Scope and control |
|---|---|---|
| Browser session | Web application use | Represents a signed-in user and is checked on each protected request |
| CLI session token | Terminal access | Represents the user who approved the CLI login |
| Organization API key | Automation and integrations | Represents the key creator inside one organization and selects a project from request context |
| OIDC or Microsoft ID token | Identity-provider sign-in | Proves the external identity and can supply group data for access decisions |
The request layer accepts a browser session by default. Selected API routes also accept an organization key and then act as its creator. This distinction keeps a key from becoming a general user credential while still letting automation use the organization’s projects.
The parent page, Access and governance, describes the wider rules for membership and control. Organizations, projects, and entitlements explains how the authenticated identity is placed into those scopes.
Login flows
Browser login
The login page in apps/…/login.tsx presents the methods that the server has configured. Email and password login can be enabled or disabled, while provider buttons and cloud SSO appear only when their settings allow them. The page also carries a safe return path so a sign-in can continue an OAuth request or return the user to the intended application page.
Email login can require email verification in cloud mode when mail delivery is active. A failed sign-in for an unverified address moves the user into a verification flow instead of creating an authenticated session. After password acceptance, accounts with two-factor protection must complete an email or authenticator challenge before the page creates the normal post-login redirect. Social and SSO sign-ins return to the requested safe page after the identity provider completes its work.
The browser flow is coordinated by apps/…/auth.ts, which passes authentication requests to the shared auth service. The service creates the session only after it accepts the credentials and applies account and deployment rules.
The following diagram shows how the browser turns a local or external identity into an application session.
flowchart LR
U[User] --> L[Login page]
L --> E[Email and password]
L --> P[Configured identity provider]
E --> V[Verification or two-factor check]
P --> A[Authentication service]
V --> A
A --> C[Account and deployment checks]
C --> S[Browser session]
S --> R[Protected application requests]
References: apps/…/login.tsx• L22-110 apps/…/login.tsx• L115-167 apps/…/login.tsx• L220-250 apps/…/auth-form.tsx• L44-140 apps/…/auth.ts• L15-44 apps/…/auth.ts• L139-145 apps/…/auth.ts• L243-314
Cloud and self-hosted login
Cloud and self-hosted deployments share the login surface but differ in how identity connects to an organization. Cloud uses deployment-level provider settings and can place a Google user into an organization by email domain. A user with no matching domain receives a personal organization.
Self-hosted deployments can use organization-level Google settings and initialize the first user’s default organization. The first-user setup can close open email sign-up after that account exists. SSO users join the default organization when it exists, while other configured social users can join its default project.
These rules make the first successful login meaningful: it does not only prove identity. It also creates or selects the initial workspace in which the user can work.
References: apps/…/auth.ts• L147-161 apps/…/auth.ts• L373-405 apps/…/auth-user-onboarding.service.ts• L5-21 apps/…/auth-config.routes.ts• L43-89
Sessions and API keys
Session requests
The session middleware converts the request credential into a user and session. It rejects missing or invalid credentials before the route runs, blocks new sessions for deactivated users, and resolves the requested project for the authenticated user.
An SSO session can have a configured maximum age. When that age is reached, the next session check removes the session for users who signed in through the configured OIDC provider. This makes a later browser request require a fresh identity-provider sign-in rather than silently extending an old SSO session.
The session rotation endpoint swaps a valid session token for a new token without changing the browser session lifetime. Only one concurrent rotation can replace a given token. The server returns the new token and refreshes the browser cookie, so a token shared with another client can be invalidated while the browser remains signed in.
References: apps/…/auth.ts• L19-30 apps/…/auth.ts• L81-91 apps/…/auth.ts• L269-283 apps/…/auth.ts• L427-439 apps/…/sso-session.service.ts• L10-20 apps/…/session-rotation-auth.service.ts• L6-29
Organization API keys
Organization API keys are for non-browser access. An organization admin creates, lists, and revokes them. The system returns the plaintext key at creation time, stores only its hash for later checks, and records when a valid key was last used.
When a request presents a key with the nao_ prefix, nao resolves the key to its organization and creator. The request can name a project with x-nao-project-name; otherwise it can use a project selected by the normal project header only when that project belongs to the key’s organization. An invalid key or a deactivated creator receives an unauthorized response.
References: apps/…/api-key.routes.ts• L8-52 apps/…/api-key.service.ts• L20-51 apps/…/auth.ts• L32-63 apps/…/api-key.queries.ts• L7-26
SSO integrations
OIDC and Microsoft Entra
Single sign-on is an enterprise identity path for work accounts. OIDC uses the provider’s discovery metadata and supports common providers such as Okta, Auth0, and Keycloak. Microsoft Entra uses the configured tenant and Microsoft account provider. Both paths appear on the login page only when the feature and provider settings are active.
The OIDC flow starts from the SSO entry point or the login button, redirects the user to the identity provider, and returns the provider identity to the shared authentication service. PKCE is enabled by default for OIDC. Microsoft sign-in can also check group membership before it accepts the account.
References: apps/…/oidc-auth.service.ts• L11-64 apps/…/microsoft-auth.service.ts• L28-47 apps/…/sso.ts• L12-47 apps/…/auth-client.ts• L49-55 apps/…/auth-oidc-button.tsx• L22-49
Group access and role mapping
An identity provider group claim can control access and organization role. The configured mapping turns provider groups into nao roles such as admin, user, or viewer. OIDC denies a sign-in when group-based access is active and no allowed group matches. After a successful sign-in, group membership and mapped roles are refreshed so a provider-side change applies on the next login.
The mapping is provider-owned when active. nao does not demote the last organization admin, even when a new group result would remove that role. Administrators can inspect the last stored OIDC claims, the group claim used, matching groups, and the resolved role to diagnose a mapping problem.
References: apps/…/oidc-auth.service.ts• L35-53 apps/…/microsoft-auth.service.ts• L49-106 apps/…/sso-login-sync.service.ts• L7-36 apps/…/sso-group-mapping.service.ts• L21-49 apps/…/sso-group-mapping.service.ts• L129-180 apps/…/sso-token-inspector.tsx• L24-109
Token handling
Identity tokens and claims
OIDC and Microsoft ID tokens carry the claims that identify the user and may carry group membership. nao can decode stored claims for administrator diagnosis, but it verifies issuer, audience, signature, and time claims before using them for access or role decisions. Provider discovery and signing keys may be unavailable; that state is kept separate from a token that is simply invalid.
The stored token is therefore both a sign-in record and an input to the next group synchronization. The token inspector shows what the provider sent at the last sign-in, while authorization uses verified claims rather than an unchecked display view.
References: apps/…/sso-token.service.ts• L29-99 apps/…/sso-group-mapping.service.ts• L49-90 apps/…/sso-group-mapping.service.ts• L129-170
OIDC provider access tokens
Some enterprise data sources need the signed-in user’s provider access token to run work as that user. When the SSO feature is active, nao returns a stored OIDC access token for the project’s provider if it remains valid for at least five minutes. Otherwise it uses the stored refresh token to obtain and save a new access token.
Refresh-token rotation is shared across concurrent requests and server replicas. If a provider rejects a refresh because another replica already used the rotating refresh token, nao waits for the saved replacement before treating the refresh as failed. This lets long-running warehouse work use the same refreshed identity without redeeming a rotating token twice.
References: apps/…/oidc-access-token.service.ts• L16-20 apps/…/oidc-access-token.service.ts• L48-68 apps/…/oidc-access-token.service.ts• L124-193 apps/…/warehouse-access-token.service.ts• L7-15
OAuth access tokens
nao also issues signed OAuth access tokens for supported resource flows. The resource server checks the token audience and issuer against nao’s local signing keys. This avoids a network call back to the public issuer and keeps token checks working in self-hosted deployments where the public URL may not resolve from the server.
Public MCP flows can use a public issuer while the browser remains on the application host. The authorization response and token issuer must stay aligned with the resource host so clients can complete discovery and verification. The MCP server and tools page describes the protected resource that uses this flow.
References: apps/…/auth.ts• L99-128 apps/…/auth.ts• L251-262 apps/…/verify-jwt.ts• L18-24 apps/…/auth.ts• L527-608
CLI authentication
The CLI uses a browser approval flow so it can use every web login method without collecting provider passwords in the terminal. nao login starts a local callback listener, opens /cli-login, and includes a random state value. The signed-in user approves or denies the request in the browser.
The browser may mint a one-time authorization code only from a trusted browser session. The CLI receives that code on its localhost callback and exchanges it for a session token. The code expires after five minutes and is deleted when exchanged. The CLI stores the token in ~/…/auth.json with user-only file permissions.
If a browser cannot open, the CLI can fall back to email and password. It stores the resulting session cookies instead. Later commands prefer the stored bearer token, then stored cookies, and can prompt for a new login when neither exists. nao logout tries to revoke the server session before removing local credentials.
The flow keeps approval and token exchange separate:
sequenceDiagram
participant C as CLI
participant B as Browser
participant N as nao server
participant K as Local callback
C->>K: Start callback listener
C->>B: Open /cli-login with port and state
B->>N: Sign in and approve
B->>N: Request one-time authorization code
N-->>B: Code
B->>K: Redirect with code and state
C->>N: Exchange code at /api/cli-auth/token
N-->>C: Session token
C->>N: Use bearer session token
References: cli/…/auth.py• L145-215 cli/…/auth.py• L274-336 cli/…/login.py• L14-60 apps/…/cli-login.tsx• L10-55 apps/…/cli-auth.ts• L12-52 apps/…/cli-auth.service.ts• L8-49
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the browser provider list, provider setup queries, and server-side trusted-provider list aligned. A mismatch can show a login option that cannot complete account linking.
- Keep API-key project selection tied to the key organization. Changes to project headers or membership lookup can silently widen an organization key’s reach.
- Keep session rotation’s endpoint, cookie update, token replacement, and conflict behavior aligned with clients that use a session token outside the browser.
- Keep the CLI callback parameters, state check, one-time code lifetime, and token exchange contract aligned across the CLI, browser route, and server route.
- Keep OIDC group claim names and role mappings aligned with the claims present in the ID token. A claim present only in userinfo cannot drive the current group check.
- Keep OIDC access-token refresh timing, refresh-token storage, and provider selection aligned with the warehouse flows that use the user’s identity.
- When changing SSO session age or token verification, preserve the distinction between an invalid token and a provider that is temporarily unavailable.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| SSO group mapping is active but the ID token has no configured group claim | OIDC access or role mapping cannot establish the required group result | Verify that the claim is in the ID token and use the token inspector |
| Provider signing keys or discovery cannot be reached | The token is treated as unavailable and role synchronization leaves existing roles unchanged | Restore provider discovery or key access, then sign in again |
| OIDC access token is near expiry and the refresh token is missing or rejected | A warehouse flow cannot act as the signed-in provider identity | Reconnect the provider account and grant a refresh token |
| Two clients rotate the same session token | One rotation returns a conflict and the old token no longer works | Use the token returned by the successful rotation |
| CLI callback state does not match | The callback is rejected and no code reaches the CLI | Start login again from the same terminal |
| CLI authorization code is reused or older than five minutes | Token exchange returns an invalid or expired-code error | Start a new browser login |
| API key is revoked, unknown, or owned by a deactivated user | The request receives unauthorized and cannot select a project | Create a new key for an active user |
| A role mapping would demote the last organization admin | The demotion is skipped and the current admin role remains | Add another organization admin before changing provider groups |
Key files
| File | Why to read it |
|---|---|
| apps/…/auth.ts | Defines the shared auth service, enabled providers, account linking, sessions, and onboarding hooks |
| apps/…/auth.ts | Shows how browser sessions and organization API keys become protected request context |
| apps/…/login.tsx | Shows the user-facing login decisions for provider selection, email verification, two-factor challenges, OAuth continuation, redirects, and deployment settings |
| apps/…/oidc-auth.service.ts | Defines OIDC availability, discovery-based sign-in, and group access checks |
| apps/…/oidc-access-token.service.ts | Defines provider access-token reuse, refresh, and rotating refresh-token coordination |
| apps/…/microsoft-auth.service.ts | Defines Microsoft Entra sign-in and group membership enforcement |
| apps/…/sso-group-mapping.service.ts | Explains provider group synchronization, role changes, and token inspection |
| apps/…/sso-token.service.ts | Defines the boundary between decoded claims, verified claims, and provider availability |
| apps/…/session-rotation-auth.service.ts | Defines in-place session-token rotation and its concurrency boundary |
| apps/…/cli-auth.ts | Defines the browser-to-CLI authorization-code contract |
| cli/…/auth.py | Shows browser login, fallback credentials, local storage, and CLI session use |
| apps/…/api-key.routes.ts | Defines organization-admin API-key management |