Files
François Voron 72aa68c462 Native model and generic ID (#971)
* Use a generic Protocol model for User instead of Pydantic

* Remove UserDB Pydantic schema

* Harmonize schema variable naming to avoid confusions

* Revamp OAuth account model management

* Revamp AccessToken DB strategy to adopt generic model approach

* Make ID a generic instead of forcing UUIDs

* Improve generic typing

* Improve Strategy typing

* Tweak base DB typing

* Don't set Pydantic schemas on FastAPIUsers class: pass it directly on router creation

* Add IntegerIdMixin and export related classes

* Start to revamp doc for V10

* Revamp OAuth documentation

* Fix code highlights

* Write the 9.x.x ➡️ 10.x.x migration doc

* Fix pyproject.toml
2022-05-05 14:51:19 +02:00

67 lines
1.7 KiB
Python

from typing import Any, Generic
from fastapi import Response
from fastapi_users import models
from fastapi_users.authentication.strategy import (
Strategy,
StrategyDestroyNotSupportedError,
)
from fastapi_users.authentication.transport import (
Transport,
TransportLogoutNotSupportedError,
)
from fastapi_users.types import DependencyCallable
class AuthenticationBackend(Generic[models.UP]):
"""
Combination of an authentication transport and strategy.
Together, they provide a full authentication method logic.
:param name: Name of the backend.
:param transport: Authentication transport instance.
:param get_strategy: Dependency callable returning
an authentication strategy instance.
"""
name: str
transport: Transport
def __init__(
self,
name: str,
transport: Transport,
get_strategy: DependencyCallable[Strategy[models.UP, models.ID]],
):
self.name = name
self.transport = transport
self.get_strategy = get_strategy
async def login(
self,
strategy: Strategy[models.UP, models.ID],
user: models.UP,
response: Response,
) -> Any:
token = await strategy.write_token(user)
return await self.transport.get_login_response(token, response)
async def logout(
self,
strategy: Strategy[models.UP, models.ID],
user: models.UP,
token: str,
response: Response,
) -> Any:
try:
await strategy.destroy_token(token, user)
except StrategyDestroyNotSupportedError:
pass
try:
await self.transport.get_logout_response(response)
except TransportLogoutNotSupportedError:
return None