-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
123 lines (99 loc) · 3.62 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var crypto = require('crypto');
var _ = require('underscore');
var XOauth = function (consumer_key, consumer_secret) {
this.consumer = {
key: consumer_key || 'anonymous',
secret: consumer_secret || 'anonymous'
};
};
/////////////// PUBLIC /////////////
XOauth.prototype.escapeAndJoin = function (elems) {
return elems.map(function (x) {
return encodeURIComponent(x);
}).join('&');
};
XOauth.prototype.formatUrlParams = function (params) {
var param_fragments = [];
_.each(_.sortBy(_.keys(params), function (key) {return key}), function (key) {
param_fragments.push(key + '=' + encodeURIComponent(params[key]));
});
return param_fragments.join('&');
};
XOauth.prototype.generateSignatureBaseString = function (method, request_url_base, params) {
return this.escapeAndJoin([method, request_url_base, this.formatUrlParams(params)]);
};
XOauth.prototype.generateHmacSha1Signature = function (text, key) {
return crypto.createHmac('sha1', key).update(text).digest('base64');
};
XOauth.prototype.generateOauthSignature = function (base_string, token_secret) {
var key = this.escapeAndJoin([this.consumer.secret, token_secret]);
return this.generateHmacSha1Signature(base_string, key);
};
XOauth.prototype.generateCommonOauthParams = function (nonce, timestamp) {
var params = {};
params['oauth_consumer_key'] = this.consumer.key
if (nonce) {
params['oauth_nonce'] = nonce;
}
else {
params['oauth_nonce'] = '' + Math.floor(Math.random() * (Math.pow(2, 64) - 1));
}
params['oauth_signature_method'] = 'HMAC-SHA1';
params['oauth_version'] = '1.0';
if (timestamp) {
params['oauth_timestamp'] = timestamp;
}
else {
params['oauth_timestamp'] = Math.floor((new Date().getTime()) / 1000);
}
return params;
};
XOauth.prototype.generateXOauthString = function (access_token, user, proto, xoauth_requestor_id, nonce, timestamp) {
var method = 'GET',
url_params = {},
oauth_params = {},
signed_params,
request_url_base,
base_string,
signature,
formatted_params = [],
param_list,
request_url,
preencoded;
if (xoauth_requestor_id) {
url_params['xoauth_requestor_id'] = xoauth_requestor_id;
}
oauth_params = this.generateCommonOauthParams(nonce, timestamp);
if (access_token.key) {
oauth_params['oauth_token'] = access_token.key;
}
signed_params = _.extend(oauth_params, url_params);
request_url_base = 'https://mail.google.com/mail/b/' + user + '/' + proto + '/';
base_string = this.generateSignatureBaseString(method, request_url_base, signed_params);
signature = this.generateOauthSignature(base_string, access_token.secret);
oauth_params['oauth_signature'] = signature;
_.each(_.sortBy(_.keys(oauth_params), function (k) {return k}), function (k) {
var v = oauth_params[k];
formatted_params.push(k + '="' + encodeURIComponent(v) + '"');
});
param_list = formatted_params.join(',');
if (url_params.length > 0) {
request_url = request_url_base + '?' + this.formatUrlParams(url_params);
}
else {
request_url = request_url_base;
}
preencoded = [method, request_url, param_list].join(' ');
return preencoded;
};
XOauth.prototype.generateIMAPXOauthString = function (user, oauth_token, oauth_token_secret, xoauth_requestor_id, nonce, timestamp) {
var access_token = {
key: oauth_token,
secret: oauth_token_secret
},
xoauth_string, encoded_xoauth_string;
xoauth_string = this.generateXOauthString(access_token, user, 'imap', xoauth_requestor_id, nonce, timestamp);
encoded_xoauth_string = (new Buffer(xoauth_string)).toString('base64');
return encoded_xoauth_string;
};
module.exports = XOauth;