def fastapi_users(request, mock_user_db, mock_authentication, oauth_client) -> FastAPIUsers: fastapi_users = FastAPIUsers( mock_user_db, [mock_authentication], User, UserCreate, UserUpdate, UserDB, "SECRET", ) fastapi_users.get_oauth_router(oauth_client, "SECRET") @fastapi_users.on_after_register() def on_after_register(): return request.param() @fastapi_users.on_after_forgot_password() def on_after_forgot_password(): return request.param() @fastapi_users.on_after_update() def on_after_update(): return request.param() return fastapi_users
async def test_app_client( mock_user_db, mock_authentication, oauth_client, get_test_client) -> AsyncGenerator[httpx.AsyncClient, None]: fastapi_users = FastAPIUsers( mock_user_db, [mock_authentication], User, UserCreate, UserUpdate, UserDB, ) app = FastAPI() app.include_router(fastapi_users.get_register_router()) app.include_router(fastapi_users.get_reset_password_router("SECRET")) app.include_router(fastapi_users.get_auth_router(mock_authentication)) app.include_router(fastapi_users.get_oauth_router(oauth_client, "SECRET")) app.include_router(fastapi_users.get_users_router(), prefix="/users") @app.get("/current-user") def current_user(user=Depends(fastapi_users.get_current_user)): return user @app.get("/current-active-user") def current_active_user(user=Depends( fastapi_users.get_current_active_user)): return user @app.get("/current-superuser") def current_superuser(user=Depends(fastapi_users.get_current_superuser)): return user @app.get("/optional-current-user") def optional_current_user(user=Depends( fastapi_users.get_optional_current_user)): return user @app.get("/optional-current-active-user") def optional_current_active_user( user=Depends(fastapi_users.get_optional_current_active_user), ): return user @app.get("/optional-current-superuser") def optional_current_superuser( user=Depends(fastapi_users.get_optional_current_superuser), ): return user async for client in get_test_client(app): yield client
def test_get_oauth_router(mocker, fastapi_users: FastAPIUsers, oauth_client: OAuth2): # Check that existing OAuth router declared # before the handlers decorators is correctly binded existing_oauth_router = fastapi_users.oauth_routers[0] event_handlers = existing_oauth_router.event_handlers assert len(event_handlers[Event.ON_AFTER_REGISTER]) == 1 assert len(event_handlers[Event.ON_AFTER_FORGOT_PASSWORD]) == 1 # Check that OAuth router declared # after the handlers decorators is correctly binded oauth_router = fastapi_users.get_oauth_router(oauth_client, "SECRET") assert isinstance(oauth_router, EventHandlersRouter) event_handlers = oauth_router.event_handlers assert len(event_handlers[Event.ON_AFTER_REGISTER]) == 1 assert len(event_handlers[Event.ON_AFTER_FORGOT_PASSWORD]) == 1
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"])
JWTAuthentication(secret=SECRET, lifetime_seconds=3600), ] app = FastAPI() fastapi_users = FastAPIUsers( user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET, ) app.include_router(fastapi_users.router, prefix="/users", tags=["users"]) google_oauth_router = fastapi_users.get_oauth_router(google_oauth_client, SECRET) app.include_router(google_oauth_router, prefix="/google-oauth", tags=["users"]) @fastapi_users.on_after_register() def on_after_register(user: User): print(f"User {user.id} has registered.") @fastapi_users.on_after_forgot_password() def on_after_forgot_password(user: User, token: str): print(f"User {user.id} has forgot their password. Reset token: {token}") @app.on_event("startup") async def startup():
secret=SECRET, lifetime_seconds=3600, tokenUrl="/auth/jwt/login") # FastAPI são ultilitários fornecidos para facilitar a integração do processo OAuth2 # https://fastapi.tiangolo.com/ app = FastAPI() fastapi_users = FastAPIUsers( user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB, ) app.include_route( fastapi_users.get_oauth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"], ) app.include_route( # Depois de registrar que chama a função on_after_register fastapi_users.get_register_router(on_after_register), prefix="/auth", tags=["auth"]) app.include_route(fastapi_users.get_reset_password_router( SECRET, after_forgot_password=on_after_forgot_password()), prefix="/auth", tags=["auth"]) app.include_route(fastapi_users.get_users_router(),
from fastapi_users import FastAPIUsers from fastapi_users.authentication import JWTAuthentication from fastapi_users.db import MongoDBUserDatabase db = CLIENT["database_name"] collection = db["users"] user_db = MongoDBUserDatabase(UserDB, collection) jwt_authentication = JWTAuthentication(secret=SECRET, lifetime_seconds=3600, tokenUrl="/auth/jwt/login") fastapi_users = FastAPIUsers( user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB, ) AUTH_ROUTER = fastapi_users.get_auth_router(jwt_authentication) REGISTER_ROUTER = fastapi_users.get_register_router() RESET_PASSWORD_ROUTER = fastapi_users.get_reset_password_router(SECRET) VERIFY_ROUTER = fastapi_users.get_verify_router(SECRET) USERS_ROUTER = fastapi_users.get_users_router() GOOGLE_OAUTH_ROUTER = fastapi_users.get_oauth_router( oauth_client=GOOGLE_OAUTH_CLIENT, state_secret=SECRET, redirect_url="http://localhost:8080/auth/google/callback", )