auth¶
The credential vocabulary: what a prover authenticates with, whether it is present on
this host, and how long it stays valid. Every prover reports one
AuthStatus from
auth_status().
- class open_atp.auth.AuthStatus(kind: AuthKind, source: str, present: bool, expires_at: datetime | None = None, refreshable: bool = False, remedy: str = '')[source]¶
The state of a prover’s credential, as read from the host.
Reported by
auth_status(). Reading the host never validates the credential against its provider; a present, unexpired credential can still be revoked or simply wrong.- Parameters:
- kind
AuthKind Whether the prover authenticates with an API key or an OAuth token.
- source
str Where the credential is read from: an environment variable name, or the path of the CLI credential file.
- presentbool
Whether the credential was found at
source.- expires_at
datetime.datetime, optional When the credential stops being valid.
None(the default) when it does not expire or exposes no expiry.- refreshablebool, default
False Whether the credential ships a refresh token, letting its CLI renew it on the host. Sandboxed runs cannot refresh it themselves.
- remedy
str, optional How to obtain the credential (e.g., command or web page). Defaults to
source.
- kind
- classmethod from_env(env_name: str, explicit: str | None = None, kind: AuthKind = AuthKind.API_KEY, remedy: str = '') AuthStatus[source]¶
Read a credential held in an environment variable.
- Parameters:
- Returns:
AuthStatusA status that never expires; an environment variable carries no expiry.
Examples
An unset variable reads as missing:
>>> from open_atp.auth import AuthStatus >>> AuthStatus.from_env("NOT_A_REAL_API_KEY").state() <AuthState.MISSING: 'missing'>
- time_remaining(now: datetime | None = None) timedelta | None[source]¶
How long the credential stays valid, relative to
now.- Parameters:
- now
datetime.datetime, optional The instant to measure from; defaults to the current UTC time.
- now
- Returns:
datetime.timedeltaorNoneTime until expiry, negative once past it.
Nonewhen the credential is absent or exposes no expiry.
- state(now: datetime | None = None) AuthState[source]¶
Classify the credential as of
now.- Parameters:
- now
datetime.datetime, optional The instant to classify against; defaults to the current UTC time.
- now
- Returns:
AuthStateMISSINGif absent, else the position ofnowin the validity window. A credential with no expiry is alwaysOKwhen present.
Examples
A key read from the environment neither expires nor refreshes:
>>> from open_atp.auth import AuthKind, AuthStatus >>> status = AuthStatus(AuthKind.API_KEY, "OPENAI_API_KEY", present=True) >>> status.state() <AuthState.OK: 'ok'>
- open_atp.auth.EXPIRY_WARNING_THRESHOLD = datetime.timedelta(seconds=900)¶
Threshold until expiration for
AuthState.EXPIRINGstatus.