Oauth¶
oauth
¶
OAuth security utilities.
Provides cryptographic functions for OAuth flows including state tokens, PKCE implementation, and authorization URL building.
Functions¶
generate_state_token
¶
Generate a cryptographically secure state token for CSRF protection.
Uses same pattern as refresh tokens.
Returns:
| Type | Description |
|---|---|
str
|
URL-safe random token string |
hash_state_token
¶
Hash state token before storing.
Uses SHA-256 hashing like other tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token |
str
|
Raw state token |
required |
Returns:
| Type | Description |
|---|---|
str
|
Hexadecimal hash of the token |
Source code in fastauth/security/oauth.py
hash_oauth_token
¶
Hash OAuth provider tokens before storing.
We store provider access/refresh tokens hashed for security.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token |
str
|
Raw OAuth token from provider |
required |
Returns:
| Type | Description |
|---|---|
str
|
Hexadecimal hash of the token |
Source code in fastauth/security/oauth.py
generate_code_verifier
¶
Generate a cryptographically random code verifier for PKCE.
Per RFC 7636, the code verifier should be 43-128 characters. We use 64 bytes for good entropy (~86 characters base64url encoded).
Returns:
| Type | Description |
|---|---|
str
|
URL-safe random string for PKCE code verifier |
Source code in fastauth/security/oauth.py
generate_code_challenge
¶
Generate S256 code challenge from verifier.
Challenge = BASE64URL(SHA256(verifier))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verifier |
str
|
Code verifier string |
required |
Returns:
| Type | Description |
|---|---|
str
|
Base64url-encoded SHA-256 hash of the verifier (without padding) |
Source code in fastauth/security/oauth.py
build_authorization_url
¶
build_authorization_url(
*,
auth_endpoint: str,
client_id: str,
redirect_uri: str,
state: str,
scope: str,
code_challenge: str | None = None
) -> str
Build OAuth authorization URL with PKCE support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_endpoint |
str
|
Provider's authorization endpoint URL |
required |
client_id |
str
|
OAuth client ID |
required |
redirect_uri |
str
|
Callback URL after authorization |
required |
state |
str
|
State token for CSRF protection |
required |
scope |
str
|
Space-separated list of OAuth scopes |
required |
code_challenge |
str | None
|
Optional PKCE code challenge |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Complete authorization URL with query parameters |