Skip to content

Commit

Permalink
feat: inegrate fauna (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: James Riehl <[email protected]>
  • Loading branch information
agent-dominatrix and jrriehl authored Nov 24, 2023
1 parent 3dc343b commit 1840474
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand All @@ -29,6 +29,10 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- if: matrix.os == 'macos-latest' && matrix.python-version == '3.11'
name: Install build tools
run: |
brew install automake
- name: Install Poetry
uses: abatilo/actions-poetry@v2
Expand Down
50 changes: 35 additions & 15 deletions src/babble/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .config import AUTH_SERVER
from .crypto.identity import Identity
from .encoding import to_base64, from_base64


@dataclass
Expand All @@ -17,43 +18,62 @@ class TokenMetadata:
expires_at: datetime


def authenticate(identity: Identity) -> Tuple[str, TokenMetadata]:
def authenticate(identity: Identity, name: str = None) -> Tuple[str, TokenMetadata]:
r = requests.post(
f"{AUTH_SERVER}/request_token",
f"{AUTH_SERVER}/auth/login/wallet/challenge",
json={
"address": identity.address,
"public_key": identity.public_key,
"client_id": name if name else "uagent",
},
)
r.raise_for_status()
resp = r.json()

payload = r.json()["payload"]
payload = resp["challenge"]

# create the signature
signed_bytes, signature = identity.sign_arbitrary(payload.encode())
_, signature = identity.sign_arbitrary(payload.encode())

r = requests.post(
f"{AUTH_SERVER}/login",
json={
"public_key": identity.public_key,
"signed_bytes": signed_bytes,
"signature": signature,
login_request = {
"address": identity.address,
"public_key": {
"value": to_base64(bytes.fromhex(identity.public_key)),
"type": "tendermint/PubKeySecp256k1",
},
"nonce": resp["nonce"],
"challenge": resp["challenge"],
"signature": signature,
"client_id": name if name else "uagent",
"scope": "",
}

r = requests.post(
f"{AUTH_SERVER}/auth/login/wallet/verify",
json=login_request,
)
r.raise_for_status()

r = requests.post(
f"{AUTH_SERVER}/tokens",
json=r.json(),
)
r.raise_for_status()

# extract the token
token = str(r.json()["token"])
token = str(r.json()["access_token"])

# parse the token
token_data = jwt.decode(
token, algorithms=["RS*"], options={"verify_signature": False}
token,
algorithms=["RS*"],
options={"verify_signature": False},
issuer="fetch.ai",
)

# build the token metadata
metadata = TokenMetadata(
address=str(token_data["name"]),
public_key=str(token_data["pubkey"]),
address=identity.address,
public_key=from_base64(str(token_data["pk"])).hex(),
issued_at=datetime.fromtimestamp(token_data["iat"], timezone.utc),
expires_at=datetime.fromtimestamp(token_data["exp"], timezone.utc),
)
Expand Down
4 changes: 3 additions & 1 deletion src/babble/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
signed_obj_base64: str,
identity: Identity,
chain_id: str,
name: str = None,
):
_validate_address(delegate_address)

Expand All @@ -53,6 +54,7 @@ def __init__(
self._signed_obj_base64 = signed_obj_base64
self._identity = identity
self._chain_id = chain_id
self._name = name

# build and restore the delivered set
self._last_rx_timestamp = self._now()
Expand All @@ -70,7 +72,7 @@ def _update_authentication(self):
or self._token_metadata.expires_at
< self._now() - timedelta(seconds=EXPIRATION_BUFFER_SECONDS)
):
self._token, self._token_metadata = authenticate(self._identity)
self._token, self._token_metadata = authenticate(self._identity, self._name)

def __repr__(self):
return f"{self._delegate_address} ({self._identity.public_key})"
Expand Down
4 changes: 1 addition & 3 deletions src/babble/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

AUTH_SERVER = os.environ.get(
"AUTH_SERVER", "https://auth-attila.sandbox-london-b.fetch-ai.com"
)
AUTH_SERVER = os.environ.get("AUTH_SERVER", "https://accounts.fetch.ai/v1")
MEMORANDUM_SERVER = os.environ.get(
"MEMORANDUM_SERVER",
"https://messaging-server.sandbox-london-b.fetch-ai.com/graphql",
Expand Down

0 comments on commit 1840474

Please sign in to comment.