Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions dimo/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
Loading