-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API supabase #593
API supabase #593
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
大筋は問題ないと思います。細かい点だけコメントしました
api/app/auth/auth_module.py
Outdated
def get_credentials(token: HTTPAuthorizationCredentials = Depends(token_scheme)): | ||
return token.credentials |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
利用していないので、この処理は不要かも?
api/app/account.py
Outdated
|
||
|
||
def get_current_user( | ||
token: HTTPAuthorizationCredentials = Depends(token_scheme), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTPBearer 以外のtoken scheme を使う予定もなさそうなので、DependsでなくてHTTP Bearer決め内でもいいかもしれません
api/app/main.py
Outdated
|
||
# Dependency injection as needed | ||
app.dependency_overrides[get_firebase_credentials] = lambda: cred | ||
app.dependency_overrides[get_auth_module] = override_get_auth_module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下でもいいかもしれません (わかりにくければ元の方でもいいかなと)
app.dependency_overrides[get_auth_module] = override_get_auth_module | |
app.dependency_overrides[get_auth_module] = lambda: auth_module |
@mshim03 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTPExceptionを返す場所を制限したいので、authmoduleの中で実行するのは避けて欲しいです
if not user: | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail="No such user", | ||
) | ||
if user.disabled: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="Inactive user", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
router以外で、HTTPExceptionはできれば避けたいが、現状HTTPBearerからトークンを取得して認証するので、HTTP専用の関数となっている。一旦このままでも問題なさそう
|
||
# Dependency injection as needed | ||
app.dependency_overrides[get_firebase_credentials] = lambda: cred | ||
app.dependency_overrides[get_auth_module] = lambda: auth_module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firebaseのdependency injectionの仕組みを利用
api/app/auth/firebase_auth_module.py
Outdated
except requests.exceptions.Timeout as firebase_timeout: | ||
raise HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail="Could not validate credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) from firebase_timeout | ||
|
||
data = resp.json() | ||
if not resp.ok: | ||
error_message = data["error"]["message"] | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail=error_message if error_message else "Could not validate credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここではHTTPExceptionでなく、Exceptionを発出し、router関数の中 (/token
) でHTTPExceptionを返すようにして欲しいです (どこでHTTPExceptionを発行しているのかがわからなくなる)
api/app/auth/firebase_auth_module.py
Outdated
raise HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail="Could not refresh token", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) from firebase_timeout | ||
|
||
data: dict = resp.json() | ||
if not resp.ok: | ||
error_message: str = data["error"]["message"] | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail=error_message if error_message else "Could not refresh token", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらも同様に、HTTPException をここで発出するのを避けて欲しいです
api/app/auth/firebase_auth_module.py
Outdated
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Token has expired", | ||
) from error | ||
except auth.RevokedIdTokenError as error: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Token has revoked", | ||
) from error | ||
except auth.CertificateFetchError as error: | ||
raise HTTPException( | ||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | ||
detail="Failed to obtain required credentials", | ||
) from error | ||
except auth.UserDisabledError as error: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Disabled user", | ||
) from error | ||
except (auth.InvalidIdTokenError, ValueError) as error: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Could not validate credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) from error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTPExceptionについて同様
@mshim03 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typoの修正とディレクトリの変更お願いいたします
from fastapi import HTTPException, status | ||
|
||
from app.auth.auth_exception import AuthErrorType, AuthException | ||
|
||
|
||
def get_status_code(error_type: AuthErrorType): | ||
match error_type: | ||
case AuthErrorType.UNAUTHORIZED: | ||
return status.HTTP_401_UNAUTHORIZED | ||
case AuthErrorType.INTERNAL_SERVER_ERROR: | ||
return status.HTTP_500_INTERNAL_SERVER_ERROR | ||
case AuthErrorType.SERVICE_UNAVAILABLE: | ||
return status.HTTP_503_SERVICE_UNAVAILABLE | ||
|
||
|
||
def create_http_excption(auth_exception: AuthException) -> HTTPException: | ||
return HTTPException( | ||
status_code=get_status_code(auth_exception.error_type), | ||
detail=auth_exception.message, | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ルーティングよう関数と同じディレクトリになっているので、 validators
同様に utils/
など別フォルダに入れていただけると助かります
return status.HTTP_503_SERVICE_UNAVAILABLE | ||
|
||
|
||
def create_http_excption(auth_exception: AuthException) -> HTTPException: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_http_exception
のtypoのようです
@mshim03 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR の目的
経緯・意図・意思決定
token: HTTPAuthorizationCredentials = Depends(HTTPBearer))
のようにして取得するが、このDependsはルーター関数のDepends、またはそのDependsから呼び出されるDepends、でないとエラーとなる
参考文献
公式 Python Client Library
https://supabase.com/docs/reference/python/introduction