Skip to content

Instantly share code, notes, and snippets.

@hitman401
Created March 16, 2016 11:43
Show Gist options
  • Save hitman401/e030b2bd091ce3bef3a2 to your computer and use it in GitHub Desktop.
Save hitman401/e030b2bd091ce3bef3a2 to your computer and use it in GitHub Desktop.
temp_forum
// libsodium wrappers is used for crypto
var libsodium = require('libsodium-wrappers');
var httpRequest = require('request');
// Generate Assymetric Key pairs
var assymetricKeys = libsodium.crypto_box_keypair();
// Generate random Nonce
var nonce = libsodium.randombytes_buf(libsodium.crypto_box_NONCEBYTES);;
// Creating the authorisation request payload
var payload = {
app: {
name: 'Demo App',
version: '0.0.1',
vendor: 'maidsafe',
id: 'org.maidsafe.demo'
},
// Converting assymetric public key to base64 string
publicKey: new Buffer(assymetricKeys.publicKey).toString('base64'),
// Converting nonce to base64 string
nonce: new Buffer(nonce).toString('base64'),
// List of permissions requested
permissions: ['SAFE_DRIVE_ACCESS']
};
// Preparing the request object
var request = {
url: 'http://localhost:8100/auth',
headers: {
'Content-Type': 'application/json'
},
json: payload
};
// Handle the response recieved from the launcher
var onAuthResponse = function(err, response) {
if (err) {
throw err;
}
if (response.statusCode !== 200) {
throw new Error('Failed with error code: ' + response.statusCode);
}
// The encrypted symmetric key recieved as base64 string is converted to Uint8Array
var cipherText = new Uint8Array(new Buffer(response.body.encryptedKey, 'base64'));
// The asymmetric public key of launcher recieved as base64 string is converted to Uint8Array
var publicKey = new Uint8Array(new Buffer(response.body.publicKey, 'base64'));
// the cipher message is decrypted using the assymetric private key of application and the public key of launcher
var data = libsodium.crypto_box_open_easy(cipherText, nonce, publicKey, assymetricKeys.privateKey);
// The first segment of the data will have the symmetric key
var symmetricKey = data.slice(0, libsodium.crypto_secretbox_KEYBYTES);
// The second segment of the data will have the nonce to be used
var symmetricNonce = data.slice(libsodium.crypto_secretbox_KEYBYTES);
// Authorisation token
var token = response.body.token;
// List of persmissions approved by the user
var permissions = response.body.permissions;
// Creating the payload
var filePath = '/private/snapshot_blob.bin';
var isPathShared = false;
// Change if the query parameters are to be sent
var appendQueryParams = false;
// Preparing the request options with the auth token
var request = {
url: 'http://localhost:8100/nfs/file/' + encodeURIComponent(filePath) + '/' + isPathShared,
auth: {
bearer: token
}
};
// send query parameters
// The query parameters are encrypted
if (appendQueryParams) {
var queryParams = 'offset=0';
request.url += '?';
var encryptedParams = libsodium.crypto_secretbox_easy(queryParams, symmetricNonce, symmetricKey).toString('base64');
request.url += encryptedParams;
}
// Handling the response
var onResponse = function(err, res, body) {
if (err) {
return console.log('Err:', err);
}
if (res.statusCode === 400) {
return console.log('Bad request');
}
if (res.statusCode === 401) {
return console.log('Unauthorised');
}
body = new Buffer(body, 'base64');
// Decrypt the file content
var decryptedData = libsodium.crypto_secretbox_open_easy(new Uint8Array(body), symmetricNonce, symmetricKey);
console.log('File Content:', new Buffer(decryptedData).toString());
};
// Send get file request
httpRequest.get(request, onResponse);
};
// Sending authorisation request
httpRequest.post(request, onAuthResponse);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment