Authorize Callback¶
Parse OAuth 2.0 / OIDC authorization callback redirect URIs, validate the state parameter for CSRF protection, and validate the iss parameter (RFC 9207) to defend against authorization-server mix-up attacks.
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 / issuer 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 |
Issuer Validation (RFC 9207)¶
Validate the authorization-response issuer against the expected authorization
server to close the mix-up attack class. Drive enforcement from the discovery
metadata flag authorization_response_iss_parameter_supported (passed as
iss_parameter_supported=), or require it
strictly via require=True.
validate_authorize_callback_issuer(response, expected_issuer, *, iss_parameter_supported=False, require=False)
¶
Validate the iss parameter from an authorization callback (RFC 9207).
RFC 9207 adds an iss parameter to authorization responses so a client
can bind the response to the authorization server that issued it, closing
the "mix-up" attack class (RFC 9207 Section 1, Section 4) where a response
from a malicious/compromised AS is replayed at an honest AS's callback.
Checks are performed in priority order:
- Error response — if the authorization server returned an error,
validation fails with
ERROR_RESPONSE(mirrors state validation; there is no issuer to validate on an error). - Issuer present — when the callback carries
iss, it MUST match expected_issuer (RFC 9207 Section 2.4: "the client MUST validate"). This holds even when the metadata flag is unset — a presentissis always validated, never ignored. "Present" means the parameter was supplied at all: a present-but-emptyiss(...&iss=) is malformed and validated (and thus rejected), not folded into the "absent" branch. A missing/empty expected_issuer yieldsISSUER_MISMATCH(fail closed: we cannot confirm the response's origin). - Issuer absent — when the AS advertises support
(iss_parameter_supported) or the caller opts into strict mode
(require), an absent
issfails withMISSING_ISSUER(the server promised it / the caller demands it). Otherwise there is nothing to validate and the result isSUCCESS.
Unlike state, the issuer is not a secret, so a plain == comparison is
used — constant-time comparison would guard a timing side-channel that
does not exist here (contrast validate_authorize_callback_state).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
AuthorizeCallbackResponse
|
A parsed |
required |
expected_issuer
|
str | None
|
The |
required |
iss_parameter_supported
|
bool
|
Whether the AS advertises
|
False
|
require
|
bool
|
Strict opt-in. When |
False
|
Returns:
| Type | Description |
|---|---|
StateValidationResult
|
A |