Skip to content

Commit

Permalink
Update flask_authgen_jwt.py
Browse files Browse the repository at this point in the history
Changed some http status codes from inst to use the library
  • Loading branch information
dmtzs committed Dec 19, 2023
1 parent 4ec81a5 commit 1e761c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
1 change: 0 additions & 1 deletion .github/config/.pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[MESSAGES CONTROL]
disable=
C0116, # Missing function or method docstring
W0703, # Catching too general exception Exception
R1705, # Unnecessary "else" after "return"
W0702, # No exception type(s) specified
Expand Down
17 changes: 9 additions & 8 deletions src/flask_authgen_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from functools import wraps
from base64 import b64decode
from datetime import datetime
from http import HTTPStatus
from typing import Callable, Optional, Union
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -91,10 +92,10 @@ def verify_dict_config(self, config: str) -> None:
claims = ["key", "algorithm"]
for claim in claims:
if claim not in self.enc_dec_jwt_callback:
self.gen_abort_error(f"The claim {claim} is not in the dictionary", 400)
self.gen_abort_error(f"The claim {claim} is not in the dictionary", HTTPStatus.BAD_REQUEST.value)
elif config == "rsa_pass":
if "passphrase" not in self.enc_dec_jwt_callback:
self.gen_abort_error("The claim passphrase is not in the dictionary", 400)
self.gen_abort_error("The claim passphrase is not in the dictionary", HTTPStatus.BAD_REQUEST.value)

def verify_user_roles(self, roles: list, user: str) -> None:
"""
Expand Down Expand Up @@ -209,7 +210,7 @@ def __create_jwt_payload(self, bauth_credentials: dict[str, str]) -> dict[str, U
self.gen_abort_error("jwt_claims decorator and function is not defined", 500)
if self.json_body_token:
if not request.is_json:
self.gen_abort_error("Missing JSON in request or not JSON format sent to endpoint", 400)
self.gen_abort_error("Missing JSON in request or not JSON format sent to endpoint", HTTPStatus.BAD_REQUEST.value)
else:
bauth_credentials = request.get_json()
if self.personal_credentials is not None:
Expand All @@ -232,15 +233,15 @@ def __dec_set_basic_auth(self) -> Optional[bool]:
"""
auth_header = request.headers.get("Authorization")
if auth_header is None:
self.gen_abort_error("Authorization header is missing", 400)
self.gen_abort_error("Authorization header is missing", HTTPStatus.BAD_REQUEST.value)
auth_header = auth_header.split(" ")
if auth_header[0] != "Basic":
self.gen_abort_error("Authorization header must be Basic", 400)
self.gen_abort_error("Authorization header must be Basic", HTTPStatus.BAD_REQUEST.value)
credentials = auth_header[1]
credentials = b64decode(credentials).decode("utf-8")
credentials = credentials.split(":")
if len(credentials) != 2:
self.gen_abort_error("Authorization header must be Basic with user and password only", 400)
self.gen_abort_error("Authorization header must be Basic with user and password only", HTTPStatus.BAD_REQUEST.value)
username = credentials[0]
password = credentials[1]
bauth_credentials = {
Expand Down Expand Up @@ -446,7 +447,7 @@ def __decode_jwt(self) -> Optional[dict]:
"""
auth_header = request.headers.get("Authorization")
if auth_header is None:
self.gen_abort_error("Authorization header is missing", 400)
self.gen_abort_error("Authorization header is missing", HTTPStatus.BAD_REQUEST.value)
auth_header = auth_header.split(" ")
token = auth_header[1]
del auth_header
Expand Down Expand Up @@ -486,7 +487,7 @@ def __verify_token(self, token: dict) -> None:
claims = self.get_jwt_claims_to_verify_callback
for claim in claims:
if claim not in token:
self.gen_abort_error(f"The claim {claim} is not in the token", 400)
self.gen_abort_error(f"The claim {claim} is not in the token", HTTPStatus.BAD_REQUEST.value)
if len(token) < 1:
self.gen_abort_error("Invalid token", 401)
if self.personal_credentials is not None:
Expand Down

0 comments on commit 1e761c8

Please sign in to comment.