From 5ae848266558072c86a879d1047522beaa67acf1 Mon Sep 17 00:00:00 2001 From: Barrett Date: Wed, 30 Apr 2025 10:22:52 -0600 Subject: [PATCH] fix: handle bytes responses in auth API methods --- dimo/api/auth.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/dimo/api/auth.py b/dimo/api/auth.py index d581d65..9603443 100644 --- a/dimo/api/auth.py +++ b/dimo/api/auth.py @@ -2,6 +2,7 @@ from dimo.errors import check_type, check_optional_type from urllib.parse import urlencode from typing import Dict, Optional +import json class Auth: @@ -36,13 +37,18 @@ def generate_challenge( "address": address, } - return self._request( + response = self._request( "POST", "Auth", "/auth/web3/generate_challenge", data=urlencode(body), headers=headers, ) + + if isinstance(response, bytes): + response = json.loads(response.decode('utf-8')) + + return response def sign_challenge(self, message: str, private_key: str) -> str: check_type("message", message, str) @@ -78,13 +84,18 @@ def submit_challenge( encoded_data = urlencode(form_data) - return self._request( + response = self._request( "POST", "Auth", "/auth/web3/submit_challenge", data=encoded_data, headers=headers, ) + + if isinstance(response, bytes): + response = json.loads(response.decode('utf-8')) + + return response # Requires client_id, domain, and private_key. Address defaults to client_id. def get_dev_jwt( @@ -96,7 +107,18 @@ def get_dev_jwt( scope="openid email", response_type="code", ) -> Dict: - + """ + Generate a signed developer JWT in one step. + For testing, mocks and POCs. + + Args: + client_id (str): The Ethereum address of the client + domain (str): The domain name for the client + private_key (str): The private key to sign the challenge + + Returns: + dict: The authentication response containing access_token + """ check_type("client_id", client_id, str) check_type("domain", domain, str) check_type("private_key", private_key, str) @@ -109,6 +131,7 @@ def get_dev_jwt( headers = {"Content-Type": "application/x-www-form-urlencoded"} + # Generate a challenge challenge = self.generate_challenge( headers=headers, client_id=client_id, @@ -117,14 +140,21 @@ def get_dev_jwt( response_type=response_type, address=address, ) - + + if isinstance(challenge, bytes): + challenge = json.loads(challenge.decode('utf-8')) + sign = self.sign_challenge( message=challenge["challenge"], private_key=private_key, ) - + state = challenge["state"] signature = sign submit = self.submit_challenge(client_id, domain, state, signature, headers) + + if isinstance(submit, bytes): + submit = json.loads(submit.decode('utf-8')) + return submit