1+ import logging
12from typing import Optional , Union
23from uuid import UUID
34
1112from . import actions
1213from . import data
1314from . import models
14- from .external import yield_db_session_from_env
15+ from .db import yield_db_read_only_session
1516from .settings import BOT_INSTALLATION_TOKEN , BOT_INSTALLATION_TOKEN_HEADER
1617
18+ logger = logging .getLogger (__name__ )
19+
1720# Login implementation follows:
1821# https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/
1922oauth2_scheme = OAuth2PasswordBearer (tokenUrl = "token" )
2225
2326async def get_current_user (
2427 token : UUID = Depends (oauth2_scheme ),
25- db_session = Depends (yield_db_session_from_env ),
28+ db_session = Depends (yield_db_read_only_session ),
2629) -> models .User :
2730 try :
2831 token_object = actions .get_token (session = db_session , token = token )
2932 except actions .TokenNotFound :
3033 raise HTTPException (status_code = 404 , detail = "Access token not found" )
34+ except Exception :
35+ logger .error ("Unhandled exception at get_current_user" )
36+ raise HTTPException (status_code = 500 )
3137 if not token_object .active :
3238 raise HTTPException (status_code = 403 , detail = "Token has expired" )
3339 return token_object .user
3440
3541
3642async def get_current_user_with_groups (
3743 token : UUID = Depends (oauth2_scheme ),
38- db_session = Depends (yield_db_session_from_env ),
44+ db_session = Depends (yield_db_read_only_session ),
3945) -> data .UserWithGroupsResponse :
4046 try :
4147 token_active , user_extended = actions .get_current_user_with_groups (
@@ -44,6 +50,7 @@ async def get_current_user_with_groups(
4450 except actions .TokenNotFound :
4551 raise HTTPException (status_code = 404 , detail = "Access token not found" )
4652 except Exception :
53+ logger .error ("Unhandled exception at get_current_user_with_groups" )
4754 raise HTTPException (status_code = 500 )
4855 if not token_active :
4956 raise HTTPException (status_code = 403 , detail = "Token has expired" )
@@ -78,7 +85,7 @@ def autogenerated_user_token_check(request: Request) -> bool:
7885async def get_current_user_or_installation (
7986 request : Request ,
8087 token : UUID = Depends (oauth2_scheme_manual ),
81- db_session = Depends (yield_db_session_from_env ),
88+ db_session = Depends (yield_db_read_only_session ),
8289) -> Union [models .User , bool ]:
8390 """
8491 Allow access if Bugout installation token provided, if not
@@ -97,7 +104,7 @@ async def get_current_user_or_installation(
97104async def is_token_restricted_or_installation (
98105 request : Request ,
99106 token : UUID = Depends (oauth2_scheme_manual ),
100- db_session = Depends (yield_db_session_from_env ),
107+ db_session = Depends (yield_db_read_only_session ),
101108) -> bool :
102109 """
103110 Allow access if Bugout installation provided.
@@ -114,10 +121,13 @@ async def is_token_restricted_or_installation(
114121
115122async def is_token_restricted (
116123 token : UUID = Depends (oauth2_scheme ),
117- db_session = Depends (yield_db_session_from_env ),
124+ db_session = Depends (yield_db_read_only_session ),
118125) -> bool :
119126 try :
120127 token_object = actions .get_token (session = db_session , token = token )
121128 except actions .TokenNotFound :
122129 raise HTTPException (status_code = 404 , detail = "Access token not found" )
130+ except Exception :
131+ logger .error ("Unhandled exception at is_token_restricted" )
132+ raise HTTPException (status_code = 500 )
123133 return token_object .restricted
0 commit comments