nat.runtime.user_manager#
Runtime credential resolver that auto-detects identity source and creates UserInfo.
Attributes#
Classes#
Stateless resolver that creates |
Module Contents#
- logger#
- class UserManager#
Stateless resolver that creates
UserInfofrom HTTP/WebSocket connections.- classmethod extract_user_from_connection(
- connection: starlette.requests.Request | fastapi.WebSocket,
Resolve an HTTP/WebSocket connection into a
UserInfo.- Args:
connection: The incoming Starlette
RequestorWebSocket.- Returns:
A fully populated
UserInfo, orNoneif no credential is present on the connection.- Raises:
- ValueError: If a credential is found but cannot be resolved
to a valid user identity.
- classmethod _resolve_from_auth_header(
- auth_header: str,
Parse an
Authorizationheader and resolve identity by scheme.- Args:
auth_header: Raw header value (e.g.
Bearer <token>orBasic <b64>).- Returns:
A
UserInfoif the header contains a recognised scheme with a non-empty credential, orNoneif the header is malformed or uses an unsupported scheme.- Raises:
- ValueError: If a credential is present but cannot be decoded
(e.g. invalid JWT structure, malformed base64).
- static _from_auth_payload(
- payload: nat.data_models.api_server.AuthPayload,
Resolve a
UserInfofrom a WebSocket auth message payload.This is an identity resolver, not an authenticator. JWTs are decoded with
verify_signature=Falseto extract identity claims; API keys and basic credentials are mapped directly. Clients should verify and authenticate credentials (e.g. via JWKS, OAuth flows, or other auth middleware) before sending them over a WebSocket auth message.- Args:
payload: Discriminated union of JWT, API key, or basic auth credentials.
- Returns:
A
UserInfowith a deterministic user ID.- Raises:
ValueError: If the payload cannot be resolved to a valid user identity.
- static _get_session_cookie(
- connection: starlette.requests.Request | fastapi.WebSocket,
Extract the
nat-sessioncookie value from a Request or WebSocket.
- static _get_api_key_header(
- connection: starlette.requests.Request | fastapi.WebSocket,
Extract the
X-API-Keyheader value from a connection.
- static _get_auth_header(
- connection: starlette.requests.Request | fastapi.WebSocket,
Extract the raw
Authorizationheader value from a connection.
- static _user_info_from_session_cookie(
- cookie_value: str,
Build a
UserInfofrom a session cookie value.
- static _user_info_from_jwt( ) nat.data_models.user_info.UserInfo#
Build a
UserInfofrom decoded JWT claims.Registered claims (
sub,iss,aud,exp,iat) follow RFC 7519. Identity claims (email,preferred_username,name) follow OpenID Connect Core 1.0 Section 5.1.subis preferred as the stable identifier per RFC 7519 Section 4.1.2.- Raises:
ValueError: If the JWT contains no usable identity claim.
- static _user_info_from_basic_auth(
- b64_credential: str,
Build a
UserInfofrom a base64-encoded Basic Auth credential.- Args:
b64_credential: The base64-encoded
username:passwordstring.- Raises:
ValueError: If the credential cannot be decoded or is malformed.