'use strict';

/**
 * Module dependencies
 */
var io = require('socket.io-client');
var NodeRSA = require('node-rsa');

/**
 * Authentication Controller
 *
 * This is merely meant as an example of how your Authentication controller
 * should look. It currently includes the minimum amount of functionality for
 * the basics of Passport.js to work.
 */
var SocketController = {
  /**
   * Log out a user and return them to the homepage
   *
   * Passport exposes a logout() function on req (also aliased as logOut()) that
   * can be called from any route handler which needs to terminate a login
   * session. Invoking logout() will remove the req.user property and clear the
   * login session (if any).
   *
   * For more information on logging out users in Passport.js, check out:
   * http://passportjs.org/guide/logout/
   *
   * @param {Object} req
   * @param {Object} res
   */
  connect: function(req, res) {
    var socket = io.connect('http://localhost:1337', {
      reconnect: true
    });

    socket.on('connect', function(data) {
      console.log('Local server connected');

      sails.RSAKey = new NodeRSA({
        b: 2048
      });

      socket.emit('associateKey', {
        "appId": 1,
        "publicKey": sails.RSAKey.exportKey('public')
      });
    });

    socket.on('associatedKey', function(data) {
      console.log(data);

      var encrypted = {
        "appId": 1,
        "appName": "Coucou",
        "encrypted": sails.RSAKey.encryptPrivate({
          "token": "77baeb53422ca9b9d5b3be10d915cbbf"
        }, 'base64')
      };

      socket.emit('checkApp', encrypted);
    });

    socket.on('authorized', function(data) {
      // That's ok
      console.log(data);
    });

    socket.on('reload', function(data) {
      // If data.appId, you have to update the appId
      // and reload the server after
      console.log(data);
    });

    socket.on('err', function(data) {
      console.log(data.text);
    });

    res.send({});
  }
};

module.exports = SocketController;
@@ -989,32 +989,56 @@
 : true%0A    %7D);%0A%0A
+    sails.rsaKey = %7B%7D;%0A%0A
     socket.on('c
@@ -1115,28 +1115,60 @@
 %0A%0A      
-sails.RSAKey
+// Generate RSA key%0A      sails.rsaKey.local
  = new N
@@ -1226,89 +1226,194 @@
 it('
-associateKey', %7B%0A        %22appId%22: 1,%0A        %22p
+getPublicKey', null);%0A    %7D);%0A%0A    socket.on('gotPublicKey', function(publicKey) %7B%0A      // Import SaaS public key%0A      var saasP
 ublic
-Key%22: sails.RSAKey.ex
+key = new NodeRSA();%0A      saasPublickey.im
 portKey(
 'pub
@@ -1412,110 +1412,113 @@
 Key(
-'public')%0A      %7D);%0A    %7D);%0A%0A    socket.on('associatedKey', function(data) %7B%0A      console.log(data);%0A
+publicKey, 'public');%0A%0A      sails.rsaKey.saas = saasPublickey;%0A%0A      // Generate object to send to SaaS
 %0A   
@@ -1524,25 +1524,22 @@
     var 
-encrypted
+object
  = %7B%0A   
@@ -1597,176 +1597,318 @@
    %22
-encrypted%22: sails.RSAKey.encryptPrivate(%7B%0A          %22token%22: %2277baeb53422ca9b9d5b3be10d915cbbf%22%0A        %7D, 'base64')%0A      %7D;%0A%0A      socket.emit('checkApp', encrypted);
+publicKey%22: sails.rsaKey.local.exportKey('public'),%0A        %22secretKey%22: %220123456789%22,%0A        %22token%22: %22azertyuiop%22,%0A        %22env%22: process.env.NODE_ENV%0A      %7D;%0A%0A      socket.emit('check', sails.rsaKey.saas.encrypt(object));%0A    %7D);%0A%0A    socket.on('associatedKey', function(data) %7B%0A      console.log(data);%0A%0A
 %0A   

Update to consider back-end response