-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
272 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...emplate/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_sa/models/users.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# type: ignore | ||
import uuid | ||
|
||
from fastapi import Depends | ||
from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin, schemas | ||
from fastapi_users.authentication import ( | ||
AuthenticationBackend, | ||
BearerTransport, | ||
|
||
CookieTransport, | ||
JWTStrategy, | ||
) | ||
from fastapi_users.db import SQLAlchemyBaseUserTableUUID, SQLAlchemyUserDatabase | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from {{cookiecutter.project_name}}.db.base import Base | ||
from {{cookiecutter.project_name}}.db.dependencies import get_db_session | ||
from {{cookiecutter.project_name}}.settings import settings | ||
|
||
|
||
class User(SQLAlchemyBaseUserTableUUID, Base): | ||
"""Represents a user entity.""" | ||
|
||
|
||
class UserRead(schemas.BaseUser[uuid.UUID]): | ||
"""Represents a read command for a user.""" | ||
|
||
|
||
class UserCreate(schemas.BaseUserCreate): | ||
"""Represents a create command for a user.""" | ||
|
||
|
||
class UserUpdate(schemas.BaseUserUpdate): | ||
"""Represents an update command for a user.""" | ||
|
||
|
||
class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]): | ||
"""Manages a user session and its tokens.""" | ||
reset_password_token_secret = settings.users_secret | ||
verification_token_secret = settings.users_secret | ||
|
||
|
||
async def get_user_db(session: AsyncSession = Depends(get_db_session)) -> SQLAlchemyUserDatabase: | ||
""" | ||
Yield a SQLAlchemyUserDatabase instance. | ||
:param session: asynchronous SQLAlchemy session. | ||
:yields: instance of SQLAlchemyUserDatabase. | ||
""" | ||
yield SQLAlchemyUserDatabase(session, User) | ||
|
||
|
||
async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)) -> UserManager: | ||
""" | ||
Yield a UserManager instance. | ||
:param user_db: SQLAlchemy user db instance | ||
:yields: an instance of UserManager. | ||
""" | ||
yield UserManager(user_db) | ||
|
||
|
||
def get_jwt_strategy() -> JWTStrategy: | ||
""" | ||
Return a JWTStrategy in order to instantiate it dynamically. | ||
:returns: instance of JWTStrategy with provided settings. | ||
""" | ||
return JWTStrategy(secret=settings.users_secret, lifetime_seconds=None) | ||
|
||
|
||
{%- if cookiecutter.jwt_auth == "True" %} | ||
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login") | ||
auth_jwt = AuthenticationBackend( | ||
name="jwt", | ||
transport=bearer_transport, | ||
get_strategy=get_jwt_strategy, | ||
) | ||
{%- endif %} | ||
|
||
{%- if cookiecutter.cookie_auth == "True" %} | ||
cookie_transport = CookieTransport() | ||
auth_cookie = AuthenticationBackend( | ||
name="cookie", transport=cookie_transport, get_strategy=get_jwt_strategy | ||
) | ||
{%- endif %} | ||
|
||
backends = [ | ||
{%- if cookiecutter.cookie_auth == "True" %} | ||
auth_cookie, | ||
{%- endif %} | ||
{%- if cookiecutter.jwt_auth == "True" %} | ||
auth_jwt, | ||
{%- endif %} | ||
] | ||
|
||
api_users = FastAPIUsers[User, uuid.UUID](get_user_manager, backends) | ||
|
||
current_active_user = api_users.current_user(active=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ate/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/api/users/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"""API for checking project status.""" | ||
from {{cookiecutter.project_name}}.web.api.users.views import router | ||
|
||
__all__ = ["router"] |
Oops, something went wrong.