forked from AzureAD/azure-activedirectory-library-for-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-credential.ts
295 lines (248 loc) · 11.2 KB
/
client-credential.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
/*
* @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 */
/* global setup */
/* global teardown */
import * as _ from "underscore";
import * as assert from "assert";
const util = require('./util/util');
const cp = util.commonParameters;
const testRequire = util.testRequire;
import * as adal from "../lib/adal";
const AuthenticationContext = adal.AuthenticationContext;
const SelfSignedJwt = testRequire('self-signed-jwt');
/**
* Tests AuthenticationContext.acquireTokenWithClientCredentials
*/
suite('client-credential', function() {
setup(function() {
util.resetLogging();
util.clearStaticCache();
});
teardown(function() {
util.resetLogging();
util.clearStaticCache();
});
test('happy-path', function(done) {
var responseOptions = { noRefresh : true };
var response = util.createResponse(responseOptions);
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(200, response.wireResponse);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(response.resource, cp.clientId, cp.clientSecret, function (err, tokenResponse) {
if (!err) {
assert(util.isMatchTokenResponse(response.cachedResponse, tokenResponse), 'The response did not match what was expected');
tokenRequest.done();
}
done(err);
});
});
// Tests happy-path followed by an additional call to acquireTokenWithClientCredentials that should
// be served from the cache.
test('happy-path-cached-token', function(done) {
var responseOptions = { noRefresh : true };
var response = util.createResponse(responseOptions);
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(200, response.wireResponse);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(response.resource, cp.clientId, cp.clientSecret, function (err, tokenResponse) {
if (err) {
done(err);
return;
}
assert(util.isMatchTokenResponse(response.cachedResponse, tokenResponse), 'The response did not match what was expected');
tokenRequest.done();
context.acquireTokenWithClientCredentials(response.resource, cp.clientId, cp.clientSecret, function (err, tokenResponse) {
if (!err) {
assert(util.isMatchTokenResponse(response.cachedResponse, tokenResponse), 'The cached response did not match what was expected');
}
done(err);
});
});
});
// Tests happy path plus a call to the cache only function acquireToken which should find the token from the
// previous call to acquireTokenWithClientCredentials.
test('happy-path-cached-token2', function(done) {
var responseOptions = { noRefresh : true };
var response = util.createResponse(responseOptions);
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(200, response.wireResponse);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(response.resource, cp.clientId, cp.clientSecret, function (err, tokenResponse) {
if (err) {
done(err);
return;
}
assert(util.isMatchTokenResponse(response.cachedResponse, tokenResponse), 'The response did not match what was expected');
tokenRequest.done();
var nullUser = null;
var context2 = new AuthenticationContext(cp.authUrl);
context2.acquireToken(response.resource, nullUser as any, cp.clientId, function (err, tokenResponse) {
if (!err) {
assert(util.isMatchTokenResponse(response.cachedResponse, tokenResponse), 'The cached response did not match what was expected');
}
done(err);
});
});
});
test('no-callback', function(done) {
var context = new AuthenticationContext(cp.authorityTenant);
var argumentError;
try {
context.acquireTokenWithClientCredentials(cp.resource, cp.clientId, cp.clientSecret, null as any);
} catch(err) {
argumentError = err;
}
assert(argumentError, 'Did not receive expected error');
assert(argumentError.message.indexOf('callback') >= 0, 'Error does not appear to be specific to callback parameter.');
done();
});
test('no-arguments', function(done) {
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCredentials(null as any, null as any, null as any, function(err) {
assert(err, 'Did not receive expected error.');
assert(err.message.indexOf('parameter') >= 0, 'Error was not specific to a parameter.');
done();
});
});
test('no-client-secret', function(done) {
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCredentials(cp.resource, cp.clientId, null as any, function(err) {
assert(err, 'Did not receive expected error.');
assert(err.message.indexOf('parameter') >= 0, 'Error was not specific to a parameter.');
done();
});
});
test('no-client-id', function(done) {
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCredentials(cp.resource, null as any, cp.clientSecret, function(err) {
assert(err, 'Did not receive expected error.');
assert(err.message.indexOf('parameter') >= 0, 'Error was not specific to a parameter.');
done();
});
});
test('no-resource', function(done) {
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCredentials(null as any, cp.clientId, cp.clientSecret, function(err) {
assert(err, 'Did not receive expected error.');
assert(err.message.indexOf('parameter') >= 0, 'Error was not specific to a parameter.');
done();
});
});
test('http-error', function(done) {
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(403);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(cp.resource, cp.clientId, cp.clientSecret, function (err, tokenResponse) {
assert(err, 'No error was returned when one was expected.');
assert(!tokenResponse, 'a token response was returned when non was expected.');
tokenRequest.done();
done();
});
});
test('oauth-error', function(done) {
var errorResponse = {
error : 'invalid_client',
error_description : 'This is a test error description', // jshint ignore:line
error_uri : 'http://errordescription.com/invalid_client.html' // jshint ignore:line
};
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(400, errorResponse);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(cp.resource, cp.clientId, cp.clientSecret, function (err, tokenResponse) {
assert(err, 'No error was returned when one was expected.');
assert(_.isEqual(errorResponse, tokenResponse), 'The response did not match what was expected');
tokenRequest.done();
done();
});
});
test('error-with-useless-return', function(done) {
var junkResponse = 'This is not properly formated return value.';
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(400, junkResponse);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(cp.resource, cp.clientId, cp.clientSecret, function (err) {
assert(err, 'No error was returned when one was expected.');
tokenRequest.done();
done();
});
});
test('success-with-useless-return', function(done) {
var junkResponse = 'This is not properly formated return value.';
var tokenRequest = util.setupExpectedClientCredTokenRequestResponse(200, junkResponse);
var context = new AuthenticationContext(cp.authUrl);
context.acquireTokenWithClientCredentials(cp.resource, cp.clientId, cp.clientSecret, function (err) {
assert(err, 'No error was returned when one was expected.');
tokenRequest.done();
done();
});
});
test('no-cached-token-found-error', function(done) {
var context = new AuthenticationContext(cp.authUrl);
context.acquireToken(cp.resource, 'unknownUser', cp.clientId, function(err) {
assert(err, 'Expected an error and non was received.');
assert(-1 !== err.message.indexOf('not found'), 'Returned error did not contain expected message: ' + err.message);
done();
});
});
function updateSelfSignedJwtStubs() {
var savedProto: any = {};
savedProto._getDateNow = SelfSignedJwt.prototype._getDateNow;
savedProto._getNewJwtId = SelfSignedJwt.prototype._getNewJwtId;
SelfSignedJwt.prototype._getDateNow = function() { return cp.nowDate; };
SelfSignedJwt.prototype._getNewJwtId = function() { return cp.jwtId; };
return savedProto;
}
function resetSelfSignedJwtStubs(saveProto: any) {
_.extend(SelfSignedJwt.prototype, saveProto);
}
test('cert-happy-path', function(done) {
var saveProto = updateSelfSignedJwtStubs();
var responseOptions = { noRefresh : true };
var response = util.createResponse(responseOptions);
var tokenRequest = util.setupExpectedClientAssertionTokenRequestResponse(200, response.wireResponse, cp.authorityTenant);
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCertificate(response.resource, cp.clientId, cp.cert, cp.certHash, function (err, tokenResponse) {
resetSelfSignedJwtStubs(saveProto);
tokenRequest.done();
if (!err) {
assert(util.isMatchTokenResponse(response.cachedResponse, tokenResponse), 'The response did not match what was expected');
tokenRequest.done();
}
done(err);
});
});
test('cert-bad-cert', function(done) {
var cert = 'gobbledy';
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCertificate(cp.resource, cp.clientId, cert, cp.certHash, function (err) {
assert(err, 'Did not receive expected error.');
assert(0 <= err.message.indexOf('Failed to sign JWT'), 'Unexpected error message' + err.message);
done();
});
});
test('cert-bad-thumbprint', function(done) {
var thumbprint = 'gobbledy';
var context = new AuthenticationContext(cp.authorityTenant);
context.acquireTokenWithClientCertificate(cp.resource, cp.clientId, cp.cert, thumbprint, function (err) {
assert(err, 'Did not receive expected error.');
assert(0 <= err.message.indexOf('thumbprint does not match a known format'), 'Unexpected error message' + err.message);
done();
});
});
});