Not sure if this was done before, but Succes in registering with rust :) inspired me to do this in Python.
This is a literal conversion of the example in /auth
#!/usr/bin/env python3
# Translation of https://maidsafe.readme.io/docs/auth into Python 3
# Requires: pysodium, a libsodium wrapper used for crypto
# cannot use nacl-python3 because it doesn't have crypto_box_*_easy
# and pysodium unfortulately currently needs a custom fix otherwise
# you wil get an unicode decode error:
# https://github.com/stef/pysodium/pull/36
import pysodium
import base64, json, codecs
from urllib.request import Request,urlopen
from urllib.error import HTTPError
# Generate Assymetric Key pairs
(ourPublicKey, ourPrivateKey) = pysodium.crypto_box_keypair()
# Generate random Nonce
nonce = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES)
# Creating the authorisation request payload
payload = {
'app': {
'name': 'Demo App',
'version': '0.0.1',
'vendor': 'maidsafe',
'id': 'org.maidsafe.demo'
},
# Converting assymetric public key to base64 string
'publicKey': base64.b64encode(ourPublicKey).decode(),
# Converting nonce to base64 string
'nonce': base64.b64encode(nonce).decode(),
# List of permissions requested
'permissions': [] # ['SAFE_DRIVE_ACCESS']
}
req = Request('http://localhost:8100/auth',
json.dumps(payload).encode(),
{'Content-Type': 'application/json'})
try:
result = urlopen(req)
except HTTPError as e:
if e.code == 401:
print('Authorization was denied')
else:
print(e)
else:
obj = json.load(codecs.getreader('utf-8')(result))
print(obj)
# The encrypted symmetric key recieved as base64 string is converted to bytes
cipherText = base64.b64decode(obj['encryptedKey'])
# The asymmetric public key of launcher recieved as base64 string is converted to bytes
publicKey = base64.b64decode(obj['publicKey'])
# the cipher message is decrypted using the assymetric private key of application and the public key of launcher
data = pysodium.crypto_box_open_easy(cipherText, nonce, publicKey, ourPrivateKey)
# The first segment of the data will have the symmetric key
symmetricKey = data[0:pysodium.crypto_secretbox_KEYBYTES]
# The second segment of the data will have the nonce to be used
symmetricNonce = data[pysodium.crypto_secretbox_KEYBYTES:]
# Authorisation token
token = obj['token']
# List of persmissions approved by the user
permissions = obj['permissions']
Unfortunately none of the Python libsodium wrappers could do this as-is, so you need pysodium + a patch right now: Remove .decode() from crypto_box_open_easy by laanwj · Pull Request #36 · stef/pysodium · GitHub . Hopefully this will be included in the next version of pysodium.