Skip to content

Commit 8a033b2

Browse files
authored
Фикс ошибки 401, 403 (#46)
1 parent 8b22195 commit 8a033b2

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

auth_lib/fastapi.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,18 @@ def __init__(
6565
)
6666
self.scopes = scopes
6767

68-
def _except(self):
68+
def _except_not_authorized(self):
6969
if self.auto_error:
7070
raise HTTPException(
71-
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
71+
status_code=HTTP_401_UNAUTHORIZED, detail="Not authorized"
72+
)
73+
else:
74+
return None
75+
76+
def _except_not_authentificated(self):
77+
if self.auto_error:
78+
raise HTTPException(
79+
status_code=HTTP_403_FORBIDDEN, detail="Not authentificated"
7280
)
7381
else:
7482
return None
@@ -77,7 +85,7 @@ async def _get_session(self, token: str | None) -> dict[str, Any] | None:
7785
if not token and self.allow_none:
7886
return None
7987
if not token:
80-
return self._except()
88+
return self._except_not_authorized()
8189
return await AsyncAuthLib(auth_url=self.auth_url).check_token(token)
8290

8391
async def _get_userdata(
@@ -86,7 +94,7 @@ async def _get_userdata(
8694
if not token and self.allow_none:
8795
return None
8896
if not token:
89-
return self._except()
97+
return self._except_not_authorized()
9098
if self.enable_userdata:
9199
return await AsyncAuthLib(userdata_url=self.userdata_url).get_user_data(
92100
token, user_id
@@ -100,7 +108,7 @@ async def __call__(
100108
token = request.headers.get("Authorization")
101109
result = await self._get_session(token)
102110
if result is None:
103-
return self._except()
111+
return self._except_not_authorized()
104112
if self.enable_userdata:
105113
user_data_info = await self._get_userdata(token, result["id"])
106114
result["userdata"] = []
@@ -111,5 +119,5 @@ async def __call__(
111119
)
112120
required_scopes = set([scope.lower() for scope in self.scopes])
113121
if required_scopes - session_scopes:
114-
self._except()
122+
self._except_not_authentificated()
115123
return result

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name="auth_lib_profcomff",
13-
version="2024.04.06",
13+
version="2024.04.07",
1414
author="Semyon Grigoriev",
1515
long_description=readme,
1616
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)