From d83b37d523d9807de8c0a5ae921b6b33655bbeb7 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Tue, 30 Jul 2024 11:16:50 +0545 Subject: [PATCH] refactor: user routes are kept in a single file --- src/backend/app/main.py | 2 - src/backend/app/users/oauth_routes.py | 75 --------------------------- src/backend/app/users/user_routes.py | 71 ++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 79 deletions(-) delete mode 100644 src/backend/app/users/oauth_routes.py diff --git a/src/backend/app/main.py b/src/backend/app/main.py index 643340a7..1e8b0081 100644 --- a/src/backend/app/main.py +++ b/src/backend/app/main.py @@ -12,7 +12,6 @@ from app.projects import project_routes from app.drones import drone_routes from app.waypoints import waypoint_routes -from app.users import oauth_routes from app.users import user_routes from app.tasks import task_routes from app.db.database import db_connection @@ -99,7 +98,6 @@ def get_application() -> FastAPI: _app.include_router(project_routes.router) _app.include_router(waypoint_routes.router) _app.include_router(user_routes.router) - _app.include_router(oauth_routes.router) _app.include_router(task_routes.router) return _app diff --git a/src/backend/app/users/oauth_routes.py b/src/backend/app/users/oauth_routes.py deleted file mode 100644 index 071d1c21..00000000 --- a/src/backend/app/users/oauth_routes.py +++ /dev/null @@ -1,75 +0,0 @@ -import os -from loguru import logger as log -from fastapi import Depends, Request -from fastapi.responses import JSONResponse -from app.db import database -from app.users.user_routes import router -from app.users.user_deps import init_google_auth, login_required -from app.users.user_schemas import AuthUser, Token -from app.users import user_crud -from app.config import settings -from databases import Database - - -if settings.DEBUG: - os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" - - -@router.get("/google-login") -async def login_url(google_auth=Depends(init_google_auth)): - """Get Login URL for Google Oauth Application. - - The application must be registered on google oauth. - Open the download url returned to get access_token. - - Args: - request: The GET request. - google_auth: The Auth object. - - Returns: - login_url (string): URL to authorize user in Google OAuth. - Includes URL params: client_id, redirect_uri, permission scope. - """ - login_url = google_auth.login() - log.debug(f"Login URL returned: {login_url}") - return JSONResponse(content=login_url, status_code=200) - - -@router.get("/callback/") -async def callback(request: Request, google_auth=Depends(init_google_auth)): - """Performs token exchange between Google and DTM API""" - - # Enforce https callback url - callback_url = str(request.url).replace("http://", "https://") - - access_token = google_auth.callback(callback_url).get("access_token") - - user_data = google_auth.deserialize_access_token(access_token) - access_token, refresh_token = await user_crud.create_access_token(user_data) - - return Token(access_token=access_token, refresh_token=refresh_token) - - -@router.get("/refresh-token", response_model=Token) -async def update_token(user_data: AuthUser = Depends(login_required)): - """Refresh access token""" - - access_token, refresh_token = await user_crud.create_access_token( - user_data.model_dump() - ) - return Token(access_token=access_token, refresh_token=refresh_token) - - -@router.get("/my-info/") -async def my_data( - db: Database = Depends(database.get_db), - user_data: AuthUser = Depends(login_required), -): - """Read access token and get user details from Google""" - - user_info = await user_crud.get_or_create_user(db, user_data) - has_user_profile = await user_crud.get_userprofile_by_userid(db, user_info.id) - - user_info_dict = user_info.model_dump() - user_info_dict["has_user_profile"] = bool(has_user_profile) - return user_info_dict diff --git a/src/backend/app/users/user_routes.py b/src/backend/app/users/user_routes.py index ec431209..5bef1d91 100644 --- a/src/backend/app/users/user_routes.py +++ b/src/backend/app/users/user_routes.py @@ -1,4 +1,5 @@ -from fastapi import APIRouter, Response, HTTPException, Depends +import os +from fastapi import APIRouter, Response, HTTPException, Depends, Request from typing import Annotated from fastapi.security import OAuth2PasswordRequestForm from app.users.user_schemas import ( @@ -6,12 +7,18 @@ ProfileUpdate, AuthUser, ) -from app.users.user_deps import login_required +from app.users.user_deps import login_required, init_google_auth from app.config import settings from app.users import user_crud from app.db import database from app.models.enums import HTTPStatus from databases import Database +from fastapi.responses import JSONResponse +from loguru import logger as log + + +if settings.DEBUG: + os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" router = APIRouter( @@ -79,3 +86,63 @@ async def update_user_profile( user = await user_crud.update_user_profile(db, user_id, profile_update) return Response(status_code=HTTPStatus.OK) + + +@router.get("/google-login") +async def login_url(google_auth=Depends(init_google_auth)): + """Get Login URL for Google Oauth Application. + + The application must be registered on google oauth. + Open the download url returned to get access_token. + + Args: + request: The GET request. + google_auth: The Auth object. + + Returns: + login_url (string): URL to authorize user in Google OAuth. + Includes URL params: client_id, redirect_uri, permission scope. + """ + login_url = google_auth.login() + log.debug(f"Login URL returned: {login_url}") + return JSONResponse(content=login_url, status_code=200) + + +@router.get("/callback/") +async def callback(request: Request, google_auth=Depends(init_google_auth)): + """Performs token exchange between Google and DTM API""" + + # Enforce https callback url + callback_url = str(request.url).replace("http://", "https://") + + access_token = google_auth.callback(callback_url).get("access_token") + + user_data = google_auth.deserialize_access_token(access_token) + access_token, refresh_token = await user_crud.create_access_token(user_data) + + return Token(access_token=access_token, refresh_token=refresh_token) + + +@router.get("/refresh-token", response_model=Token) +async def update_token(user_data: AuthUser = Depends(login_required)): + """Refresh access token""" + + access_token, refresh_token = await user_crud.create_access_token( + user_data.model_dump() + ) + return Token(access_token=access_token, refresh_token=refresh_token) + + +@router.get("/my-info/") +async def my_data( + db: Database = Depends(database.get_db), + user_data: AuthUser = Depends(login_required), +): + """Read access token and get user details from Google""" + + user_info = await user_crud.get_or_create_user(db, user_data) + has_user_profile = await user_crud.get_userprofile_by_userid(db, user_info.id) + + user_info_dict = user_info.model_dump() + user_info_dict["has_user_profile"] = bool(has_user_profile) + return user_info_dict