-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
227 lines (160 loc) · 5.97 KB
/
index.js
File metadata and controls
227 lines (160 loc) · 5.97 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use strict'
var schemesBuilder = require('./lib/schemes-builder'),
parser = require('./lib/parser'),
path = require('path'),
fs = require('fs');
this.authorizationSchemes = [];
function init(schemes) {
this.authorizationSchemes = schemesBuilder.build(schemes);
this.initialized = true;
}
function generateAuthorizationHeader(options, data, timestampDate) {
options = options || {};
data = data || '';
if (typeof data !== 'string') {
throw 'data must be a string';
}
var scheme = findScheme(module.exports.authorizationSchemes, options.schemeName);
var client = findClient(scheme, options.clientId);
var timestamp = generateTimestamp(scheme, timestampDate);
data = data + timestamp;
var signature = '';
if (scheme.isHasher) {
signature = scheme.hash(data, client);
}
else {
var publicKeyPathValid = (client.relativeOrAbsolutePathToPublicKey !== undefined && (typeof client.relativeOrAbsolutePathToPublicKey === "string") && client.relativeOrAbsolutePathToPublicKey.length > 0);
if (!publicKeyPathValid)
throw "relativeOrAbsolutePathToPublicKey is invalid";
signature = scheme.encrypt(data, client);
}
var schemeName = (typeof scheme.alias !== 'undefined') ? scheme.alias : scheme.scheme;
return schemeName + " clientId=" + client.clientId + timestamp + ";signature=" + signature;
}
function isAuthorized(authorizationHeader, data, timestampDate) {
var result = {
result: false,
schemeName: undefined,
clientId: undefined,
error: undefined
};
data = data || '';
if (typeof data !== 'string') {
result.error = 'data must be a string';
return result;
}
var parsedHeader = parser.parseAuthorizationHeader(authorizationHeader);
if (!parsedHeader) {
result.error = 'Authorization header is invalid';
return result;
}
var scheme;
try {
scheme = findScheme(module.exports.authorizationSchemes, parsedHeader.scheme);
}
catch (err) {
result.error = err;
return result;
}
result.schemeName = scheme.scheme;
var client;
try {
client = findClient(scheme, parsedHeader.clientId);
}
catch (err) {
result.error = err;
return result;
}
result.clientId = client.clientId;
if (!parsedHeader.signature || parsedHeader.signature.length == 0) {
result.error = 'signature is invalid';
return result;
}
if (scheme.useTimestamp && !parsedHeader.timestamp) {
result.error = "timestamp is required";
return result;
}
if (scheme.useTimestamp && new Date(parsedHeader.timestamp) == 'Invalid Date') {
result.error = "timestamp is invalid";
return result;
}
var timestamp = (scheme.useTimestamp ? generateTimestamp(scheme, new Date(parsedHeader.timestamp)) : '');
data = data + timestamp;
if (scheme.isHasher) {
var signature = scheme.hash(data, client);
if (parsedHeader.signature !== signature) {
result.error = "Signatures do not match";
return result;
}
}
else {
var privateKeyPathValid = (client.relativeOrAbsolutePathToPrivateKey !== undefined && (typeof client.relativeOrAbsolutePathToPrivateKey === "string") && client.relativeOrAbsolutePathToPrivateKey.length > 0);
if (!privateKeyPathValid) {
result.error = "relativeOrAbsolutePathToPrivateKey is invalid";
return result;
}
var decryptedData = scheme.decrypt(parsedHeader.signature, client);
if (data !== decryptedData) {
result.error = "decrypted data does not match data";
return result;
}
}
if (scheme.useTimestamp && typeof scheme.timestampValidationWindowInSeconds !== "undefined" && typeof scheme.timestampValidationWindowInSeconds === 'number') {
var differenceInSeconds = differenceBetweenDatesInSeconds(new Date(parsedHeader.timestamp), timestampDate || new Date());
if (differenceInSeconds > scheme.timestampValidationWindowInSeconds) {
result.error = "validation window has been breached";
return result;
}
}
result.result = true;
return result;
}
function authorized(getDataFunc) {
return function(req, res, next) {
var data = '';
if (getDataFunc)
data = getDataFunc(req);
var result = isAuthorized(req.headers['authorization'], data);
req.requestAuthorizationIsAuthorizedResult = result;
if (!result.result) {
res.status(401).end();
return;
}
next();
};
}
function generateTimestamp(scheme, timestampDate) {
var timestamp = '';
if (scheme.useTimestamp) {
timestampDate = timestampDate || new Date();
timestamp = ';timestamp=' + timestampDate.toISOString();
}
return timestamp;
}
function findScheme(schemes, schemeName) {
var matchingSchemes = schemes.filter(function(current) {
return current.scheme === schemeName || ((typeof current.alias !== 'undefined') && current.alias === schemeName);
});
if (matchingSchemes.length == 0)
throw "schemeName is not valid, has the request-authorization.init should be called or an invalid schemeName has been used";
return matchingSchemes[0];
}
function findClient(scheme, clientId) {
var matchingClients = scheme.clients.filter(function(current) {
return current.clientId === clientId;
});
if (matchingClients.length == 0)
throw "clientId is not valid";
return matchingClients[0];
}
function differenceBetweenDatesInSeconds(dateOne, dateTwo) {
var dif = dateOne.getTime() - dateTwo.getTime();
return Math.abs(dif / 1000);
}
module.exports = {
authorizationSchemes: this.authorizationSchemes,
init: init,
generateAuthorizationHeader: generateAuthorizationHeader,
isAuthorized: isAuthorized,
authorized: authorized
};