Skip to content

Commit 6b752a3

Browse files
authored
Merge pull request DIMO-Network#23 from DIMO-Network/fix/auth-bytes-handling
fix: handle bytes responses in auth API methods
2 parents 52b76c1 + 5ae8482 commit 6b752a3

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

dimo/api/auth.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from dimo.errors import check_type, check_optional_type
33
from urllib.parse import urlencode
44
from typing import Dict, Optional
5+
import json
56

67

78
class Auth:
@@ -36,13 +37,18 @@ def generate_challenge(
3637
"address": address,
3738
}
3839

39-
return self._request(
40+
response = self._request(
4041
"POST",
4142
"Auth",
4243
"/auth/web3/generate_challenge",
4344
data=urlencode(body),
4445
headers=headers,
4546
)
47+
48+
if isinstance(response, bytes):
49+
response = json.loads(response.decode('utf-8'))
50+
51+
return response
4652

4753
def sign_challenge(self, message: str, private_key: str) -> str:
4854
check_type("message", message, str)
@@ -78,13 +84,18 @@ def submit_challenge(
7884

7985
encoded_data = urlencode(form_data)
8086

81-
return self._request(
87+
response = self._request(
8288
"POST",
8389
"Auth",
8490
"/auth/web3/submit_challenge",
8591
data=encoded_data,
8692
headers=headers,
8793
)
94+
95+
if isinstance(response, bytes):
96+
response = json.loads(response.decode('utf-8'))
97+
98+
return response
8899

89100
# Requires client_id, domain, and private_key. Address defaults to client_id.
90101
def get_dev_jwt(
@@ -96,7 +107,18 @@ def get_dev_jwt(
96107
scope="openid email",
97108
response_type="code",
98109
) -> Dict:
99-
110+
"""
111+
Generate a signed developer JWT in one step.
112+
For testing, mocks and POCs.
113+
114+
Args:
115+
client_id (str): The Ethereum address of the client
116+
domain (str): The domain name for the client
117+
private_key (str): The private key to sign the challenge
118+
119+
Returns:
120+
dict: The authentication response containing access_token
121+
"""
100122
check_type("client_id", client_id, str)
101123
check_type("domain", domain, str)
102124
check_type("private_key", private_key, str)
@@ -109,6 +131,7 @@ def get_dev_jwt(
109131

110132
headers = {"Content-Type": "application/x-www-form-urlencoded"}
111133

134+
# Generate a challenge
112135
challenge = self.generate_challenge(
113136
headers=headers,
114137
client_id=client_id,
@@ -117,14 +140,21 @@ def get_dev_jwt(
117140
response_type=response_type,
118141
address=address,
119142
)
120-
143+
144+
if isinstance(challenge, bytes):
145+
challenge = json.loads(challenge.decode('utf-8'))
146+
121147
sign = self.sign_challenge(
122148
message=challenge["challenge"],
123149
private_key=private_key,
124150
)
125-
151+
126152
state = challenge["state"]
127153
signature = sign
128154

129155
submit = self.submit_challenge(client_id, domain, state, signature, headers)
156+
157+
if isinstance(submit, bytes):
158+
submit = json.loads(submit.decode('utf-8'))
159+
130160
return submit

0 commit comments

Comments
 (0)