Skip to content

Instantly share code, notes, and snippets.

@christophermina
Created October 4, 2014 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophermina/18ea87f20145b68e5267 to your computer and use it in GitHub Desktop.
Save christophermina/18ea87f20145b68e5267 to your computer and use it in GitHub Desktop.
getSignedRequest: function(container, resourceKey, expirationTimeInSeconds, fn) {
if (_.isFunction(expirationTimeInSeconds)) {
fn = expirationTimeInSeconds;
expirationTimeInSeconds = 3600;
}
if (process.env.SL_STORAGE_URL == null) {
request.get(slConfigs.SL_STORAGE_AUTH_URL_V1, {
headers: {
'X-Auth-User':slConfigs.SL_STORAGE_USERNAME,
'X-Auth-Key':slConfigs.SL_STORAGE_PASS
}
}, function(err, value) {
if (value.statusCode == 200) {
var storageUrl = value.headers['x-storage-url'];
process.env.SL_STORAGE_URL = storageUrl;
return softlayerEngine.getSignedRequest(container, resourceKey, expirationTimeInSeconds, fn);
} else {
return fn("Could not retrieve SoftLayer Storage URL");
}
});
return;
}
var tempUrlKey = slConfigs.SL_STORAGE_TEMP_URL_KEY;
var method = 'GET';
var expires = Math.floor(Date.now() / 1000) + expirationTimeInSeconds;
var storageUrl = process.env.SL_STORAGE_URL;
var storageParts = url.parse(storageUrl);
var path = storageParts.pathname + "/" + container + "/" + resourceKey;
var body = method + '\n' + expires + '\n' + path;
var hash = crypto.createHmac('sha1', tempUrlKey).update(body).digest('hex');
var uri = storageParts.protocol + '//' + storageParts.host + path + '?temp_url_sig=' + hash + '&temp_url_expires=' + expires;
return fn(null, uri);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment