mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 03:04:27 +08:00
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
This commit is contained in:
@ -2,7 +2,7 @@ from typing import Generic, Sequence, Type
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from fastapi_users import models
|
||||
from fastapi_users import models, schemas
|
||||
from fastapi_users.authentication import AuthenticationBackend, Authenticator
|
||||
from fastapi_users.jwt import SecretType
|
||||
from fastapi_users.manager import UserManagerDependency
|
||||
@ -22,58 +22,49 @@ except ModuleNotFoundError: # pragma: no cover
|
||||
BaseOAuth2 = Type # type: ignore
|
||||
|
||||
|
||||
class FastAPIUsers(Generic[models.U, models.UC, models.UU, models.UD]):
|
||||
class FastAPIUsers(Generic[models.UP, models.ID]):
|
||||
"""
|
||||
Main object that ties together the component for users authentication.
|
||||
|
||||
:param get_user_manager: Dependency callable getter to inject the
|
||||
user manager class instance.
|
||||
:param auth_backends: List of authentication backends.
|
||||
:param user_model: Pydantic model of a user.
|
||||
:param user_create_model: Pydantic model for creating a user.
|
||||
:param user_update_model: Pydantic model for updating a user.
|
||||
:param user_db_model: Pydantic model of a DB representation of a user.
|
||||
|
||||
:attribute current_user: Dependency callable getter to inject authenticated user
|
||||
with a specific set of parameters.
|
||||
"""
|
||||
|
||||
authenticator: Authenticator
|
||||
_user_model: Type[models.U]
|
||||
_user_create_model: Type[models.UC]
|
||||
_user_update_model: Type[models.UU]
|
||||
_user_db_model: Type[models.UD]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
get_user_manager: UserManagerDependency[models.UC, models.UD],
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
auth_backends: Sequence[AuthenticationBackend],
|
||||
user_model: Type[models.U],
|
||||
user_create_model: Type[models.UC],
|
||||
user_update_model: Type[models.UU],
|
||||
user_db_model: Type[models.UD],
|
||||
):
|
||||
self.authenticator = Authenticator(auth_backends, get_user_manager)
|
||||
|
||||
self._user_model = user_model
|
||||
self._user_db_model = user_db_model
|
||||
self._user_create_model = user_create_model
|
||||
self._user_update_model = user_update_model
|
||||
|
||||
self.get_user_manager = get_user_manager
|
||||
self.current_user = self.authenticator.current_user
|
||||
|
||||
def get_register_router(self) -> APIRouter:
|
||||
"""Return a router with a register route."""
|
||||
def get_register_router(
|
||||
self, user_schema: Type[schemas.U], user_create_schema: Type[schemas.UC]
|
||||
) -> APIRouter:
|
||||
"""
|
||||
Return a router with a register route.
|
||||
|
||||
:param user_schema: Pydantic schema of a public user.
|
||||
:param user_create_schema: Pydantic schema for creating a user.
|
||||
"""
|
||||
return get_register_router(
|
||||
self.get_user_manager,
|
||||
self._user_model,
|
||||
self._user_create_model,
|
||||
self.get_user_manager, user_schema, user_create_schema
|
||||
)
|
||||
|
||||
def get_verify_router(self) -> APIRouter:
|
||||
"""Return a router with e-mail verification routes."""
|
||||
return get_verify_router(self.get_user_manager, self._user_model)
|
||||
def get_verify_router(self, user_schema: Type[schemas.U]) -> APIRouter:
|
||||
"""
|
||||
Return a router with e-mail verification routes.
|
||||
|
||||
:param user_schema: Pydantic schema of a public user.
|
||||
"""
|
||||
return get_verify_router(self.get_user_manager, user_schema)
|
||||
|
||||
def get_reset_password_router(self) -> APIRouter:
|
||||
"""Return a reset password process router."""
|
||||
@ -122,19 +113,22 @@ class FastAPIUsers(Generic[models.U, models.UC, models.UU, models.UD]):
|
||||
|
||||
def get_users_router(
|
||||
self,
|
||||
user_schema: Type[schemas.U],
|
||||
user_update_schema: Type[schemas.UU],
|
||||
requires_verification: bool = False,
|
||||
) -> APIRouter:
|
||||
"""
|
||||
Return a router with routes to manage users.
|
||||
|
||||
:param user_schema: Pydantic schema of a public user.
|
||||
:param user_update_schema: Pydantic schema for updating a user.
|
||||
:param requires_verification: Whether the endpoints
|
||||
require the users to be verified or not.
|
||||
"""
|
||||
return get_users_router(
|
||||
self.get_user_manager,
|
||||
self._user_model,
|
||||
self._user_update_model,
|
||||
self._user_db_model,
|
||||
user_schema,
|
||||
user_update_schema,
|
||||
self.authenticator,
|
||||
requires_verification,
|
||||
)
|
||||
|
Reference in New Issue
Block a user