Beispiel #1
0
async def startup():
    await db.connect_to_database(path=DATABASE_URL)
    disc_db = db.client[DATABASE_NAME]
    global fs
    fs = AsyncIOMotorGridFSBucket(disc_db)

    global song_db
    song_db = disc_db.get_collection(SONGS_DB)

    global mix_db
    mix_db = disc_db.get_collection(MIX_DB)

    user_col = disc_db[USER_DB]
    global user_db
    user_db = MongoDBUserDatabase(UserDB, user_col)
    global fastapi_users
    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(after_register=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"])
Beispiel #2
0
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"])
Beispiel #3
0
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.include_router(fastapi_users.get_verify_router("SECRET"))

    @app.delete("/users/me")
    def custom_users_route():
        return None

    @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-verified-user")
    def current_verified_user(user=Depends(fastapi_users.get_current_verified_user)):
        return user

    @app.get("/current-superuser")
    def current_superuser(user=Depends(fastapi_users.get_current_superuser)):
        return user

    @app.get("/current-verified-superuser")
    def current_verified_superuser(
        user=Depends(fastapi_users.get_current_verified_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-verified-user")
    def optional_current_verified_user(
        user=Depends(fastapi_users.get_optional_current_verified_user),
    ):
        return user

    @app.get("/optional-current-superuser")
    def optional_current_superuser(
        user=Depends(fastapi_users.get_optional_current_superuser),
    ):
        return user

    @app.get("/optional-current-verified-superuser")
    def optional_current_verified_superuser(
        user=Depends(fastapi_users.get_optional_current_verified_superuser),
    ):
        return user

    async for client in get_test_client(app):
        yield client
Beispiel #4
0
    [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(),
                   prefix="/auth",
                   tags=["auth"])
app.include_router(fastapi_users.get_reset_password_router(APP_SECRET),
                   prefix="/auth",
                   tags=["auth"])
app.include_router(fastapi_users.get_verify_router(APP_SECRET),
                   prefix="/auth",
                   tags=["auth"])
app.include_router(fastapi_users.get_users_router(),
                   prefix="/users",
                   tags=["users"])
app.add_middleware(CORSMiddleware,
                   allow_origins=["*"],
                   allow_credentials=True,
                   allow_methods=["*"],
                   allow_headers=["*"])


@app.on_event("startup")
async def startup():
    await database.connect()
Beispiel #5
0
from fastapi import APIRouter
from fastapi_users import FastAPIUsers

from app.models.user import User, UserCreate, UserDB, UserUpdate
from app.users.auth_backend import authentication_backend
from app.users.user_manager import get_user_manager

fastapi_users = FastAPIUsers(
    get_user_manager,
    [authentication_backend],
    User,
    UserCreate,
    UserUpdate,
    UserDB,
)

auth_router = APIRouter(prefix="/auth", tags=["auth"])
auth_router.include_router(
    fastapi_users.get_auth_router(authentication_backend), prefix="/jwt")
auth_router.include_router(fastapi_users.get_register_router())
auth_router.include_router(fastapi_users.get_reset_password_router())
auth_router.include_router(fastapi_users.get_verify_router())

users_router = APIRouter()
users_router.include_router(auth_router)
users_router.include_router(fastapi_users.get_users_router(),
                            prefix="/users",
                            tags=["users"])

current_active_user = fastapi_users.current_user(active=True)
Beispiel #6
0
from fastapi_users import FastAPIUsers
from fastapi_users.authentication import JWTAuthentication

from {{ cookiecutter.project_slug }}.adapters.db import repo_sqlalchemy, models
from {{ cookiecutter.project_slug }}.config import config
from {{ cookiecutter.project_slug }} import domain


jwt_authentication = JWTAuthentication(
    secret=config.TOKEN_SECRET, lifetime_seconds=3600, tokenUrl=config.TOKEN_URL,
)


users = FastAPIUsers(
    repo_sqlalchemy.fastapi_user,
    [jwt_authentication],
    domain.User,
    domain.commands.CreateUser,
    domain.commands.UpdateUser,
    models.UserDB,
)


auth_router = users.get_auth_router(jwt_authentication)
register_router = users.get_register_router()
reset_password_router = users.get_reset_password_router(config.TOKEN_SECRET)
verify_router = users.get_verify_router(config.TOKEN_SECRET)
users_router = users.get_users_router()
Beispiel #7
0
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",
)