Skip to main content

OAuth2 bearer validation

OAuth2 bearer validation lets applications call the gateway with an OAuth2 access token that is not a JWT. Many identity providers issue opaque tokens, random strings that mean nothing on their own. The gateway asks the provider whether the token is valid through the token introspection endpoint defined in RFC 7662, reads who the token belongs to from the answer, and runs the request as that user and team. A second mode, OAuth2 proxy auth, trusts identity headers set by a reverse proxy that has already signed the user in.

Both modes are part of Spanway Enterprise. Without a license the gateway answers 403 to a request on either path and virtual keys keep working. Tokens that are JWTs are better served by JWT authentication, which checks the signature locally and never calls out per request.

A real-world scenario​

A company's internal portal signs users in with an identity provider that issues opaque access tokens. The portal's chat feature should call the gateway as the signed-in user, so each person's spend shows up under their own name and team, without the portal holding a Spanway key per user. The admin turns on OAuth2 bearer validation, points it at the provider's introspection endpoint, and the portal forwards each user's access token as the bearer token.

Steps​

  1. Create a confidential client at your identity provider that is allowed to call the token introspection endpoint. Note its client id and secret.

  2. Turn on OAuth2 auth in config.yaml.

    general_settings:
    enable_oauth2_auth: true
  3. Tell the gateway where to introspect tokens and which fields of the answer name the user, role and team, then restart it.

    export OAUTH_TOKEN_INFO_ENDPOINT="https://login.example.com/oauth2/introspect"
    export OAUTH_CLIENT_ID="spanway-gateway"
    export OAUTH_CLIENT_SECRET="..."
    export OAUTH_USER_ID_FIELD_NAME="sub"
    export OAUTH_USER_ROLE_FIELD_NAME="role"
    export OAUTH_USER_TEAM_ID_FIELD_NAME="team_id"
  4. Create the team the introspection answer names. In the Admin UI open Teams and choose Create New Team with that team id, or call POST /team/new.

  5. Call an inference route with a user's access token.

    curl -s http://localhost:4000/v1/chat/completions \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"model": "claude-sonnet", "messages": [{"role": "user", "content": "Draft a status update."}]}'

Expected result​

The gateway posts the token to the introspection endpoint with the client credentials as HTTP Basic authentication, receives {"active": true, "sub": "...", "team_id": "..."}, and the completion succeeds. In Logs the request is attributed to the user named by sub and the team named by team_id, and spend accrues to that team. A token the provider reports as inactive, expired or unknown is refused with 401.

Which routes use it​

With enable_oauth2_auth: true, OAuth2 validation applies to the inference routes (chat completions, embeddings, images, audio and the other llm_api_routes), the info routes and /lens. Management routes such as /key/generate or /team/new keep using virtual keys, so an introspected token can never manage the gateway. A bearer token that parses as a JWT is handled by JWT auth when that is enabled, unless a spanway_jwtauth.routing_overrides rule matches its iss, client_id, scope or aud and sends it through the OAuth2 path on purpose.

The gateway picks the request shape from the endpoint. When OAUTH_TOKEN_INFO_ENDPOINT contains introspect and both client credentials are set, it posts token=<token> as a form with Basic authentication, the RFC 7662 shape, and refuses the token when the answer has "active": false. Otherwise it sends GET with the token as a bearer header, the shape of a plain token info endpoint, and accepts any 2xx answer. With only OAUTH_CLIENT_ID set, the client id is sent in the form body for a public client.

The field named by OAUTH_USER_ROLE_FIELD_NAME sets the caller's gateway role. The accepted values are the gateway roles, proxy_admin, proxy_admin_viewer, internal_user, internal_user_viewer and the other roles on the roles page, and any other value, or a value in another case, gives no role. A role from introspection only matters on the inference, info and Lens routes listed above, so proxy_admin there lets the caller read admin views such as Lens but never manage keys, teams or settings. The gateway never writes the token or the introspection answer to its logs, at any log level.

OAuth2 proxy auth​

When a reverse proxy in front of the gateway already signs users in and adds headers such as X-Forwarded-User, the gateway can read identity from those headers instead of calling anyone.

general_settings:
enable_oauth2_proxy_auth: true
trusted_proxy_ranges: ["10.0.0.0/8"]
oauth2_config_mappings:
user_id: X-Forwarded-User
user_email: X-Forwarded-Email
team_id: X-Forwarded-Groups

Only six fields may be mapped: user_id, user_email, team_id, team_alias, org_id and models. A mapping to user_role, max_budget or any other field that grants privileges or sets limits is refused at request time, because a client could otherwise forge it with a header. Mapping models lets the proxy's header decide which models the caller may use, so map it only when your reverse proxy always sets or strips that header itself. The headers are read only when the direct peer of the connection is inside trusted_proxy_ranges, so a client that reaches the gateway directly cannot set them.

Errors​

What you seeWhyFix
403 Oauth2 token validation is only available for premium users. ... or 403 OAuth2 proxy auth is only available for premium users. ...The gateway has no enterprise license.Set the license key, see enterprise, or turn the setting off.
401 with OAUTH_TOKEN_INFO_ENDPOINT environment variable is not set.enable_oauth2_auth is on but the endpoint is missing.Export OAUTH_TOKEN_INFO_ENDPOINT and restart.
401 Authentication Error, Token is not activeThe provider reported "active": false, the token expired or was revoked.Have the client get a fresh token.
401 Authentication Error, Oauth 2.0 Token validation failed: <reason> naming a 401 from the provider.The introspection client credentials are wrong or the client is not allowed to introspect.Check OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET at the provider.
401 Authentication Error, An error occurred during token validation: <reason>The gateway could not reach the provider, or its answer was not JSON.Check network egress from the gateway to the endpoint. The token is never cached, so every request needs the provider.
Management routes refuse the token.OAuth2 validation only covers inference, info and Lens routes.Use a virtual key or a JWT with an admin scope for management routes.
Oauth2 proxy auth refuses to map non-identity UserAPIKeyAuth fieldsoauth2_config_mappings maps a header to a privilege or limit field.Keep only identity fields in the mapping and set roles, budgets and models on the user or team instead.
Every request with proxy auth is refused as untrusted.The direct peer is outside trusted_proxy_ranges.Add your reverse proxy's address range to trusted_proxy_ranges.

Logs​

LevelLineMeaningAction
DEBUGOauth2 token validation for token=[set=True]A request went to the introspection path. The token itself is never logged.None, it is for tracing with --detailed_debug.
DEBUGOauth2 token validation answered active=TrueThe provider's answer arrived. Only the active flag is logged.None.