spakky-oauth¶
OAuth 2.0 client 플러그인 API입니다. Authorization code + PKCE, confidential client 인증, token 갱신과 폐기를 제공합니다.
spakky.plugins.oauth
¶
OAuth 2.0 client plugin identity.
PLUGIN_NAME = Plugin(name='spakky-oauth')
module-attribute
¶
Plugin identifier for the OAuth 2.0 client package.
spakky.plugins.oauth.main
¶
Plugin initialization for the OAuth 2.0 client.
initialize(app)
¶
Register the OAuth configuration and client service.
Token and authorization-state stores are application-owned: register one
IOAuthTokenStore and one IOAuthAuthorizationStateStore Pod.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/main.py
spakky.plugins.oauth.client
¶
OAuth 2.0 client: authorization code with PKCE, refresh, and revocation.
OAuthClient(config, token_store, state_store, clock=None)
¶
Obtain, refresh, and revoke tokens for application subjects.
Registrations may be public clients or confidential clients that
authenticate with client_secret_basic or client_secret_post.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
begin_authorization(client, subject, redirect_uri, *, client_id=None, parameters=None)
async
¶
Start an authorization-code flow and return the redirect target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
OAuthClientName
|
Name of the configured registration. |
required |
subject
|
OAuthSubject
|
Application identity the resulting tokens belong to. |
required |
redirect_uri
|
OAuthRedirectUri
|
Callback URL registered for this flow. |
required |
client_id
|
OAuthClientId | None
|
Overrides the registered client id, for servers that issue a client id per sign-in. |
None
|
parameters
|
Mapping[str, str] | None
|
Extra authorization parameters for this request only. |
None
|
Returns:
| Type | Description |
|---|---|
OAuthAuthorizationRequest
|
The authorization URL and the state value the callback must echo. |
Raises:
| Type | Description |
|---|---|
OAuthAuthorizationRequestError
|
A request parameter overrides a protocol field or the client id override is blank. |
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
complete_authorization(state, code, *, client_id=None)
async
¶
Exchange a callback code for tokens and store them for the subject.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
OAuthState
|
State value echoed by the authorization server. |
required |
code
|
OAuthAuthorizationCode
|
Authorization code from the callback. |
required |
client_id
|
OAuthClientId | None
|
Client id the server issued in the callback, when it differs from the one the flow started with. |
None
|
Returns:
| Type | Description |
|---|---|
OAuthAuthorizationResult
|
The stored tokens and the nonce an ID token must carry. |
Raises:
| Type | Description |
|---|---|
OAuthAuthorizationStateError
|
The state is unknown, used, or expired. |
OAuthAuthorizationRequestError
|
The client id override is blank. |
OAuthAuthorizationRequiredError
|
The server rejected the code. |
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
access_token(client, subject, *, rejected_token=None)
async
¶
Return a usable access token, refreshing it when needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
OAuthClientName
|
Name of the configured registration. |
required |
subject
|
OAuthSubject
|
Application identity that signed in. |
required |
rejected_token
|
OAuthAccessToken | None
|
A token the resource server just rejected; the stored token is refreshed only if it is still that token. |
None
|
Returns:
| Type | Description |
|---|---|
OAuthAccessToken
|
An access token valid beyond the configured refresh leeway. |
Raises:
| Type | Description |
|---|---|
OAuthAuthorizationRequiredError
|
The subject has no tokens, no
refresh token, or the refresh grant was rejected. Stored
tokens are kept; call |
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
revoke(client, subject)
async
¶
Remove the subject's tokens and revoke them at the server.
Local tokens are removed first, so the subject is signed out here even when the server cannot confirm revocation.
Raises:
| Type | Description |
|---|---|
OAuthRevocationError
|
The server was unreachable or did not confirm revocation. |
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
refresh_tokens(client, refresh_token, *, client_id=None)
async
¶
Run a refresh grant for a refresh token the caller stores itself.
The plugin's token store is neither read nor written, so an application that keeps tokens in its own storage can refresh them with the registration's client authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
OAuthClientName
|
Name of the configured registration. |
required |
refresh_token
|
SecretStr
|
The refresh token to exchange. |
required |
client_id
|
OAuthClientId | None
|
Client id the tokens were issued to, when it differs from the registered one. |
None
|
Returns:
| Type | Description |
|---|---|
OAuthTokenSet
|
The new token set. When the server does not rotate the refresh |
OAuthTokenSet
|
token, the given one is kept; when it reports no scope, the |
OAuthTokenSet
|
registration's scopes are used. |
Raises:
| Type | Description |
|---|---|
OAuthAuthorizationRequestError
|
The refresh token or client id override is blank. |
OAuthAuthorizationRequiredError
|
The server rejected the grant
( |
OAuthTokenEndpointError
|
The server rejected the request for any
other reason; it carries the HTTP status and |
OAuthTokenResponseError
|
The response is not a usable token set. |
OAuthTransportError
|
The server could not be reached. |
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
revoke_token(client, token, *, token_type_hint=None, client_id=None)
async
¶
Revoke a token the caller stores itself (RFC 7009).
The plugin's token store is neither read nor written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
OAuthClientName
|
Name of the configured registration. |
required |
token
|
SecretStr
|
The access or refresh token to revoke. |
required |
token_type_hint
|
OAuthTokenTypeHint | None
|
Which kind of token |
None
|
client_id
|
OAuthClientId | None
|
Client id the token was issued to, when it differs from the registered one. |
None
|
Raises:
| Type | Description |
|---|---|
OAuthAuthorizationRequestError
|
The token or client id override is blank. |
OAuthRevocationNotSupportedError
|
The registration has no revocation endpoint. |
OAuthRevocationError
|
The server was unreachable or did not
answer 200; it carries the HTTP status and |
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/client.py
spakky.plugins.oauth.config
¶
Configuration for operator-owned OAuth 2.0 client registrations.
OAuthClientAuthMethod
¶
Bases: StrEnum
How the client authenticates to the token and revocation endpoints.
Values follow the RFC 7591 token_endpoint_auth_method names.
NONE = 'none'
class-attribute
instance-attribute
¶
Public client: only client_id is sent (RFC 6749 section 2.1).
CLIENT_SECRET_BASIC = 'client_secret_basic'
class-attribute
instance-attribute
¶
HTTP Basic authentication with the client secret (RFC 6749 section 2.3.1).
CLIENT_SECRET_POST = 'client_secret_post'
class-attribute
instance-attribute
¶
client_id and client_secret in the form body (RFC 6749 section 2.3.1).
OAuthClientRegistration
¶
Bases: BaseModel
One client registration at an OAuth 2.0 authorization server.
A public client leaves token_endpoint_auth_method at none. A
confidential client sets it to client_secret_basic or
client_secret_post together with client_secret.
OAuthConfig()
¶
Bases: BaseSettings
Named OAuth 2.0 client registrations and token lifetime policy.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/config.py
clients = Field(default_factory=dict)
class-attribute
instance-attribute
¶
Registrations keyed by the name callers pass to OAuthClient.
http_timeout_seconds = Field(default=DEFAULT_OAUTH_HTTP_TIMEOUT_SECONDS, gt=0)
class-attribute
instance-attribute
¶
Timeout for each token and revocation endpoint request.
refresh_leeway_seconds = Field(default=DEFAULT_OAUTH_REFRESH_LEEWAY_SECONDS, ge=0)
class-attribute
instance-attribute
¶
Refresh an access token this long before it expires.
authorization_ttl_seconds = Field(default=DEFAULT_OAUTH_AUTHORIZATION_TTL_SECONDS, gt=0)
class-attribute
instance-attribute
¶
How long a started authorization waits for its callback.
spakky.plugins.oauth.token
¶
Value types exchanged with the OAuth 2.0 client and its stores.
OAuthTokenTypeHint
¶
Bases: StrEnum
RFC 7009 token_type_hint values for a revocation request.
OAuthTokenKey(client, subject)
dataclass
¶
Identify the tokens one application subject holds for one registration.
OAuthTokenSet
¶
Bases: BaseModel
Tokens issued to one subject; secrets stay wrapped until they are sent.
OAuthPendingAuthorization
¶
Bases: BaseModel
PKCE and binding data kept between the authorization redirect and callback.
OAuthAuthorizationRequest
¶
Bases: BaseModel
Redirect target that starts one authorization-code flow.
OAuthAuthorizationResult
¶
Bases: BaseModel
Tokens stored by a completed flow plus the nonce to check an ID token.
spakky.plugins.oauth.store
¶
Storage and clock ports for the OAuth 2.0 client, with in-memory defaults.
IOAuthTokenStore
¶
IOAuthAuthorizationStateStore
¶
IOAuthClock
¶
Bases: ABC
Replaceable wall clock for deterministic expiry decisions.
SystemOAuthClock
¶
InMemoryOAuthTokenStore()
¶
Bases: IOAuthTokenStore
Process-local token store for development and tests; tokens die on restart.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/store.py
InMemoryOAuthAuthorizationStateStore(clock=None)
¶
Bases: IOAuthAuthorizationStateStore
Process-local state store; callbacks must reach the process that started them.
Authorizations whose expires_at has passed are dropped on every
save and take, so sign-ins abandoned before their callback do not
accumulate for the life of the process.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/store.py
spakky.plugins.oauth.error
¶
Errors raised by the OAuth 2.0 client plugin.
AbstractSpakkyOAuthError
¶
OAuthClientNotConfiguredError
¶
Bases: AbstractSpakkyOAuthError
Raised when a caller names a client registration that does not exist.
OAuthAuthorizationRequestError
¶
Bases: AbstractSpakkyOAuthError
Raised when a caller's per-request authorization arguments are invalid.
OAuthAuthorizationStateError
¶
OAuthAuthorizationRequiredError
¶
Bases: AbstractSpakkyOAuthError
Raised when the subject must sign in again to obtain a usable token.
OAuthTokenEndpointError(status_code, error=None)
¶
Bases: AbstractSpakkyOAuthError
Raised when the token endpoint rejects a request for a non-grant reason.
Carries only the HTTP status and the RFC 6749 section 5.2 error code so
callers can tell configuration errors from transient server failures. The
response body and request secrets are never kept.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/error.py
OAuthTokenResponseError
¶
Bases: AbstractSpakkyOAuthError
Raised when a token endpoint response is not a usable bearer token set.
OAuthTransportError
¶
OAuthRevocationError(status_code=None, error=None)
¶
Bases: AbstractSpakkyOAuthError
Raised when the revocation endpoint did not confirm a revocation.
status_code is None when the server could not be reached; error is
None when the response carried no RFC 6749 section 5.2 error code.
Source code in plugins/spakky-oauth/src/spakky/plugins/oauth/error.py
OAuthRevocationNotSupportedError
¶
Bases: AbstractSpakkyOAuthError
Raised when a token is revoked through a registration without an endpoint.