Skip to content

Commit 975baeb

Browse files
author
IndominusByte
committed
refactor code
1 parent f56fbd6 commit 975baeb

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

fastapi_jwt_auth/auth_jwt.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def _verified_token(self,encoded_token: str, issuer: Optional[str] = None) -> Di
227227
except Exception as err:
228228
raise JWTDecodeError(status_code=422,message=str(err))
229229

230-
def has_token_in_denylist_callback(self) -> bool:
230+
def _has_token_in_denylist_callback(self) -> bool:
231231
"""
232232
Return True if token denylist callback set
233233
"""
@@ -242,7 +242,7 @@ def _check_token_is_revoked(self, raw_token: Dict[str,Union[str,int,bool]]) -> N
242242
if not self._denylist_enabled:
243243
return
244244

245-
if not self.has_token_in_denylist_callback():
245+
if not self._has_token_in_denylist_callback():
246246
raise RuntimeError("A token_in_denylist_callback must be provided via "
247247
"the '@AuthJWT.token_in_denylist_loader' if "
248248
"AUTHJWT_DENYLIST_ENABLED is 'True'")
@@ -346,7 +346,7 @@ def jwt_required(self) -> None:
346346
if not self._token:
347347
raise MissingHeaderError(status_code=401,message="Missing {} Header".format(self._header_name))
348348

349-
if self.get_raw_jwt()['type'] != 'access':
349+
if self._get_type_token() != 'access':
350350
raise AccessTokenRequired(status_code=422,message="Only access tokens are allowed")
351351

352352
def jwt_optional(self) -> None:
@@ -360,7 +360,7 @@ def jwt_optional(self) -> None:
360360
if self._token:
361361
self._verifying_token(encoded_token=self._token,issuer=self._decode_issuer)
362362

363-
if self._token and self.get_raw_jwt()['type'] != 'access':
363+
if self._token and self._get_type_token() != 'access':
364364
raise AccessTokenRequired(status_code=422,message="Only access tokens are allowed")
365365

366366
def jwt_refresh_token_required(self) -> None:
@@ -375,7 +375,7 @@ def jwt_refresh_token_required(self) -> None:
375375
if not self._token:
376376
raise MissingHeaderError(status_code=401,message="Missing {} Header".format(self._header_name))
377377

378-
if self.get_raw_jwt()['type'] != 'refresh':
378+
if self._get_type_token() != 'refresh':
379379
raise RefreshTokenRequired(status_code=422,message="Only refresh tokens are allowed")
380380

381381
def fresh_jwt_required(self) -> None:
@@ -390,12 +390,18 @@ def fresh_jwt_required(self) -> None:
390390
if not self._token:
391391
raise MissingHeaderError(status_code=401,message="Missing {} Header".format(self._header_name))
392392

393-
if self.get_raw_jwt()['type'] != 'access':
393+
if self._get_type_token() != 'access':
394394
raise AccessTokenRequired(status_code=422,message="Only access tokens are allowed")
395395

396-
if not self.get_raw_jwt()['fresh']:
396+
if not self._get_fresh_token():
397397
raise FreshTokenRequired(status_code=401,message="Fresh token required")
398398

399+
def _get_type_token(self) -> str:
400+
return self.get_raw_jwt()['type']
401+
402+
def _get_fresh_token(self) -> bool:
403+
return self.get_raw_jwt()['fresh']
404+
399405
def get_raw_jwt(self) -> Optional[Dict[str,Union[str,int,bool]]]:
400406
"""
401407
this will return the python dictionary which has all of the claims of the JWT that is accessing the endpoint.

0 commit comments

Comments
 (0)