auth_backends = [] jwt_authentication = JWTAuthentication(secret=SECRET, lifetime_seconds=3600) auth_backends.append(jwt_authentication) all_users = FastAPIUsers( user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, ) any_user = all_users.current_user(active=True) employee = all_users.current_user(active=True, superuser=False) developer_user = all_users.current_user(active=True, superuser=True) default_uuid = UUID4("00000000-0000-0000-0000-000000000000") async def get_owner(client_id: int, user: UserTable = Depends(any_user)): if not (user.client_id == client_id and user.is_owner): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) return user async def get_owner_with_superuser(client_id: int, user: UserTable = Depends(any_user)): if not user.is_superuser and not (user.client_id == client_id
SECRET = "SECRET" auth_backends = [] jwt_authentication = JWTAuthentication(secret=SECRET, lifetime_seconds=3600) auth_backends.append(jwt_authentication) fastapi_users = FastAPIUsers( user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, ) current_active_user = fastapi_users.current_user(active=True) auth_router = APIRouter(tags=["auth"]) auth_router.include_router( fastapi_users.get_auth_router(jwt_authentication, requires_verification=False) ) auth_router.include_router(fastapi_users.get_register_router()) user_router = APIRouter(tags=["users"]) user_router.include_router( fastapi_users.get_users_router(), prefix="/users", )
from .authentication.models.core import * from .authentication.models.account import * from .authentication.models.pydantic import * from .authentication.Mailman import * from .authentication.fapiusers import * userdb = TortoiseUDB(UserDB, UserMod, include=['username', 'timezone'], alternate=UserDBComplete) jwtauth = JWTAuthentication(secret=s.SECRET_KEY, lifetime_seconds=s.ACCESS_TOKEN_EXPIRE) fapiuser = FastAPIUsers(userdb, [jwtauth], User, UserCreate, UserUpdate, UserDB) current_user = fapiuser.current_user() tokenonly = OAuth2PasswordBearer(tokenUrl='token') REFRESH_TOKEN_KEY = 'refresh_token' # Don't change this. This is hard-coded as a variable. async def register_callback(user: UserDB, _: Response): """ Send an email containing a link the user can use to verify their account. This email directly shows the success/fail notice upon completion. """ # Set the groups for this new user groups = await Group.filter(name__in=s.USER_GROUPS) user = await UserMod.get(pk=user.id).only('id', 'email') await user.groups.add(*groups)
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"], dependencies=[Depends(fastapi_users.current_user(active=True, superuser=True))] ) app.include_router( fastapi_users.get_reset_password_router( SECRET_KEY, after_forgot_password=on_after_forgot_password ), prefix="/auth", tags=["auth"], ) app.include_router( fastapi_users.get_users_router(), prefix="/users", tags=["users"] )
models.UserCreate, models.UserUpdate, models.UserDB) # Add the router for users to get JWTs app.include_router(fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["Authentication"]) # Add the router that provides the user registration endpoint app.include_router(fastapi_users.get_register_router(), prefix="/auth", tags=["Authentication"], include_in_schema=False) # Helper functions that gets the user based on the JWT passed to the endpoint current_active_user = fastapi_users.current_user(active=True) current_super_user = fastapi_users.current_user(active=True, superuser=True) @app.get('/', response_class=RedirectResponse, include_in_schema=False) async def redirect_to_project() -> str: """Redirects the root path to the project website""" return 'https://ibm.github.io/arcade' @app.get('/user_reports', response_model=List[models.UserReport], include_in_schema=False) async def get_user_reports(user: models.User = Depends( current_super_user)) -> List[models.UserReport]: """Returns summary statistics for all users"""