Implement OAuth2 flow (#88)

* Move users router in sub-module

* Factorize UserRouter into EventHandlersRouter

* Implement OAuth registration/login router

* Apply isort/black

* Remove temporary pytest marker

* Fix httpx-oauth version in lock file

* Ensure ON_AFTER_REGISTER event is triggered on OAuth registration

* Add API on FastAPIUsers to generate an OAuth router

* Improve test coverage of FastAPIUsers

* Small fixes

* Write the OAuth documentation

* Fix SQL unit-tests by avoiding collisions in SQLite db files
This commit is contained in:
François Voron
2020-01-17 11:43:17 +01:00
committed by GitHub
parent 54aefea59a
commit 88b133d41c
32 changed files with 1723 additions and 107 deletions

View File

@ -1,5 +1,5 @@
import uuid
from typing import Optional, TypeVar
from typing import List, Optional, TypeVar
import pydantic
from pydantic import BaseModel, EmailStr
@ -19,7 +19,8 @@ class BaseUser(BaseModel):
def create_update_dict(self):
return self.dict(
exclude_unset=True, exclude={"id", "is_superuser", "is_active"}
exclude_unset=True,
exclude={"id", "is_superuser", "is_active", "oauth_accounts"},
)
def create_update_dict_superuser(self):
@ -44,3 +45,28 @@ class BaseUserDB(BaseUser):
UD = TypeVar("UD", bound=BaseUserDB)
class BaseOAuthAccount(BaseModel):
"""Base OAuth account model."""
id: Optional[str] = None
oauth_name: str
access_token: str
expires_at: int
refresh_token: Optional[str] = None
account_id: str
account_email: str
@pydantic.validator("id", pre=True, always=True)
def default_id(cls, v):
return v or str(uuid.uuid4())
class Config:
orm_mode = True
class BaseOAuthAccountMixin(BaseModel):
"""Adds OAuth accounts list to a User model."""
oauth_accounts: List[BaseOAuthAccount] = []