mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 19:30:47 +08:00

* 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
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
import secrets
|
|
from typing import Generic, Optional
|
|
|
|
import aioredis
|
|
|
|
from fastapi_users import models
|
|
from fastapi_users.authentication.strategy.base import Strategy
|
|
from fastapi_users.manager import BaseUserManager, InvalidID, UserNotExists
|
|
|
|
|
|
class RedisStrategy(Strategy[models.UP, models.ID], Generic[models.UP, models.ID]):
|
|
def __init__(self, redis: aioredis.Redis, lifetime_seconds: Optional[int] = None):
|
|
self.redis = redis
|
|
self.lifetime_seconds = lifetime_seconds
|
|
|
|
async def read_token(
|
|
self, token: Optional[str], user_manager: BaseUserManager[models.UP, models.ID]
|
|
) -> Optional[models.UP]:
|
|
if token is None:
|
|
return None
|
|
|
|
user_id = await self.redis.get(token)
|
|
if user_id is None:
|
|
return None
|
|
|
|
try:
|
|
parsed_id = user_manager.parse_id(user_id)
|
|
return await user_manager.get(parsed_id)
|
|
except (UserNotExists, InvalidID):
|
|
return None
|
|
|
|
async def write_token(self, user: models.UP) -> str:
|
|
token = secrets.token_urlsafe()
|
|
await self.redis.set(token, str(user.id), ex=self.lifetime_seconds)
|
|
return token
|
|
|
|
async def destroy_token(self, token: str, user: models.UP) -> None:
|
|
await self.redis.delete(token)
|