Oauth¶
oauth
¶
OAuth authentication core logic.
Provides business logic for OAuth flows including authorization URL generation, callback handling, and account linking.
Classes¶
OAuthError
¶
Bases: Exception
Base exception for OAuth errors.
OAuthStateError
¶
Bases: Exception
Raised when OAuth state token is invalid or expired.
OAuthAccountAlreadyLinkedError
¶
Bases: Exception
Raised when OAuth account is already linked to a different user.
OAuthProviderNotFoundError
¶
Bases: Exception
Raised when OAuth provider is not found.
Functions¶
initiate_oauth_flow
¶
initiate_oauth_flow(
*,
states: OAuthStateAdapter,
provider: OAuthProvider,
redirect_uri: str,
user_id: UUID | None = None,
use_pkce: bool = True,
state_ttl_minutes: int = 10
) -> tuple[str, str, str | None]
Initiate OAuth authorization flow.
Generates state token for CSRF protection and optional PKCE challenge. Stores state in database and builds authorization URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states |
OAuthStateAdapter
|
OAuth state adapter for database operations |
required |
provider |
OAuthProvider
|
OAuth provider instance |
required |
redirect_uri |
str
|
Callback URL after authorization |
required |
user_id |
UUID | None
|
Optional user ID for linking mode (logged-in user) |
None
|
use_pkce |
bool
|
Whether to use PKCE for enhanced security (default: True) |
True
|
state_ttl_minutes |
int
|
State token time-to-live in minutes (default: 10) |
10
|
Returns:
| Type | Description |
|---|---|
str
|
Tuple of (authorization_url, state_token, code_verifier) |
str
|
code_verifier is None if PKCE is disabled |
Example
auth_url, state, verifier = initiate_oauth_flow( ... states=state_adapter, ... provider=google_provider, ... redirect_uri="https://example.com/oauth/callback", ... use_pkce=True, ... )
Source code in fastauth/core/oauth.py
complete_oauth_flow
async
¶
complete_oauth_flow(
*,
states: OAuthStateAdapter,
oauth_accounts: OAuthAccountAdapter,
users: UserAdapter,
provider: OAuthProvider,
code: str,
state: str,
code_verifier: str | None = None
) -> tuple[object, bool]
Complete OAuth authorization flow.
Validates state token, exchanges code for tokens, fetches user info, and handles account linking logic.
Account Linking Strategy: 1. If OAuth account exists → update tokens, return existing user 2. If user_id in state (linking mode) → link OAuth to that user 3. If user exists by email (verified) → auto-link OAuth account 4. If new user → create user with random password, mark verified, link OAuth
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states |
OAuthStateAdapter
|
OAuth state adapter |
required |
oauth_accounts |
OAuthAccountAdapter
|
OAuth account adapter |
required |
users |
UserAdapter
|
User adapter |
required |
provider |
OAuthProvider
|
OAuth provider instance |
required |
code |
str
|
Authorization code from OAuth callback |
required |
state |
str
|
State token from OAuth callback |
required |
code_verifier |
str | None
|
Optional PKCE code verifier |
None
|
Returns:
| Type | Description |
|---|---|
tuple[object, bool]
|
Tuple of (user, is_new_user) |
Raises:
| Type | Description |
|---|---|
OAuthStateError
|
If state token is invalid or expired |
OAuthAccountAlreadyLinkedError
|
If OAuth account is linked to different user |
OAuthError
|
If OAuth flow fails for any reason |
Source code in fastauth/core/oauth.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
unlink_oauth_account
¶
unlink_oauth_account(
*, oauth_accounts: OAuthAccountAdapter, user_id: UUID, provider: str
) -> None
Unlink an OAuth provider from a user's account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oauth_accounts |
OAuthAccountAdapter
|
OAuth account adapter |
required |
user_id |
UUID
|
User ID to unlink from |
required |
provider |
str
|
Provider name (e.g., 'google', 'github') |
required |
Raises:
| Type | Description |
|---|---|
OAuthError
|
If OAuth account not found |
Source code in fastauth/core/oauth.py
get_linked_accounts
¶
get_linked_accounts(
*, oauth_accounts: OAuthAccountAdapter, user_id: UUID
) -> list
Get all OAuth accounts linked to a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oauth_accounts |
OAuthAccountAdapter
|
OAuth account adapter |
required |
user_id |
UUID
|
User ID to get linked accounts for |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of OAuth account records |