Oauth¶
oauth
¶
OAuth authentication API endpoints.
Provides endpoints for OAuth authentication flows including authorization URL generation, callback handling, and account linking.
Requires: pip install sreekarnv-fastauth[oauth] (for OAuth providers)
Classes¶
Functions¶
authorize
¶
authorize(
provider: str,
request: Request,
session: Session = Depends(get_session),
current_user: User | None = None,
) -> OAuthAuthorizationResponse
Initiate OAuth authorization flow.
This endpoint generates a state token and authorization URL. If the user is logged in, this will be a linking flow. If not logged in, this will be a login/registration flow.
The code_verifier for PKCE is stored in the session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name |
OAuth provider (e.g., 'google', 'github') |
required | |
request |
Request
|
FastAPI request object |
required |
session |
Session
|
Database session |
Depends(get_session)
|
current_user |
User | None
|
Optional current user (if authenticated) |
None
|
Returns:
| Type | Description |
|---|---|
OAuthAuthorizationResponse
|
OAuthAuthorizationResponse with authorization_url |
Source code in fastauth/api/oauth.py
oauth_callback
async
¶
oauth_callback(
provider: str,
payload: OAuthCallbackRequest,
request: Request,
session: Session = Depends(get_session),
) -> TokenResponse
Handle OAuth callback after user authorization.
This endpoint: 1. Validates the state token (CSRF protection) 2. Exchanges authorization code for tokens 3. Fetches user info from provider 4. Creates or links user account 5. Issues FastAuth tokens (JWT + refresh token)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name |
OAuth provider (e.g., 'google', 'github') |
required | |
payload |
OAuthCallbackRequest
|
Callback request with code and state |
required |
request |
Request
|
FastAPI request object |
required |
session |
Session
|
Database session |
Depends(get_session)
|
Returns:
| Type | Description |
|---|---|
TokenResponse
|
TokenResponse with access_token and refresh_token |
Source code in fastauth/api/oauth.py
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 | |
list_linked_accounts
¶
list_linked_accounts(
session: Session = Depends(get_session),
current_user: User = Depends(get_current_user),
) -> list[OAuthLinkResponse]
List all OAuth accounts linked to the current user.
Requires authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session |
Session
|
Database session |
Depends(get_session)
|
current_user |
User
|
Current authenticated user |
Depends(get_current_user)
|
Returns:
| Type | Description |
|---|---|
list[OAuthLinkResponse]
|
List of linked OAuth accounts |
Source code in fastauth/api/oauth.py
unlink_provider
¶
unlink_provider(
provider: str,
session: Session = Depends(get_session),
current_user: User = Depends(get_current_user),
) -> None
Unlink an OAuth provider from the current user's account.
Requires authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name |
OAuth provider to unlink (e.g., 'google', 'github') |
required | |
session |
Session
|
Database session |
Depends(get_session)
|
current_user |
User
|
Current authenticated user |
Depends(get_current_user)
|
Returns:
| Type | Description |
|---|---|
None
|
204 No Content on success |