diff --git a/auth_lib/fastapi.py b/auth_lib/fastapi.py index 7555840..e6481ba 100644 --- a/auth_lib/fastapi.py +++ b/auth_lib/fastapi.py @@ -65,10 +65,18 @@ def __init__( ) self.scopes = scopes - def _except(self): + def _except_not_authorized(self): if self.auto_error: raise HTTPException( - status_code=HTTP_403_FORBIDDEN, detail="Not authenticated" + status_code=HTTP_401_UNAUTHORIZED, detail="Not authorized" + ) + else: + return None + + def _except_not_authentificated(self): + if self.auto_error: + raise HTTPException( + status_code=HTTP_403_FORBIDDEN, detail="Not authentificated" ) else: return None @@ -77,7 +85,7 @@ async def _get_session(self, token: str | None) -> dict[str, Any] | None: if not token and self.allow_none: return None if not token: - return self._except() + return self._except_not_authorized() return await AsyncAuthLib(auth_url=self.auth_url).check_token(token) async def _get_userdata( @@ -86,7 +94,7 @@ async def _get_userdata( if not token and self.allow_none: return None if not token: - return self._except() + return self._except_not_authorized() if self.enable_userdata: return await AsyncAuthLib(userdata_url=self.userdata_url).get_user_data( token, user_id @@ -100,7 +108,7 @@ async def __call__( token = request.headers.get("Authorization") result = await self._get_session(token) if result is None: - return self._except() + return self._except_not_authorized() if self.enable_userdata: user_data_info = await self._get_userdata(token, result["id"]) result["userdata"] = [] @@ -111,5 +119,5 @@ async def __call__( ) required_scopes = set([scope.lower() for scope in self.scopes]) if required_scopes - session_scopes: - self._except() + self._except_not_authentificated() return result diff --git a/setup.py b/setup.py index e60b586..0d5d9d2 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="auth_lib_profcomff", - version="2024.04.06", + version="2024.04.07", author="Semyon Grigoriev", long_description=readme, long_description_content_type="text/markdown",