Reference
LoginManager
Bases: OAuth2PasswordBearer
Attributes:
Name | Type | Description |
---|---|---|
secret |
Secret
|
The secret used to sign and decrypt the JWT |
algorithm |
str
|
The algorithm used to decrypt the token defaults to |
token_url |
str
|
The url where the user can login to get the token |
use_cookie |
bool
|
Whether cookies should be checked for the token |
use_header |
bool
|
Whether headers should be checked for the token |
pwd_context |
CryptContext
|
Instance of |
Source code in fastapi_login/fastapi_login.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
__call__(request, security_scopes=None)
async
Provides the functionality to act as a Dependency
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The incoming request, this is set automatically by FastAPI |
required |
Returns:
Type | Description |
---|---|
The user object or None |
Raises:
Type | Description |
---|---|
not_authenticated_exception
|
If set by the user and |
Source code in fastapi_login/fastapi_login.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
__init__(secret, token_url, algorithm='HS256', use_cookie=False, use_header=True, cookie_name='access-token', custom_exception=None, default_expiry=timedelta(minutes=15), scopes=None)
Initializes LoginManager
Parameters:
Name | Type | Description | Default |
---|---|---|---|
algorithm |
str
|
Should be "HS256" or "RS256" used to decrypt the JWT |
'HS256'
|
token_url |
str
|
The url where the user can login to get the token |
required |
use_cookie |
bool
|
Set if cookies should be checked for the token |
False
|
use_header |
bool
|
Set if headers should be checked for the token |
True
|
cookie_name |
str
|
Name of the cookie to check for the token |
'access-token'
|
custom_exception |
Exception
|
Exception to raise when the user is not authenticated
this defaults to |
None
|
default_expiry |
timedelta
|
The default expiry time of the token, defaults to 15 minutes |
timedelta(minutes=15)
|
scopes |
Dict[str, str]
|
Scopes argument of OAuth2PasswordBearer for more information see
|
None
|
Source code in fastapi_login/fastapi_login.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
algorithm = algorithm
instance-attribute
cookie_name = cookie_name
instance-attribute
create_access_token(*, data, expires=None, scopes=None)
Helper function to create the encoded access token using the provided secret and the algorithm of the LoginManager instance
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
The data which should be stored in the token |
required |
expires |
timedelta
|
An optional timedelta in which the token expires. Defaults to 15 minutes |
None
|
scopes |
Collection
|
Optional scopes the token user has access to. |
None
|
Returns:
Type | Description |
---|---|
str
|
The encoded JWT with the data and the expiry. The expiry is |
str
|
available under the 'exp' key |
Source code in fastapi_login/fastapi_login.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
default_expiry = default_expiry
instance-attribute
get_current_user(token)
async
This decodes the jwt based on the secret and the algorithm set on the instance.
If the token is correctly formatted and the user is found the user object
is returned else this raises LoginManager.not_authenticated_exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token |
str
|
The encoded jwt token |
required |
Returns:
Type | Description |
---|---|
The user object returned by the instances |
Raises:
Type | Description |
---|---|
not_authenticated_exception
|
The token is invalid or None was returned by |
Source code in fastapi_login/fastapi_login.py
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 |
|
has_scopes(token, required_scopes)
Returns true if the required scopes are present in the token Args: token: The encoded JWT token required_scopes: The scopes required to access this route
Returns:
Type | Description |
---|---|
bool
|
True if the required scopes are contained in the tokens payload |
Source code in fastapi_login/fastapi_login.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
not_authenticated_exception
property
writable
Exception raised when no (valid) token is present.
Defaults to fastapi_login.exceptions.InvalidCredentialsException
The property will deprecated in the future in favor of the custom_exception argument
on initialization
oauth_scheme = None
instance-attribute
optional(request, security_scopes=None)
async
Acts as a dependency which catches all errors, i.e. NotAuthenticatedException
and returns None instead
Source code in fastapi_login/fastapi_login.py
422 423 424 425 426 427 428 429 430 431 |
|
pwd_context = CryptContext(schemes=['bcrypt'])
instance-attribute
secret = to_secret({'algorithms': algorithm, 'secret': secret})
instance-attribute
set_cookie(response, token)
Utility function to set a cookie containing token on the response
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Response
|
The response which is send back |
required |
token |
str
|
The created JWT |
required |
Source code in fastapi_login/fastapi_login.py
297 298 299 300 301 302 303 304 305 |
|
tokenUrl = token_url
instance-attribute
useRequest(app)
Add the instance as a middleware, which adds the user object, if present, to the request state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app |
FastAPI
|
A instance of FastAPI |
required |
Source code in fastapi_login/fastapi_login.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
use_cookie = use_cookie
instance-attribute
use_header = use_header
instance-attribute
user_loader(*args, **kwargs)
This sets the callback to retrieve the user. The function should take an unique identifier like an email and return the user object or None.
Basic usage:
>>> from fastapi import FastAPI
>>> from fastapi_login import LoginManager
>>> app = FastAPI()
>>> # use import os; print(os.urandom(24).hex()) to get a suitable secret key
>>> SECRET = "super-secret"
>>> manager = LoginManager(SECRET, token_url="Login")
>>> manager.user_loader()(get_user)
>>> @manager.user_loader(...) # Arguments and keyword arguments declared here are passed on
>>> def get_user(user_identifier, ...):
... # get user logic here
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Positional arguments to pass on to the decorated method |
()
|
|
kwargs |
Keyword arguments to pass on to the decorated method |
{}
|
Returns:
Type | Description |
---|---|
Union[Callable, Callable[..., Awaitable]]
|
The callback |
Source code in fastapi_login/fastapi_login.py
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 |
|