def run()

in api/pages/node/register.py [0:0]


def run(API, environ, indata, session):
    
    method = environ['REQUEST_METHOD']
    
    # Registering a new node?
    if method == "POST":
        hostname = indata['hostname']
        pubkey_pem = indata['pubkey']
        nodeversion = indata['version'] # TODO: Check for incompatibilities!
        
        # Try loading the PEM
        try:
            pubkey = plugins.crypto.loads(pubkey_pem)
        except:
            raise API.exception(400, "Bad PEM payload passed from client!")
        
        # Okay, we have what we need for now. Register potential node and gen an API key
        node = plugins.registry.node(session)
        node.hostname = hostname
        node.pem = pubkey_pem
        node.version = nodeversion
        node.ip = environ.get('HTTP_X_FORWARDED_FOR', environ.get('REMOTE_ADDR', '0.0.0.0'))
        
        # Encrypt API key with the pub key we just got. base64 encode the result
        apikey_crypt = str(base64.b64encode(plugins.crypto.encrypt(pubkey, node.apikey)), 'ascii')
        node.save()
        
        yield json.dumps({"encrypted": True, "key": apikey_crypt}, indent = 2)
        return

    
    # Finally, if we hit a method we don't know, balk!
    yield API.exception(400, "I don't know this request method!!")