2424
2525from aiohttp .web import Request
2626from aiohttp .web import Response
27- from aiohttp .web_exceptions import HTTPNotFound , HTTPFound , HTTPUnauthorized
27+ from aiohttp .web_exceptions import HTTPNotFound , HTTPFound , HTTPUnauthorized , HTTPBadRequest
2828
2929from ....htserver import UnauthorizedError
3030from ....htserver import ForbiddenError
4141
4242from ..auth import AuthManager
4343
44+ from ....logging import get_logger
45+
4446
4547# =====
4648_COOKIE_AUTH_TOKEN = "auth_token"
@@ -139,7 +141,7 @@ async def __logout_handler(self, req: Request) -> Response:
139141 async def __check_handler (self , _ : Request ) -> Response :
140142 return make_json_response ()
141143
142- @exposed_http ("GET" , "/auth/oauth/providers" , auth_required = False )
144+ @exposed_http ("GET" , "/auth/oauth/providers" , auth_required = False , allow_usc = False )
143145 async def __oauth_providers (self , request : Request ) -> Response :
144146 """
145147 Return a json containing the available Providers with short_name and long_name and if oauth is enabled
@@ -153,7 +155,7 @@ async def __oauth_providers(self, request: Request) -> Response:
153155 response .update ({'enabled' : True , 'providers' : self .__auth_manager .oauth_manager .get_providers ()})
154156 return make_json_response (response )
155157
156- @exposed_http ("GET" , "/auth/oauth/login/{provider}" , auth_required = False )
158+ @exposed_http ("GET" , "/auth/oauth/login/{provider}" , auth_required = False , allow_usc = False )
157159 async def __oauth (self , request : Request ) -> None :
158160 """
159161 Creates the redirect to the Provider specified in the URL. Checks if the provider is valid.
@@ -162,12 +164,13 @@ async def __oauth(self, request: Request) -> None:
162164 @return: redirect to provider
163165 """
164166 if self .__auth_manager .oauth_manager is None :
165- return
167+ raise HTTPBadRequest (reason = "Auth disabled" )
168+
166169 provider = format (request .match_info ['provider' ])
167170 if not self .__auth_manager .oauth_manager .valid_provider (provider ):
168171 raise HTTPNotFound (reason = "Unknown provider %s" % provider )
169172
170- redirect_url = request .url .with_path (f"/api/auth/oauth/callback/{ provider } / " ).with_scheme (' https' )
173+ redirect_url = request .url .with_path (f"/api/auth/oauth/callback/{ provider } " ).with_scheme (" https" )
171174 oauth_cookie = request .cookies .get (_COOKIE_OAUTH_SESSION , "" )
172175
173176 is_valid_session = await self .__auth_manager .oauth_manager .is_valid_session (provider , oauth_cookie )
@@ -186,7 +189,7 @@ async def __oauth(self, request: Request) -> None:
186189 # 302 redirect to provider:
187190 raise response
188191
189- @exposed_http ("GET" , "/auth/oauth/callback/{provider}" , auth_required = False )
192+ @exposed_http ("GET" , "/auth/oauth/callback/{provider}" , auth_required = False , allow_usc = False )
190193 async def __callback (self , request : Request ) -> Response :
191194 """
192195 After successful login on the side of the provider, the user gets redirected here. If everything is correct,
@@ -195,7 +198,7 @@ async def __callback(self, request: Request) -> Response:
195198 @return:
196199 """
197200 if self .__auth_manager .oauth_manager is None :
198- return make_json_response ( )
201+ raise HTTPBadRequest ( reason = "Auth disabled" )
199202
200203 if not request .match_info ['provider' ]:
201204 raise HTTPUnauthorized (reason = "Provider is missing" )
@@ -210,19 +213,25 @@ async def __callback(self, request: Request) -> Response:
210213 if not self .__auth_manager .oauth_manager .is_redirect_from_provider (provider = provider , request_query = dict (request .query )):
211214 raise HTTPUnauthorized (reason = "Authorization Code is missing" )
212215
213- redirect_url = request .url .with_query ( "" ). with_path (f"/api/auth/oauth/callback/{ provider } " ).with_scheme (' https' )
216+ redirect_url = request .url .with_path (f"/api/auth/oauth/callback/{ provider } " ).with_scheme (" https" )
214217 user = await self .__auth_manager .oauth_manager .get_user_info (
215218 provider = provider ,
216219 oauth_session = oauth_session ,
217220 request_query = dict (request .query ),
218221 redirect_url = redirect_url
219222 )
223+ if not user :
224+ raise ForbiddenError ()
220225
221- if self .__auth_manager .is_auth_enabled ():
222- token = await self .__auth_manager .login_oauth (
223- user = valid_user (user )
224- )
225- if token :
226- return make_json_response (set_cookies = {_COOKIE_AUTH_TOKEN : token })
226+ token = await self .__auth_manager .login_oauth (
227+ user = valid_user (user )
228+ )
229+ get_logger ().info (f"OAUTH CALLBACK: { token = } " )
230+ if not token :
227231 raise ForbiddenError ()
228- return make_json_response ()
232+
233+ response = HTTPFound (
234+ request .url .with_path ("" ).with_scheme ("https" )
235+ )
236+ response .set_cookie (name = _COOKIE_AUTH_TOKEN , value = token , samesite = "Lax" , httponly = True )
237+ return response
0 commit comments