Skip to content

Commit 6fbc27f

Browse files
authored
[storefront] refactor: auth error handling, dependency upgrade (#19)
Signed-off-by: T.H. <7197142+metalalive@users.noreply.github.com>
1 parent d4e57be commit 6fbc27f

File tree

5 files changed

+315
-298
lines changed

5 files changed

+315
-298
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from jwt.exceptions import DecodeError
2+
from ecommerce_common.auth.jwt import JWT
3+
4+
5+
def base_authentication(token: str, audience, keystore, error_obj=None):
6+
jwt = JWT(encoded=token)
7+
payld = jwt.verify(keystore=keystore, audience=audience)
8+
if not payld:
9+
raise DecodeError("payload of jwt token is null, authentication failure")
10+
return payld
11+
12+
13+
def base_permission_check(
14+
user: dict, app_code: int, required_perm_codes: set, error_obj
15+
):
16+
granted_perms = user.get("perms", [])
17+
granted_perms = filter(lambda d: d["app_code"] == app_code, granted_perms)
18+
granted_perm_codes = set(map(lambda d: d["codename"], granted_perms))
19+
coverage = required_perm_codes - granted_perm_codes
20+
if any(coverage):
21+
raise error_obj
22+
return user
23+
24+
25+
def get_unverified_token_payld(token: str):
26+
try:
27+
jwt = JWT(encoded=token)
28+
payld = jwt.payload
29+
except DecodeError as e:
30+
payld = {}
31+
return payld

services/common/python/src/ecommerce_common/auth/fastapi.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

services/common/python/src/ecommerce_common/util/json_renderer.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
# "active": True
1414
# }
1515

16+
1617
def render_json_template(input_template_path, output_file_path, parameters):
1718
if not os.path.exists(input_template_path):
1819
raise FileNotFoundError(f"Template file not found: {input_template_path}")
1920

20-
with open(input_template_path, 'r') as f:
21+
with open(input_template_path, "r") as f:
2122
try:
2223
json_data = json.load(f)
2324
except json.JSONDecodeError as e:
@@ -29,20 +30,27 @@ def set_nested_value(data, keys, value):
2930
data[keys[-1]] = value
3031

3132
for json_path, value in parameters.items():
32-
keys = json_path.split('/')
33+
keys = json_path.split("/")
3334
set_nested_value(json_data, keys, value)
3435

35-
with open(output_file_path, 'w') as f:
36+
with open(output_file_path, "w") as f:
3637
json.dump(json_data, f, indent=4)
3738

39+
3840
def main():
39-
parser = argparse.ArgumentParser(description="Render a JSON template with parameters.")
40-
parser.add_argument("--template", required=True, help="Path to the input JSON template file.")
41-
parser.add_argument("--output", required=True, help="Path to the output rendered JSON file.")
41+
parser = argparse.ArgumentParser(
42+
description="Render a JSON template with parameters."
43+
)
44+
parser.add_argument(
45+
"--template", required=True, help="Path to the input JSON template file."
46+
)
47+
parser.add_argument(
48+
"--output", required=True, help="Path to the output rendered JSON file."
49+
)
4250
parser.add_argument(
4351
"--parameters",
4452
required=True,
45-
help='Parameters as a single string, e.g., "key1=value1 key2=value2 key3=value3".'
53+
help='Parameters as a single string, e.g., "key1=value1 key2=value2 key3=value3".',
4654
)
4755
args = parser.parse_args()
4856
# Resolve relative paths to absolute paths
@@ -65,5 +73,6 @@ def main():
6573
# Call the main function
6674
render_json_template(input_template_path, output_file_path, parameters)
6775

76+
6877
if __name__ == "__main__":
6978
main()

0 commit comments

Comments
 (0)