|
| 1 | +from dependency_injector.wiring import Provide, inject |
| 2 | +from fastapi import Depends, Response |
| 3 | +from fastapi.responses import RedirectResponse |
| 4 | +from starlette import status |
| 5 | + |
| 6 | +from app.core.auth import AuthDeps |
| 7 | +from app.core.configs import configs |
| 8 | +from app.core.container import Container |
| 9 | +from app.core.router import CoreAPIRouter |
| 10 | +from app.schemas.auth import JwtAccessToken, JwtRefreshToken, JwtToken |
| 11 | +from app.schemas.users import ( |
| 12 | + UserOut, |
| 13 | + UserPasswordRequest, |
| 14 | + UserRegisterRequest, |
| 15 | + UserResponse, |
| 16 | +) |
| 17 | +from app.services.users import UserService |
| 18 | + |
| 19 | +router = CoreAPIRouter(prefix="/auth", tags=["auth"]) |
| 20 | + |
| 21 | + |
| 22 | +@router.post( |
| 23 | + "/refresh", |
| 24 | + response_model=JwtAccessToken, |
| 25 | + status_code=status.HTTP_200_OK, |
| 26 | + summary="", |
| 27 | + description="", |
| 28 | +) |
| 29 | +@inject |
| 30 | +async def post_refresh_token( |
| 31 | + token: JwtRefreshToken, |
| 32 | + service: UserService = Depends(Provide[Container.user_service]), |
| 33 | +): |
| 34 | + return await service.refresh(token) |
| 35 | + |
| 36 | + |
| 37 | +@router.post( |
| 38 | + "/register", |
| 39 | + response_model=UserResponse, |
| 40 | + status_code=status.HTTP_201_CREATED, |
| 41 | + summary="Register with password", |
| 42 | + description="", |
| 43 | +) |
| 44 | +@inject |
| 45 | +async def register_password( |
| 46 | + request: UserRegisterRequest, |
| 47 | + service: UserService = Depends(Provide[Container.user_service]), |
| 48 | +): |
| 49 | + return await service.register(request) |
| 50 | + |
| 51 | + |
| 52 | +@router.post( |
| 53 | + "/login", |
| 54 | + response_model=JwtToken, |
| 55 | + status_code=status.HTTP_200_OK, |
| 56 | + summary="Log in with password", |
| 57 | + description="", |
| 58 | +) |
| 59 | +@inject |
| 60 | +async def log_in_password( |
| 61 | + request: UserPasswordRequest, |
| 62 | + service: UserService = Depends(Provide[Container.user_service]), |
| 63 | +): |
| 64 | + return await service.log_in_password(schema=request) |
| 65 | + |
| 66 | + |
| 67 | +@router.get( |
| 68 | + "/oauth/login/github", |
| 69 | + response_model=Response, |
| 70 | + status_code=status.HTTP_302_FOUND, |
| 71 | + summary="Log in with GitHub OAuth", |
| 72 | + description="GitHub OAuth를 위해 redirection", |
| 73 | +) |
| 74 | +async def log_in_github(): |
| 75 | + # NOTE: &scope=repo,user |
| 76 | + # TODO: APIResponse (related: #24) |
| 77 | + return RedirectResponse( |
| 78 | + f"https://github.com/login/oauth/authorize?client_id={configs.GITHUB_OAUTH_CLIENT_ID}" |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +@router.get( |
| 83 | + "/oauth/callback/github", |
| 84 | + response_model=JwtToken, |
| 85 | + status_code=status.HTTP_200_OK, |
| 86 | + summary="Callback for GitHub OAuth", |
| 87 | + description="GitHub OAuth에 의해 redirection될 endpoint", |
| 88 | + include_in_schema=False, |
| 89 | +) |
| 90 | +@inject |
| 91 | +async def callback_github( |
| 92 | + code: str, |
| 93 | + service: UserService = Depends(Provide[Container.user_service]), |
| 94 | +): |
| 95 | + """ |
| 96 | + # NOTE: Cookie 방식으로 JWT token 사용 시 |
| 97 | + response.set_cookie( |
| 98 | + key="access_token", value=jwt_token.access_token, httponly=True, secure=True |
| 99 | + ) |
| 100 | + response.set_cookie( |
| 101 | + key="refresh_token", value=jwt_token.refresh_token, httponly=True, secure=True |
| 102 | + ) |
| 103 | + """ |
| 104 | + return await service.log_in_github(code=code) |
| 105 | + |
| 106 | + |
| 107 | +@router.get( |
| 108 | + "/me", |
| 109 | + response_model=UserOut, |
| 110 | + status_code=status.HTTP_200_OK, |
| 111 | + summary="", |
| 112 | + description="", |
| 113 | +) |
| 114 | +async def get_me(user: AuthDeps): |
| 115 | + return user |
0 commit comments