nat.runtime.user_manager#
Runtime credential resolver that auto-detects identity source and creates UserInfo.
Attributes#
Exceptions#
Raised when a supplied identity credential method is disabled by policy. |
|
Raised when a configured trusted identity header is missing or ambiguous. |
|
Raised when an enabled JWT verification policy rejects a token. |
Classes#
Stateless resolver that creates |
Module Contents#
- logger#
- exception IdentityCredentialNotAcceptedError#
Bases:
ValueErrorRaised when a supplied identity credential method is disabled by policy.
Initialize self. See help(type(self)) for accurate signature.
- exception IdentityHeaderError#
Bases:
ValueErrorRaised when a configured trusted identity header is missing or ambiguous.
Initialize self. See help(type(self)) for accurate signature.
- exception JwtVerificationError#
Bases:
ValueErrorRaised when an enabled JWT verification policy rejects a token.
Initialize self. See help(type(self)) for accurate signature.
- class UserManager#
Stateless resolver that creates
UserInfofrom HTTP/WebSocket connections.- classmethod extract_user_from_connection(
- connection: starlette.requests.Request | fastapi.WebSocket,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None = None,
- identity_header: str | None = None,
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 extract_user_from_connection_with_verification(
- connection: starlette.requests.Request | fastapi.WebSocket,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None = None,
- jwt_validators: Mapping[str, nat.authentication.credential_validator.bearer_token_validator.BearerTokenValidator] | None = None,
- identity_header: str | None = None,
- Async:
- classmethod _resolve_from_auth_header_with_verification(
- auth_header: str,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None = None,
- jwt_validators: Mapping[str, nat.authentication.credential_validator.bearer_token_validator.BearerTokenValidator] | None = None,
- Async:
- classmethod _resolve_from_auth_header(
- auth_header: str,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None = None,
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,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None = None,
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 from_auth_payload_with_verification(
- payload: nat.data_models.api_server.AuthPayload,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None = None,
- jwt_validators: Mapping[str, nat.authentication.credential_validator.bearer_token_validator.BearerTokenValidator] | None = None,
- Async:
Resolve an auth payload, verifying JWTs when a verifier is configured.
- static _decode_verified_jwt(
- token: str,
- jwt_validators: Mapping[str, nat.authentication.credential_validator.bearer_token_validator.BearerTokenValidator] | None,
- Async:
- static _ensure_identity_credential_accepted(
- credential_type: nat.data_models.api_server.IdentityCredentialType,
- accepted_identity_credentials: Collection[nat.data_models.api_server.IdentityCredentialType] | None,
Reject a recognized credential type when it is disabled by policy.
- static _get_identity_header(
- connection: starlette.requests.Request | fastapi.WebSocket,
- header_name: str,
Read one non-empty value for a trusted identity header, failing closed otherwise.
- 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.