src/api/auth.js
function () {
    this.log('Authorizing...');
    // Generate new asymmetric key pair and nonce, e.g. public-key encryption
    var asymKeys  = nacl.box.keyPair(),
        asymNonce = nacl.randomBytes(nacl.box.nonceLength);

    // The payload for the request
    var authPayload = {
      app: this.app,
      publicKey: base64.fromByteArray(asymKeys.publicKey),
      nonce: base64.fromByteArray(asymNonce),
      permissions: this.permissions
    };

    // Proceed with request
    return this.Request
      .post('/auth')
      .body(authPayload)
      .execute()
      .then(function (json) {
        // We received the launcher's private key + encrypted symmetrical keys (i.e. private key encryption)
        var launcherPubKey = base64.toByteArray(json.publicKey);
        var messageWithSymKeys = base64.toByteArray(json.encryptedKey);

        // Decrypt the private key/nonce
        var key = nacl.box.open(messageWithSymKeys, asymNonce, launcherPubKey, asymKeys.secretKey);

        // Save auth data for future requests
        this._auth = {
          token: json.token,
          symKey: key.slice(0, nacl.secretbox.keyLength),
          symNonce: key.slice(nacl.secretbox.keyLength)
        };

        // Set the auth data for future requests
        this.storage.set(utils.serialize(this._auth));
        this.log('Authorized');
      }.bind(this), function(res) {
        this.log('Could not authorize.');
        this.storage.clear();
      }.bind(this));
  }
function () {
    this.log('Authorizing...');
    // Generate new asymmetric key pair and nonce, e.g. public-key encryption
    var asymKeys  = nacl.box.keyPair(),
        asymNonce = nacl.randomBytes(nacl.box.nonceLength);

    // The payload for the request
    var authPayload = {
      app: this.app,
      publicKey: base64.fromByteArray(asymKeys.publicKey),
      nonce: base64.fromByteArray(asymNonce),
      permissions: this.permissions
    };

    // Proceed with request
    return this.Request
      .post('/auth')
      .body(authPayload)
      .execute()
      .then(function (json) {
        // We received the launcher's private key + encrypted symmetrical keys (i.e. private key encryption)
        var launcherPubKey = base64.toByteArray(json.publicKey);
        var messageWithSymKeys = base64.toByteArray(json.encryptedKey);

        // Decrypt the private key/nonce
        var key = nacl.box.open(messageWithSymKeys, asymNonce, launcherPubKey, asymKeys.secretKey);

        // Save auth data for future requests
        this._auth = {
          token: json.token,
          symKey: key.slice(0, nacl.secretbox.keyLength),
          symNonce: key.slice(nacl.secretbox.keyLength)
        };

        // Set the auth data for future requests
        this.storage.set(utils.serialize(this._auth));
        this.log('Authorized');
      }.bind(this), function(res) {
        this.log('Could not authorize.');
        this.storage.clear();
      }.bind(this));
  }
function ( ) { this . log ( 'Authorizing...' ) ; // Generate new asymmetric key pair and nonce, e.g. public-key encryption var asymKeys = nacl . box . keyPair ( ) , asymNonce = nacl . randomBytes ( nacl . box . nonceLength ) ; // The payload for the request var authPayload = { app : this . app , publicKey : base64 . fromByteArray ( asymKeys . publicKey ) , nonce : base64 . fromByteArray ( asymNonce ) , permissions : this . permissions } ; // Proceed with request return this . Request . post ( '/auth' ) . body ( authPayload ) . execute ( ) . then ( function ( json ) { // We received the launcher's private key + encrypted symmetrical keys (i.e. private key encryption) var launcherPubKey = base64 . toByteArray ( json . publicKey ) ; var messageWithSymKeys = base64 . toByteArray ( json . encryptedKey ) ; // Decrypt the private key/nonce var key = nacl . box . open ( messageWithSymKeys , asymNonce , launcherPubKey , asymKeys . secretKey ) ; // Save auth data for future requests this . _auth = { token : json . token , symKey : key . slice ( 0 , nacl . secretbox . keyLength ) , symNonce : key . slice ( nacl . secretbox . keyLength ) } ; // Set the auth data for future requests this . storage . set ( utils . serialize ( this . _auth ) ) ; this . log ( 'Authorized' ) ; } . bind ( this ) , function ( res ) { this . log ( 'Could not authorize.' ) ; this . storage . clear ( ) ; } . bind ( this ) ) ; }
Requests authorization from the safe launcher.
@returns {*}
Requests authorization from the safe launcher .