Fix #42: multiple authentication backends (#47)

* Revamp authentication to allow multiple backends

* Make router generate a login route for each backend

* Apply black

* Remove unused imports

* Complete docstrings

* Update documentation

* WIP add cookie auth

* Complete cookie auth unit tests

* Add documentation for cookie auth

* Fix cookie backend default name

* Don't make cookie return a Response
This commit is contained in:
François Voron
2019-12-04 13:32:49 +01:00
committed by GitHub
parent 5e4c7996de
commit 49deb437a6
22 changed files with 591 additions and 341 deletions

View File

@ -1,8 +1,8 @@
from typing import Callable, Type
from typing import Callable, Sequence, Type
from fastapi_users.authentication import BaseAuthentication
from fastapi_users.authentication import Authenticator, BaseAuthentication
from fastapi_users.db import BaseUserDatabase
from fastapi_users.models import BaseUser, BaseUserDB
from fastapi_users.models import BaseUser
from fastapi_users.router import Event, UserRouter, get_user_router
@ -11,7 +11,7 @@ class FastAPIUsers:
Main object that ties together the component for users authentication.
:param db: Database adapter instance.
:param auth: Authentication logic instance.
:param auth_backends: List of authentication backends.
:param user_model: Pydantic model of a user.
:param reset_password_token_secret: Secret to encode reset password token.
:param reset_password_token_lifetime_seconds: Lifetime of reset password token.
@ -21,36 +21,30 @@ class FastAPIUsers:
"""
db: BaseUserDatabase
auth: BaseAuthentication
authenticator: Authenticator
router: UserRouter
get_current_user: Callable[..., BaseUserDB]
def __init__(
self,
db: BaseUserDatabase,
auth: BaseAuthentication,
auth_backends: Sequence[BaseAuthentication],
user_model: Type[BaseUser],
reset_password_token_secret: str,
reset_password_token_lifetime_seconds: int = 3600,
):
self.db = db
self.auth = auth
self.authenticator = Authenticator(auth_backends, db)
self.router = get_user_router(
self.db,
user_model,
self.auth,
self.authenticator,
reset_password_token_secret,
reset_password_token_lifetime_seconds,
)
get_current_user = self.auth.get_current_user(self.db)
self.get_current_user = get_current_user # type: ignore
get_current_active_user = self.auth.get_current_active_user(self.db)
self.get_current_active_user = get_current_active_user # type: ignore
get_current_superuser = self.auth.get_current_superuser(self.db)
self.get_current_superuser = get_current_superuser # type: ignore
self.get_current_user = self.authenticator.get_current_user
self.get_current_active_user = self.authenticator.get_current_active_user
self.get_current_superuser = self.authenticator.get_current_superuser
def on_after_register(self) -> Callable:
"""Add an event handler on successful registration."""