Registering with safe_launcher from python

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.

24 Likes

Brilliant contribution, thank you! :slight_smile:

5 Likes

Oh yes buddy that is excellent !! Who makes the c++ one ?

On my rust side I’m stuck with nfs create dir, hitting a 401 for some reason

1 Like

Excellent! Solid code too.

IIRC there’s at the moment no other way to talk to the launcher other than just sending a payload to the launcher, right?

3 Likes

For C++ there’d likely be several ones, based on people’s favorite dependency of the day for (async) networking,json,etc… no way it’d so easily fit on one page either. I don’t particularly look forward to writing one I already have to stare at C++ code all day :scorpion:

IIRC there’s at the moment no other way to talk to the launcher other than just sending a payload to the launcher, right?

One thing to try would be to use safe_ffi directly from your favorite language. But I think then you can only completely bypass the launcher and would have to implement the authentication to the network etc. Not sure though.

2 Likes

uuuuuuuh - just thought i’d mention i finally found the time to drink 1.5 beer and test your wrapper!

WOHOOOOW - safenet with python!!! :smiley: thank you very much =D :heart_eyes:

we’ll see if this leads somewhere Oo …

5 Likes

Anyone tested this with the more recent launchers? Maybe the Alpha one?

Might give it a shot today

1 Like

At least I haven’t tested this for a while.

Was trying to find ways to use this is a web app, possibly through Django.

Might just use it for desktop software instead.