-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
57 lines (44 loc) · 1.63 KB
/
auth.js
File metadata and controls
57 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// auth.js
// author : David Freiman
// influenced_by : https://github.com/jlwebster/node_api_auth_client
// license : GPL 3.0
var crypto = require('crypto');
var moment = require('moment');
var settings = require('./settings');
var exports = module.exports;
exports.auth = function(access_id, secret) {
this.access_id = access_id;
this.secret = secret;
this.sign_options = function(options, content_body, algorithm) {
var content_type = options.headers['Content-Type'];
if (isEmpty(content_type))
{
// Default to json
content_type = options.headers['Content-Type'] = 'application/json';
}
var path = options.path;
if (isEmpty(path))
{
// Default to the host's root
path = options.path = '/'
}
var algorithm = algorithm;
if (isEmpty(algorithm)) {
algorithm = options.algorithm = 'sha1';
}
var algorithm_label = settings.algorithm_labels[algorithm];
// var content_md5 = crypto.createHash('md5').update(JSON.stringify(content_body)).digest('base64');
var date = moment().utc().format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT';
var canonical_string = [options.method, content_type, '', path, date].join();
var auth_header_value = 'APIAuth' + algorithm_label + ' ' + this.access_id + ':' + crypto.createHmac(algorithm, this.secret).update(canonical_string).digest('base64');
//options.headers['Content-MD5'] = content_md5;
options.headers['DATE'] = date;
options.headers['Authorization'] = auth_header_value;
return options;
};
return this;
};
function isEmpty(value)
{
return value === null || value === '' || typeof value === 'undefined';
}