forked from AzureAD/azure-activedirectory-library-for-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication-parameters.ts
344 lines (320 loc) · 11.1 KB
/
authentication-parameters.ts
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* @copyright
* Copyright © Microsoft Open Technologies, Inc.
*
* All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: *www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
* ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
* PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
*
* See the Apache License, Version 2.0 for the specific language
* governing permissions and limitations under the License.
*/
'use strict';
/* Directive tells jshint that suite and test are globals defined by mocha */
/* global suite */
/* global test */
import * as assert from "assert";
import * as nock from "nock";
import * as url from "url";
import * as adal from "../lib/adal";
const util = require('./util/util');
const cp: any = util.commonParameters;
suite('authentication-parameters', function () {
function runTestCases(testData: any[], testFunc: Function) {
for (var i = 0; i < testData.length; i++) {
var parameters;
var error = null;
var testCase = testData[i];
var testInput = testCase[0];
var testParameters = testCase[1];
try {
parameters = testFunc(testInput);
} catch (err) {
error = err;
}
var assertPrefixMsg = 'Test case: ' + i + ' - ';
if (testParameters) {
var stack = !error || error.stack;
assert(!error, assertPrefixMsg + 'Parse failed but should have succeeded. ' + stack);
assert(parameters.authorizationUri === testParameters.authorizationUri, assertPrefixMsg + 'Parsed authorizationUri did not match expected value.: ' + parameters.authorizationUri);
assert(parameters.resource === testParameters.resource, assertPrefixMsg + 'Parsed resource did not match expected value.: ' + parameters.resource);
} else {
assert(error, assertPrefixMsg + 'Parse succeeded but should have failed.');
}
}
}
test('create-from-header', function (done) {
var testData = [
[
'Bearer authorization_uri="test,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="test"',
{
'authorizationUri': 'test,lkfj,;l,',
'resource': 'clark, &^()- q32,shark',
}
],
[
'Bearer resource="clark, &^()- q32,shark", authorization_uri="test,lkfj,;l,"',
{
'authorizationUri': 'test,lkfj,;l,',
'resource': 'clark, &^()- q32,shark',
}
],
[
'Bearer authorization_uri="' + cp.authorityTenant + '", resource="' + cp.resource + '"',
{
'authorizationUri': cp.authorityTenant,
'resource': cp.resource,
}
],
[
'Bearer authorization_uri="' + cp.authorizeUrl + '", resource="' + cp.resource + '"',
{
'authorizationUri': cp.authorizeUrl,
'resource': cp.resource,
}
],
// Add second = sign on first pair.
[
'Bearer authorization_uri=="test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Add second = sign on second pair.
[
'Bearer authorization_uri="test,lkfj,;l,", resource=="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Add second quote on first pair.
[
'Bearer authorization_uri=""test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Add second quote on second pair.
[
'Bearer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark"",fruitcake="f" , f="test"',
null
],
// Add trailing quote.
[
'Bearer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test""',
null
],
// Add trailing comma at end of string.
[
'Bearer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test",',
null
],
// Add second comma between 2 and 3 pairs.
[
'Bearer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" ,, f="test"',
null
],
// Add second comma between 1 and 2 pairs.
[
'Bearer authorization_uri=test,lkfj,;l,", , resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Add random letter between Bearer and first pair.
[
'Bearer f authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Add random letter between 2 and 3 pair.
[
'Bearer authorization_uri=test,lkfj,;l,", a resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Add random letter between 3 and 2 pair.
[
'Bearer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" a, f="test"',
null
],
// Mispell Bearer
[
'Berer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Missing resource.
[
'Bearer authorization_uri="test,lkfj,;l,"',
{
'authorizationUri': 'test,lkfj,;l,'
}
],
// Missing authoritzation uri.
[
'Bearer resource="clark, &^()- q32,shark",fruitcake="f" , f="test"',
null
],
// Boris's test.
[
'Bearer test="bar" ANYTHING HERE, ANYTHING PRESENT HERE, test1="bar1"',
null
],
[
'Bearerauthorization_uri="authuri", resource="resourceHere"',
null
],
];
runTestCases(testData, adal.createAuthenticationParametersFromHeader);
done();
});
test('create-from-response', function (done) {
var testData = [
[
{
status: 401,
headers: { 'www-authenticate': 'Bearer authorization_uri="test,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="test"' }
},
{
'authorizationUri': 'test,lkfj,;l,',
'resource': 'clark, &^()- q32,shark',
}
],
[
{
status: 200,
headers: { 'www-authenticate': 'Bearer authorization_uri="test,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="test"' }
},
null
],
[
{
status: 401
},
null
],
[
{
status: 401,
headers: { 'test': 'this is not the www-authenticate header' }
},
null
],
[
{
status: 401,
headers: { 'www-authenticate': 'Berer authorization_uri=test,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="test"' }
},
null
],
[
{
status: 401,
headers: { 'www-authenticate': null }
},
null
],
[
{
headers: { 'www-authenticate': null }
},
null
],
[
null,
null
]
];
runTestCases(testData, adal.createAuthenticationParametersFromResponse);
done();
});
var testHost = 'https://this.is.my.domain.com';
var testPath = '/path/to/resource';
var testQuery = 'a=query&string=really';
var testUrl = testHost + testPath + '?' + testQuery;
test('create-from-url-happy-path-string-url', function (done) {
var getResource = nock(testHost)
.filteringPath(function (path) {
return util.removeQueryStringIfMatching(path, testQuery);
})
.get(testPath)
.reply(401, 'test',
{ 'www-authenticate': 'Bearer authorization_uri="test,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="test"' }
);
util.matchStandardRequestHeaders(getResource);
adal.createAuthenticationParametersFromUrl(testUrl, function (err, parameters) {
if (!err) {
var testParameters = {
'authorizationUri': 'test,lkfj,;l,',
'resource': 'clark, &^()- q32,shark',
};
assert(parameters.authorizationUri === testParameters.authorizationUri, 'Parsed authorizationUri did not match expected value.: ' + parameters.authorizationUri);
assert(parameters.resource === testParameters.resource, 'Parsed resource did not match expected value.: ' + parameters.resource);
getResource.done();
}
done(err);
});
});
test('create-from-url-happy-path-url-object', function (done) {
var getResource = nock(testHost)
.filteringPath(function (path) {
return util.removeQueryStringIfMatching(path, testQuery);
})
.get(testPath)
.reply(401, 'test',
{ 'www-authenticate': 'Bearer authorization_uri="test,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="test"' }
);
util.matchStandardRequestHeaders(getResource);
var urlObj = url.parse(testUrl);
adal.createAuthenticationParametersFromUrl(urlObj as any, function (err, parameters) {
getResource.done();
if (!err) {
var testParameters = {
'authorizationUri': 'test,lkfj,;l,',
'resource': 'clark, &^()- q32,shark',
};
assert(parameters.authorizationUri === testParameters.authorizationUri, 'Parsed authorizationUri did not match expected value.: ' + parameters.authorizationUri);
assert(parameters.resource === testParameters.resource, 'Parsed resource did not match expected value.: ' + parameters.resource);
}
done(err);
});
});
test('create-from-url-bad-object', function (done) {
adal.createAuthenticationParametersFromUrl({} as any, function (err) {
assert(err, 'Did not receive expected error');
done();
});
});
test('create-from-url-not-passed', function (done) {
adal.createAuthenticationParametersFromUrl(null as any, function (err) {
assert(err, 'Did not receive expected error');
done();
});
});
test('create-from-url-no-header', function (done) {
var getResource = nock(testHost)
.filteringPath(function (path) {
return util.removeQueryStringIfMatching(path, testQuery);
})
.get(testPath)
.reply(401, 'test');
util.matchStandardRequestHeaders(getResource);
adal.createAuthenticationParametersFromUrl(testUrl, function (err) {
getResource.done();
assert(err, 'Did not receive expected error');
assert(err.message.indexOf('header') >= 0, 'Error did not include message about missing header');
done();
});
});
// This tests enables actual network connections but then attempts a connection to
// a completely invalid address. This causes a the node request to fail with
// an error rather than returning an HTTP error of some sort.
test('create-from-url-network-error', function (done) {
this.timeout(5000);
nock.enableNetConnect();
adal.createAuthenticationParametersFromUrl('https://0.0.0.0/test', function (err) {
assert(err, 'Did not receive expected error');
nock.disableNetConnect();
done();
});
});
});