Authorize Callback¶
Parse OAuth 2.0 / OIDC authorization callback redirect URIs and validate the state parameter for CSRF protection.
These functions are pure (no I/O) and available identically from both sync and async modules.
Response Model¶
AuthorizeCallbackResponse(is_successful, raw, values, code=None, access_token=None, identity_token=None, token_type=None, expires_in=None, scope=None, refresh_token=None, state=None, session_state=None, issuer=None, error=None, error_description=None)
dataclass
¶
Bases: _GuardedResponseMixin
Parsed OAuth 2.0 / OIDC authorization callback response.
Fields like code and access_token are guarded: accessing them
raises FailedResponseAccessError when is_successful is
False. The state field is not guarded, per RFC 6749 Section
4.1.2.1 which requires state in error responses so callers can
correlate errors with the original request.
.. warning::
The raw field contains the full redirect URI and values
contains all parsed parameters. For implicit/hybrid flows these
may include access_token in cleartext. Avoid logging,
serializing, or persisting these fields in production.
__repr__()
¶
Redact sensitive fields to prevent token leakage in logs.
Parsing¶
parse_authorize_callback_response(redirect_uri)
¶
Parse an OAuth 2.0 / OIDC authorization callback redirect URI.
Extracts parameters from the URL fragment (implicit / hybrid flows) or query string (authorization code flow). Fragment takes precedence when both are present, matching standard browser behavior where the authorization server places parameters in one location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redirect_uri
|
str
|
The full callback URL received from the authorization
server (e.g. |
required |
Returns:
| Type | Description |
|---|---|
AuthorizeCallbackResponse
|
An |
AuthorizeCallbackResponse
|
URL contains an |
Raises:
| Type | Description |
|---|---|
AuthorizeCallbackException
|
If redirect_uri is |
State Validation¶
AuthorizeCallbackValidationResult
¶
Bases: Enum
Outcome of authorization callback state validation.
StateValidationResult(is_valid, result, error=None, error_description=None)
dataclass
¶
Result of validating the state parameter in an authorization callback.
validate_authorize_callback_state(response, expected_state)
¶
Validate the state parameter from an authorization callback.
Checks are performed in priority order:
- Error response — if the authorization server returned an error,
validation fails with
ERROR_RESPONSE. - Missing state — if the callback or the caller-supplied
expected_state is
Noneor an empty string, validation fails withMISSING_STATE. Non-string types are also treated as missing. - State mismatch — the received state is compared to
expected_state using
hmac.compare_digest(constant-time) to avoid timing side-channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
AuthorizeCallbackResponse
|
A parsed |
required |
expected_state
|
str | None
|
The |
required |
Returns:
| Type | Description |
|---|---|
StateValidationResult
|
A |