1+ import base64
12import datetime
23import logging
34import typing
67import jwt
78import streamlit as st
89
10+ from testgen import settings
911from testgen .ui .queries import user_queries
1012from testgen .ui .session import session
1113
1214RoleType = typing .Literal ["admin" , "data_quality" , "analyst" , "business" , "catalog" ]
1315
14- JWT_HASHING_KEY = "dk_signature_key"
1516AUTH_TOKEN_COOKIE_NAME = "dk_cookie_name" # noqa: S105
1617AUTH_TOKEN_EXPIRATION_DAYS = 1
1718DISABLED_ACTION_TEXT = "You do not have permissions to perform this action. Contact your administrator."
1819
1920LOG = logging .getLogger ("testgen" )
2021
2122
23+ def _get_jwt_hashing_key () -> bytes :
24+ try :
25+ return base64 .b64decode (settings .JWT_HASHING_KEY_B64 .encode ("ascii" ))
26+ except Exception as e :
27+ raise ValueError (
28+ "Error reading the JWT signing key from settings. Make sure you have a valid base 64 "
29+ "string assigned to the TG_JWT_HASHING_KEY environment variable."
30+ ) from e
31+
32+
2233def load_user_session () -> None :
2334 # Replacing this with st.context.cookies does not work
2435 # Because it does not update when cookies are deleted on logout
@@ -29,7 +40,7 @@ def load_user_session() -> None:
2940 token = cookies .get (AUTH_TOKEN_COOKIE_NAME )
3041 if token is not None :
3142 try :
32- token = jwt .decode (token , JWT_HASHING_KEY , algorithms = ["HS256" ])
43+ token = jwt .decode (token , _get_jwt_hashing_key () , algorithms = ["HS256" ])
3344 if token ["exp_date" ] > datetime .datetime .utcnow ().timestamp ():
3445 start_user_session (token ["name" ], token ["username" ])
3546 except Exception :
@@ -77,7 +88,11 @@ def get_auth_data():
7788
7889 return {
7990 "credentials" : {"usernames" : usernames },
80- "cookie" : {"expiry_days" : AUTH_TOKEN_EXPIRATION_DAYS , "key" : JWT_HASHING_KEY , "name" : AUTH_TOKEN_COOKIE_NAME },
91+ "cookie" : {
92+ "expiry_days" : AUTH_TOKEN_EXPIRATION_DAYS ,
93+ "key" : _get_jwt_hashing_key (),
94+ "name" : AUTH_TOKEN_COOKIE_NAME ,
95+ },
8196 "preauthorized" : {"emails" : preauthorized_list },
8297 }
8398
0 commit comments