11
2- import jwt
3- import hashlib
4- import uuid
5-
6- from urllib .parse import urlencode
7-
82from bravado .requests_client import Authenticator
93
104
115class APIKeyAuthenticator (Authenticator ):
126
7+ import jwt
8+ import uuid
9+
10+ from urllib .parse import urlencode
11+
12+
1313 MAPPER = "swg_mapper.json"
1414 QUERY_PARAMS = ( "uuids" , "txids" , "identifiers" , "states" )
1515
@@ -26,6 +26,8 @@ def __init__(
2626 self .host = host
2727 self .access_key = access_key
2828 self .secret_key = secret_key
29+ self .algorithms = self .jwt .algorithms
30+ self .algo = "HS512"
2931
3032
3133 def matches (self , url ):
@@ -49,27 +51,27 @@ def generate_payload(self, request):
4951
5052 payload = {
5153 'access_key' : self .access_key ,
52- 'nonce' : str (uuid .uuid4 ())
54+ 'nonce' : str (self . uuid .uuid4 ())
5355 }
5456 if isinstance (data , dict ):
5557 params .update (data )
5658 if params :
5759 query = self .generate_query (params )
5860
59- sha512 = hashlib . sha512 ( )
61+ sha512 = self . get_hash_algo ( self . algo )
6062 sha512 .update (query .encode ())
6163 query_hash = sha512 .hexdigest ()
6264
6365 payload ["query_hash" ] = query_hash
64- payload ["query_hash_alg" ] = "SHA512"
66+ payload ["query_hash_alg" ] = sha512 . name
6567
66- jwt_token = jwt .encode (payload , self .secret_key )
68+ jwt_token = self . jwt .encode (payload , self .secret_key , algorithm = self . algo )
6769 authorize_token = f"Bearer { jwt_token } "
6870 return authorize_token
6971
7072
7173 def generate_query (self , params ):
72- query = urlencode ({
74+ query = self . urlencode ({
7375 k : v
7476 for k , v in params .items ()
7577 if k .lower () not in APIKeyAuthenticator .QUERY_PARAMS
@@ -84,3 +86,10 @@ def generate_query(self, params):
8486 ])
8587 query = f"{ query } &{ query_params } " if query else query_params
8688 return query
89+
90+
91+ def get_hash_algo (self , algo ):
92+ algorithms = self .algorithms .get_default_algorithms ()
93+ algo = algorithms .get (algo , "HS512" )
94+ hash_algo = algo .hash_alg ()
95+ return hash_algo
0 commit comments