OAuth2¶
FastAPI Users provides an optional OAuth2 authentication support. It relies on HTTPX OAuth library, which is a pure-async implementation of OAuth2.
Installation¶
You should install the library with the optional dependencies for OAuth:
pip install 'fastapi-users[sqlalchemy,oauth]'
pip install 'fastapi-users[mongodb,oauth]'
pip install 'fastapi-users[tortoise-orm,oauth]'
Configuration¶
Instantiate an OAuth2 client¶
You first need to get an HTTPX OAuth client instance. Read the documentation for more information.
from httpx_oauth.clients.google import GoogleOAuth2
google_oauth_client = GoogleOAuth2("CLIENT_ID", "CLIENT_SECRET")
Setup the models¶
The user models differ a bit from the standard one as we have to have a way to store the OAuth information (access tokens, account ids...).
from fastapi_users import models
class User(models.BaseUser, models.BaseOAuthAccountMixin):
pass
class UserCreate(models.BaseUserCreate):
pass
class UserUpdate(models.BaseUserUpdate):
pass
class UserDB(User, models.BaseUserDB):
pass
Notice that we inherit from the BaseOAuthAccountMixin
, which adds a List
of BaseOAuthAccount
objects. This object is structured like this:
id
(UUID4
) – Unique identifier of the OAuth account information. Defaults to a UUID4.oauth_name
(str
) – Name of the OAuth service. It corresponds to thename
property of the OAuth client.access_token
(str
) – Access token.expires_at
(Optional[int]
) - Timestamp at which the access token is expired.refresh_token
(Optional[str]
) – On services that support it, a token to get a fresh access token.account_id
(str
) - Identifier of the OAuth account on the corresponding service.account_email
(str
) - Email address of the OAuth account on the corresponding service.
Setup the database adapter¶
SQLAlchemy¶
You'll need to define the table for storing the OAuth account model. We provide a base one for this:
import databases
import sqlalchemy
from fastapi_users.db import (
SQLAlchemyBaseOAuthAccountTable,
SQLAlchemyBaseUserTable,
SQLAlchemyUserDatabase,
)
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from .models import UserDB
DATABASE_URL = "sqlite:///./test.db"
database = databases.Database(DATABASE_URL)
Base: DeclarativeMeta = declarative_base()
class UserTable(Base, SQLAlchemyBaseUserTable):
pass
class OAuthAccount(SQLAlchemyBaseOAuthAccountTable, Base):
pass
engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
Base.metadata.create_all(engine)
users = UserTable.__table__
oauth_accounts = OAuthAccount.__table__
async def get_user_db():
yield SQLAlchemyUserDatabase(UserDB, database, users, oauth_accounts)
When instantiating the database adapter, you should pass this table in argument::
import databases
import sqlalchemy
from fastapi_users.db import (
SQLAlchemyBaseOAuthAccountTable,
SQLAlchemyBaseUserTable,
SQLAlchemyUserDatabase,
)
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from .models import UserDB
DATABASE_URL = "sqlite:///./test.db"
database = databases.Database(DATABASE_URL)
Base: DeclarativeMeta = declarative_base()
class UserTable(Base, SQLAlchemyBaseUserTable):
pass
class OAuthAccount(SQLAlchemyBaseOAuthAccountTable, Base):
pass
engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
Base.metadata.create_all(engine)
users = UserTable.__table__
oauth_accounts = OAuthAccount.__table__
async def get_user_db():
yield SQLAlchemyUserDatabase(UserDB, database, users, oauth_accounts)
MongoDB¶
Nothing to do, the basic configuration is enough.
Tortoise ORM¶
You'll need to define the Tortoise model for storing the OAuth account model. We provide a base one for this:
from fastapi_users import models
from fastapi_users.db import TortoiseBaseOAuthAccountModel, TortoiseBaseUserModel
from tortoise import fields
from tortoise.contrib.pydantic import PydanticModel
class User(models.BaseUser, models.BaseOAuthAccountMixin):
pass
class UserCreate(models.BaseUserCreate):
pass
class UserUpdate(models.BaseUserUpdate):
pass
class UserModel(TortoiseBaseUserModel):
pass
class UserDB(User, models.BaseUserDB, PydanticModel):
class Config:
orm_mode = True
orig_model = UserModel
class OAuthAccount(TortoiseBaseOAuthAccountModel):
user = fields.ForeignKeyField("models.UserModel", related_name="oauth_accounts")
Warning
Note that you should define the foreign key yourself, so that you can point it the user model in your namespace.
Then, you should declare it on the database adapter:
from fastapi_users.db import TortoiseUserDatabase
from .models import OAuthAccount, UserDB, UserModel
DATABASE_URL = "sqlite://./test.db"
async def get_user_db():
yield TortoiseUserDatabase(UserDB, UserModel, OAuthAccount)
Generate a router¶
Once you have a FastAPIUsers
instance, you can make it generate a single OAuth router for the given client.
app.include_router(
fastapi_users.get_oauth_router(google_oauth_client, "SECRET"),
prefix="/auth/google",
tags=["auth"],
)
Full example¶
Warning
Notice that SECRET should be changed to a strong passphrase. Insecure passwords may give attackers full access to your database.