1
1
2
- import jwt
3
- import hashlib
4
- import uuid
5
-
6
- from urllib .parse import urlencode
7
-
8
2
from bravado .requests_client import Authenticator
9
3
10
4
11
5
class APIKeyAuthenticator (Authenticator ):
12
6
7
+ import jwt
8
+ import uuid
9
+
10
+ from urllib .parse import urlencode
11
+
12
+
13
13
MAPPER = "swg_mapper.json"
14
14
QUERY_PARAMS = ( "uuids" , "txids" , "identifiers" , "states" )
15
15
@@ -26,6 +26,8 @@ def __init__(
26
26
self .host = host
27
27
self .access_key = access_key
28
28
self .secret_key = secret_key
29
+ self .algorithms = self .jwt .algorithms
30
+ self .algo = "HS512"
29
31
30
32
31
33
def matches (self , url ):
@@ -49,27 +51,27 @@ def generate_payload(self, request):
49
51
50
52
payload = {
51
53
'access_key' : self .access_key ,
52
- 'nonce' : str (uuid .uuid4 ())
54
+ 'nonce' : str (self . uuid .uuid4 ())
53
55
}
54
56
if isinstance (data , dict ):
55
57
params .update (data )
56
58
if params :
57
59
query = self .generate_query (params )
58
60
59
- sha512 = hashlib . sha512 ( )
61
+ sha512 = self . get_hash_algo ( self . algo )
60
62
sha512 .update (query .encode ())
61
63
query_hash = sha512 .hexdigest ()
62
64
63
65
payload ["query_hash" ] = query_hash
64
- payload ["query_hash_alg" ] = "SHA512"
66
+ payload ["query_hash_alg" ] = sha512 . name
65
67
66
- jwt_token = jwt .encode (payload , self .secret_key )
68
+ jwt_token = self . jwt .encode (payload , self .secret_key , algorithm = self . algo )
67
69
authorize_token = f"Bearer { jwt_token } "
68
70
return authorize_token
69
71
70
72
71
73
def generate_query (self , params ):
72
- query = urlencode ({
74
+ query = self . urlencode ({
73
75
k : v
74
76
for k , v in params .items ()
75
77
if k .lower () not in APIKeyAuthenticator .QUERY_PARAMS
@@ -84,3 +86,10 @@ def generate_query(self, params):
84
86
])
85
87
query = f"{ query } &{ query_params } " if query else query_params
86
88
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