Initial commit
This commit is contained in:
0
api/rpk-api/__init__.py
Normal file
0
api/rpk-api/__init__.py
Normal file
BIN
api/rpk-api/__pycache__/main.cpython-313.pyc
Normal file
BIN
api/rpk-api/__pycache__/main.cpython-313.pyc
Normal file
Binary file not shown.
0
api/rpk-api/firm/__init__.py
Normal file
0
api/rpk-api/firm/__init__.py
Normal file
13
api/rpk-api/hub/__init__.py
Normal file
13
api/rpk-api/hub/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import os
|
||||
|
||||
import motor.motor_asyncio
|
||||
from beanie import Document
|
||||
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
|
||||
from pydantic import Field
|
||||
|
||||
DATABASE_URL = "mongodb://localhost:27017"
|
||||
client = motor.motor_asyncio.AsyncIOMotorClient(
|
||||
DATABASE_URL, uuidRepresentation="standard"
|
||||
)
|
||||
db = client["database_name"]
|
||||
|
||||
BIN
api/rpk-api/hub/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
api/rpk-api/hub/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
52
api/rpk-api/hub/auth/__init__.py
Normal file
52
api/rpk-api/hub/auth/__init__.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi_users import UUIDIDMixin, BaseUserManager
|
||||
from fastapi_users.authentication import AuthenticationBackend, BearerTransport
|
||||
from fastapi_users.authentication.strategy import AccessTokenDatabase, DatabaseStrategy
|
||||
from fastapi_users_db_beanie.access_token import BeanieBaseAccessTokenDocument, BeanieAccessTokenDatabase
|
||||
from httpx_oauth.clients.google import GoogleOAuth2
|
||||
from fastapi_users.fastapi_users import get_oauth_router
|
||||
|
||||
from hub.user import User, get_user_db
|
||||
|
||||
|
||||
SECRET = os.getenv("FASTAPI_USERS_SECRET")
|
||||
google_oauth_client = GoogleOAuth2(os.getenv("GOOGLE_CLIENT_ID"), os.getenv("GOOGLE_CLIENT_SECRET"))
|
||||
|
||||
|
||||
TOKEN_LIFETIME = 3600
|
||||
|
||||
bearer_transport = BearerTransport(tokenUrl="auth/login")
|
||||
|
||||
|
||||
class AccessToken(BeanieBaseAccessTokenDocument):
|
||||
pass
|
||||
|
||||
async def get_access_token_db():
|
||||
yield BeanieAccessTokenDatabase(AccessToken)
|
||||
|
||||
|
||||
def get_database_strategy(
|
||||
access_token_db: AccessTokenDatabase[AccessToken] = Depends(get_access_token_db),
|
||||
) -> DatabaseStrategy:
|
||||
return DatabaseStrategy(access_token_db, lifetime_seconds=TOKEN_LIFETIME)
|
||||
|
||||
|
||||
class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
|
||||
reset_password_token_secret = SECRET
|
||||
verification_token_secret = SECRET
|
||||
|
||||
|
||||
async def get_user_manager(user_db=Depends(get_user_db)):
|
||||
yield UserManager(user_db)
|
||||
|
||||
|
||||
auth_backend = AuthenticationBackend(
|
||||
name="db",
|
||||
transport=bearer_transport,
|
||||
get_strategy=get_database_strategy,
|
||||
)
|
||||
|
||||
oauth_router = get_oauth_router(google_oauth_client, auth_backend, get_user_manager, SECRET)
|
||||
BIN
api/rpk-api/hub/auth/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
api/rpk-api/hub/auth/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
19
api/rpk-api/hub/user/__init__.py
Normal file
19
api/rpk-api/hub/user/__init__.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import Depends
|
||||
from fastapi_users_db_beanie import BaseOAuthAccount, BeanieUserDatabase, BeanieBaseUserDocument
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
class OAuthAccount(BaseOAuthAccount):
|
||||
pass
|
||||
|
||||
class User(BeanieBaseUserDocument):
|
||||
oauth_accounts: list[OAuthAccount] = Field(default_factory=list)
|
||||
|
||||
class UserDatabase(BeanieUserDatabase):
|
||||
pass
|
||||
|
||||
async def get_user_db():
|
||||
yield UserDatabase(User, OAuthAccount)
|
||||
|
||||
async def get_user_manager(user_db=Depends(get_user_db)):
|
||||
yield UserManager(user_db)
|
||||
BIN
api/rpk-api/hub/user/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
api/rpk-api/hub/user/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
25
api/rpk-api/main.py
Normal file
25
api/rpk-api/main.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from hub.auth import oauth_router
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run("main:app", host='0.0.0.0', port=8000, reload=True)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# create_db_and_tables()
|
||||
# create_admin_user()
|
||||
yield
|
||||
# do something before end
|
||||
|
||||
app = FastAPI(root_path="/api/v1", lifespan=lifespan)
|
||||
|
||||
app.include_router(
|
||||
oauth_router,
|
||||
prefix="/auth/google",
|
||||
tags=["auth"],
|
||||
)
|
||||
Reference in New Issue
Block a user