mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 03:04:27 +08:00
Complete OAuth2 documentation
This commit is contained in:
@ -2,6 +2,7 @@ import databases
|
||||
import sqlalchemy
|
||||
from fastapi_users.db import OrmarBaseUserModel, OrmarUserDatabase
|
||||
|
||||
from .models import UserDB
|
||||
|
||||
DATABASE_URL = "sqlite:///test.db"
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
35
docs/src/db_sqlalchemy_oauth.py
Normal file
35
docs/src/db_sqlalchemy_oauth.py
Normal file
@ -0,0 +1,35 @@
|
||||
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__
|
||||
|
||||
|
||||
def get_user_db():
|
||||
yield SQLAlchemyUserDatabase(UserDB, database, users, oauth_accounts)
|
9
docs/src/db_tortoise_oauth_adapter.py
Normal file
9
docs/src/db_tortoise_oauth_adapter.py
Normal file
@ -0,0 +1,9 @@
|
||||
from fastapi_users.db import TortoiseUserDatabase
|
||||
|
||||
from .models import OAuthAccount, UserDB, UserModel
|
||||
|
||||
DATABASE_URL = "sqlite://./test.db"
|
||||
|
||||
|
||||
def get_user_db():
|
||||
yield TortoiseUserDatabase(UserDB, UserModel, OAuthAccount)
|
30
docs/src/db_tortoise_oauth_model.py
Normal file
30
docs/src/db_tortoise_oauth_model.py
Normal file
@ -0,0 +1,30 @@
|
||||
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")
|
@ -1,89 +0,0 @@
|
||||
import motor.motor_asyncio
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi_users import FastAPIUsers, models
|
||||
from fastapi_users.authentication import JWTAuthentication
|
||||
from fastapi_users.db import MongoDBUserDatabase
|
||||
from httpx_oauth.clients.google import GoogleOAuth2
|
||||
|
||||
DATABASE_URL = "mongodb://localhost:27017"
|
||||
SECRET = "SECRET"
|
||||
|
||||
|
||||
google_oauth_client = GoogleOAuth2("CLIENT_ID", "CLIENT_SECRET")
|
||||
|
||||
|
||||
class User(models.BaseUser, models.BaseOAuthAccountMixin):
|
||||
pass
|
||||
|
||||
|
||||
class UserCreate(models.BaseUserCreate):
|
||||
pass
|
||||
|
||||
|
||||
class UserUpdate(User, models.BaseUserUpdate):
|
||||
pass
|
||||
|
||||
|
||||
class UserDB(User, models.BaseUserDB):
|
||||
pass
|
||||
|
||||
|
||||
client = motor.motor_asyncio.AsyncIOMotorClient(
|
||||
DATABASE_URL, uuidRepresentation="standard"
|
||||
)
|
||||
db = client["database_name"]
|
||||
collection = db["users"]
|
||||
user_db = MongoDBUserDatabase(UserDB, collection)
|
||||
|
||||
|
||||
def on_after_register(user: UserDB, request: Request):
|
||||
print(f"User {user.id} has registered.")
|
||||
|
||||
|
||||
def on_after_forgot_password(user: UserDB, token: str, request: Request):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
|
||||
|
||||
def after_verification_request(user: UserDB, token: str, request: Request):
|
||||
print(f"Verification requested for user {user.id}. Verification token: {token}")
|
||||
|
||||
|
||||
jwt_authentication = JWTAuthentication(
|
||||
secret=SECRET, lifetime_seconds=3600, tokenUrl="auth/jwt/login"
|
||||
)
|
||||
|
||||
app = FastAPI()
|
||||
fastapi_users = FastAPIUsers(
|
||||
user_db,
|
||||
[jwt_authentication],
|
||||
User,
|
||||
UserCreate,
|
||||
UserUpdate,
|
||||
UserDB,
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"]
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_register_router(on_after_register), prefix="/auth", tags=["auth"]
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_reset_password_router(
|
||||
SECRET, after_forgot_password=on_after_forgot_password
|
||||
),
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_verify_router(
|
||||
SECRET, after_verification_request=after_verification_request
|
||||
),
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
)
|
||||
app.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"])
|
||||
|
||||
google_oauth_router = fastapi_users.get_oauth_router(
|
||||
google_oauth_client, SECRET, after_register=on_after_register
|
||||
)
|
||||
app.include_router(google_oauth_router, prefix="/auth/google", tags=["auth"])
|
@ -1,119 +0,0 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi_users import FastAPIUsers, models
|
||||
from fastapi_users.authentication import JWTAuthentication
|
||||
from fastapi_users.db import (
|
||||
SQLAlchemyBaseOAuthAccountTable,
|
||||
SQLAlchemyBaseUserTable,
|
||||
SQLAlchemyUserDatabase,
|
||||
)
|
||||
from httpx_oauth.clients.google import GoogleOAuth2
|
||||
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
||||
|
||||
DATABASE_URL = "sqlite:///./test.db"
|
||||
SECRET = "SECRET"
|
||||
|
||||
|
||||
google_oauth_client = GoogleOAuth2("CLIENT_ID", "CLIENT_SECRET")
|
||||
|
||||
|
||||
class User(models.BaseUser, models.BaseOAuthAccountMixin):
|
||||
pass
|
||||
|
||||
|
||||
class UserCreate(models.BaseUserCreate):
|
||||
pass
|
||||
|
||||
|
||||
class UserUpdate(User, models.BaseUserUpdate):
|
||||
pass
|
||||
|
||||
|
||||
class UserDB(User, models.BaseUserDB):
|
||||
pass
|
||||
|
||||
|
||||
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__
|
||||
user_db = SQLAlchemyUserDatabase(UserDB, database, users, oauth_accounts)
|
||||
|
||||
|
||||
def on_after_register(user: UserDB, request: Request):
|
||||
print(f"User {user.id} has registered.")
|
||||
|
||||
|
||||
def on_after_forgot_password(user: UserDB, token: str, request: Request):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
|
||||
|
||||
def after_verification_request(user: UserDB, token: str, request: Request):
|
||||
print(f"Verification requested for user {user.id}. Verification token: {token}")
|
||||
|
||||
|
||||
jwt_authentication = JWTAuthentication(
|
||||
secret=SECRET, lifetime_seconds=3600, tokenUrl="auth/jwt/login"
|
||||
)
|
||||
|
||||
app = FastAPI()
|
||||
fastapi_users = FastAPIUsers(
|
||||
user_db,
|
||||
[jwt_authentication],
|
||||
User,
|
||||
UserCreate,
|
||||
UserUpdate,
|
||||
UserDB,
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"]
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_register_router(on_after_register), prefix="/auth", tags=["auth"]
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_reset_password_router(
|
||||
SECRET, after_forgot_password=on_after_forgot_password
|
||||
),
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_verify_router(
|
||||
SECRET, after_verification_request=after_verification_request
|
||||
),
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
)
|
||||
app.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"])
|
||||
|
||||
google_oauth_router = fastapi_users.get_oauth_router(
|
||||
google_oauth_client, SECRET, after_register=on_after_register
|
||||
)
|
||||
app.include_router(google_oauth_router, prefix="/auth/google", tags=["auth"])
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
await database.connect()
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
await database.disconnect()
|
@ -1,106 +0,0 @@
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi_users import FastAPIUsers, models
|
||||
from fastapi_users.authentication import JWTAuthentication
|
||||
from fastapi_users.db import (
|
||||
TortoiseBaseOAuthAccountModel,
|
||||
TortoiseBaseUserModel,
|
||||
TortoiseUserDatabase,
|
||||
)
|
||||
from httpx_oauth.clients.google import GoogleOAuth2
|
||||
from tortoise import fields
|
||||
from tortoise.contrib.fastapi import register_tortoise
|
||||
from tortoise.contrib.pydantic import PydanticModel
|
||||
|
||||
DATABASE_URL = "sqlite://./test.db"
|
||||
SECRET = "SECRET"
|
||||
|
||||
|
||||
google_oauth_client = GoogleOAuth2("CLIENT_ID", "CLIENT_SECRET")
|
||||
|
||||
|
||||
class UserModel(TortoiseBaseUserModel):
|
||||
pass
|
||||
|
||||
|
||||
class OAuthAccountModel(TortoiseBaseOAuthAccountModel):
|
||||
user = fields.ForeignKeyField("models.UserModel", related_name="oauth_accounts")
|
||||
|
||||
|
||||
class User(models.BaseUser, models.BaseOAuthAccountMixin):
|
||||
pass
|
||||
|
||||
|
||||
class UserCreate(models.BaseUserCreate):
|
||||
pass
|
||||
|
||||
|
||||
class UserUpdate(User, models.BaseUserUpdate):
|
||||
pass
|
||||
|
||||
|
||||
class UserDB(User, models.BaseUserDB, PydanticModel):
|
||||
class Config:
|
||||
orm_mode = True
|
||||
orig_model = UserModel
|
||||
|
||||
|
||||
user_db = TortoiseUserDatabase(UserDB, UserModel, OAuthAccountModel)
|
||||
app = FastAPI()
|
||||
register_tortoise(
|
||||
app,
|
||||
db_url=DATABASE_URL,
|
||||
modules={"models": ["oauth_full_tortoise"]},
|
||||
generate_schemas=True,
|
||||
)
|
||||
|
||||
|
||||
def on_after_register(user: UserDB, request: Request):
|
||||
print(f"User {user.id} has registered.")
|
||||
|
||||
|
||||
def on_after_forgot_password(user: UserDB, token: str, request: Request):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
|
||||
|
||||
def after_verification_request(user: UserDB, token: str, request: Request):
|
||||
print(f"Verification requested for user {user.id}. Verification token: {token}")
|
||||
|
||||
|
||||
jwt_authentication = JWTAuthentication(
|
||||
secret=SECRET, lifetime_seconds=3600, tokenUrl="auth/jwt/login"
|
||||
)
|
||||
|
||||
fastapi_users = FastAPIUsers(
|
||||
user_db,
|
||||
[jwt_authentication],
|
||||
User,
|
||||
UserCreate,
|
||||
UserUpdate,
|
||||
UserDB,
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"]
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_register_router(on_after_register), prefix="/auth", tags=["auth"]
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_reset_password_router(
|
||||
SECRET, after_forgot_password=on_after_forgot_password
|
||||
),
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
)
|
||||
app.include_router(
|
||||
fastapi_users.get_verify_router(
|
||||
SECRET, after_verification_request=after_verification_request
|
||||
),
|
||||
prefix="/auth",
|
||||
tags=["auth"],
|
||||
)
|
||||
app.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"])
|
||||
|
||||
google_oauth_router = fastapi_users.get_oauth_router(
|
||||
google_oauth_client, SECRET, after_register=on_after_register
|
||||
)
|
||||
app.include_router(google_oauth_router, prefix="/auth/google", tags=["auth"])
|
Reference in New Issue
Block a user