@yawetse/pkgcloud
Version:
An infrastructure-as-a-service agnostic cloud library for node.js
57 lines (46 loc) • 1.49 kB
JavaScript
/*
* auth.js: Utilities for authenticating with multiple cloud providers
*
* (C) 2011-2012 Charlie Robbins, Ken Perkins, Ross Kukulinski & the Contributors.
*
*/
var httpSignature = require('./http-signature'),
azureSignature = require('./azure-signature');
var auth = exports;
auth.basic = function basicAuth(req) {
var credentials = this.credentials
|| this.config.username + ':' + this.config.password;
req.headers = req.headers || {};
req.headers.authorization = [
'Basic',
new Buffer(credentials).toString('base64')
].join(' ');
};
// Add Account number for requests to rackspace API
auth.accountId = function (req) {
req.headers = req.headers || {};
if (this.config.accountNumber) {
req.headers['x-auth-project-id'] = this.config.accountNumber;
}
};
function signatureGenerator(sign) {
return function signatureAuth(req, keys) {
keys = keys || this.config;
sign.call(this, req, {
key: keys.key,
keyId: keys.keyId
});
};
}
function azureSignatureGenerator(sign) {
return function azureSignatureGenerator(req, keys) {
keys = keys || this.azureKeys;
sign.call(this, req, keys);
};
}
auth.httpSignature = signatureGenerator(httpSignature.sign);
auth.azure = {
managementSignature: azureSignatureGenerator(azureSignature.managementSignature),
storageSignature: azureSignatureGenerator(azureSignature.storageSignature),
tablesSignature: azureSignatureGenerator(azureSignature.tablesSignature)
};