@webex/plugin-authorization-browser-first-party 3.12.0-webex-services-ready.4 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -7
- package/dist/authorization.js +82 -17
- package/dist/authorization.js.map +1 -1
- package/dist/config.js +42 -13
- package/dist/config.js.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/package.json +13 -13
- package/src/authorization.js +78 -15
- package/src/config.js +28 -8
- package/src/index.js +1 -1
- package/test/unit/spec/authorization.js +288 -16
|
@@ -14,7 +14,8 @@ import {base64, patterns} from '@webex/common';
|
|
|
14
14
|
import {merge, times} from 'lodash';
|
|
15
15
|
import CryptoJS from 'crypto-js';
|
|
16
16
|
import Authorization from '@webex/plugin-authorization-browser-first-party';
|
|
17
|
-
import {Events} from '../../../src';
|
|
17
|
+
import {Events, InitialAuthorizationCodeGrantOutcomes} from '../../../src';
|
|
18
|
+
import authorizationConfig from '../../../src/config';
|
|
18
19
|
|
|
19
20
|
// Necessary to require lodash this way in order to stub the method
|
|
20
21
|
const lodash = require('lodash');
|
|
@@ -25,9 +26,13 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
25
26
|
href = 'https://example.com',
|
|
26
27
|
csrfToken = undefined,
|
|
27
28
|
pkceVerifier = undefined,
|
|
28
|
-
config = {}
|
|
29
|
+
config = {},
|
|
30
|
+
getRandomValues = sinon.stub().callsFake((randomValues) => randomValues.fill(0))
|
|
29
31
|
) {
|
|
30
32
|
const mockWindow = {
|
|
33
|
+
crypto: {
|
|
34
|
+
getRandomValues,
|
|
35
|
+
},
|
|
31
36
|
history: {
|
|
32
37
|
replaceState(a, b, location) {
|
|
33
38
|
mockWindow.location.href = location;
|
|
@@ -104,6 +109,35 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
104
109
|
sinon.restore();
|
|
105
110
|
});
|
|
106
111
|
|
|
112
|
+
it('exposes the initial authorization code grant outcome as readonly', () => {
|
|
113
|
+
const webex = makeWebex();
|
|
114
|
+
const changeSpy = sinon.spy();
|
|
115
|
+
|
|
116
|
+
webex.authorization.on('change:initialAuthorizationCodeGrantOutcome', changeSpy);
|
|
117
|
+
|
|
118
|
+
assert.equal(
|
|
119
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
120
|
+
InitialAuthorizationCodeGrantOutcomes.notAttempted
|
|
121
|
+
);
|
|
122
|
+
assert.throws(() => {
|
|
123
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome =
|
|
124
|
+
InitialAuthorizationCodeGrantOutcomes.success;
|
|
125
|
+
}, /derived property, it can't be set directly/);
|
|
126
|
+
|
|
127
|
+
webex.authorization._initialAuthorizationCodeGrantOutcome =
|
|
128
|
+
InitialAuthorizationCodeGrantOutcomes.success;
|
|
129
|
+
|
|
130
|
+
assert.equal(
|
|
131
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
132
|
+
InitialAuthorizationCodeGrantOutcomes.success
|
|
133
|
+
);
|
|
134
|
+
assert.calledOnceWithExactly(
|
|
135
|
+
changeSpy,
|
|
136
|
+
webex.authorization,
|
|
137
|
+
InitialAuthorizationCodeGrantOutcomes.success
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
107
141
|
describe('#initialize()', () => {
|
|
108
142
|
describe('when there is a code in the url', () => {
|
|
109
143
|
it('exchanges it for an access token and sets ready', () => {
|
|
@@ -119,9 +153,25 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
119
153
|
assert.calledTwice(webex.request);
|
|
120
154
|
assert.isTrue(webex.authorization.ready);
|
|
121
155
|
assert.isTrue(webex.credentials.canAuthorize);
|
|
156
|
+
assert.equal(
|
|
157
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
158
|
+
InitialAuthorizationCodeGrantOutcomes.success
|
|
159
|
+
);
|
|
122
160
|
});
|
|
123
161
|
});
|
|
124
162
|
|
|
163
|
+
it('retains the initialization exchange outcome after logout', async () => {
|
|
164
|
+
const webex = makeWebex('http://example.com/?code=5');
|
|
165
|
+
|
|
166
|
+
await webex.authorization.when('change:ready');
|
|
167
|
+
webex.authorization.logout({noRedirect: true});
|
|
168
|
+
|
|
169
|
+
assert.equal(
|
|
170
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
171
|
+
InitialAuthorizationCodeGrantOutcomes.success
|
|
172
|
+
);
|
|
173
|
+
});
|
|
174
|
+
|
|
125
175
|
it('validates the csrf token', () => {
|
|
126
176
|
const csrfToken = 'abcd';
|
|
127
177
|
|
|
@@ -264,8 +314,32 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
264
314
|
'authorization: failed initial authorization code grant request',
|
|
265
315
|
error
|
|
266
316
|
);
|
|
317
|
+
assert.equal(
|
|
318
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
319
|
+
InitialAuthorizationCodeGrantOutcomes.failure
|
|
320
|
+
);
|
|
267
321
|
});
|
|
268
322
|
});
|
|
323
|
+
|
|
324
|
+
it('retains failure when the automatic exchange promise rejects', async () => {
|
|
325
|
+
const error = new Error('exchange rejected');
|
|
326
|
+
|
|
327
|
+
sinon.stub(Authorization.prototype, 'requestAuthorizationCodeGrant').rejects(error);
|
|
328
|
+
|
|
329
|
+
const webex = makeWebex('http://example.com?code=5');
|
|
330
|
+
|
|
331
|
+
await webex.authorization.when('change:ready');
|
|
332
|
+
|
|
333
|
+
assert.equal(
|
|
334
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
335
|
+
InitialAuthorizationCodeGrantOutcomes.failure
|
|
336
|
+
);
|
|
337
|
+
assert.calledOnceWithExactly(
|
|
338
|
+
webex.logger.warn,
|
|
339
|
+
'authorization: failed initial authorization code grant request',
|
|
340
|
+
error
|
|
341
|
+
);
|
|
342
|
+
});
|
|
269
343
|
});
|
|
270
344
|
describe('when the url contains an error', () => {
|
|
271
345
|
it('throws a grant error', () => {
|
|
@@ -287,6 +361,21 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
287
361
|
|
|
288
362
|
assert.isTrue(webex.authorization.ready);
|
|
289
363
|
assert.isFalse(webex.credentials.canAuthorize);
|
|
364
|
+
assert.equal(
|
|
365
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
366
|
+
InitialAuthorizationCodeGrantOutcomes.notAttempted
|
|
367
|
+
);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it('does not treat a later authorization-code grant as the initialization exchange', async () => {
|
|
371
|
+
const webex = makeWebex('http://example.com');
|
|
372
|
+
|
|
373
|
+
await webex.authorization.requestAuthorizationCodeGrant({code: 'later-code'});
|
|
374
|
+
|
|
375
|
+
assert.equal(
|
|
376
|
+
webex.authorization.initialAuthorizationCodeGrantOutcome,
|
|
377
|
+
InitialAuthorizationCodeGrantOutcomes.notAttempted
|
|
378
|
+
);
|
|
290
379
|
});
|
|
291
380
|
});
|
|
292
381
|
|
|
@@ -306,6 +395,139 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
306
395
|
});
|
|
307
396
|
});
|
|
308
397
|
|
|
398
|
+
describe('#requestAuthorizationCodeGrant()', () => {
|
|
399
|
+
it('uses the client ID in the form for a public client', async () => {
|
|
400
|
+
const webex = makeWebex('http://example.com', undefined, undefined, {
|
|
401
|
+
credentials: {
|
|
402
|
+
clientType: 'public',
|
|
403
|
+
client_id: 'public-client-id',
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
await webex.authorization.requestAuthorizationCodeGrant({
|
|
408
|
+
code: 'authorization-code',
|
|
409
|
+
codeVerifier: 'code-verifier',
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
assert.calledOnceWithExactly(webex.request, {
|
|
413
|
+
method: 'POST',
|
|
414
|
+
uri: webex.config.credentials.tokenUrl,
|
|
415
|
+
form: {
|
|
416
|
+
grant_type: 'authorization_code',
|
|
417
|
+
redirect_uri: 'http://example.com',
|
|
418
|
+
code: 'authorization-code',
|
|
419
|
+
self_contained_token: true,
|
|
420
|
+
code_verifier: 'code-verifier',
|
|
421
|
+
client_id: 'public-client-id',
|
|
422
|
+
},
|
|
423
|
+
addAuthHeader: false,
|
|
424
|
+
shouldRefreshAccessToken: false,
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('uses HTTP Basic authentication for a confidential client', async () => {
|
|
429
|
+
const webex = makeWebex('http://example.com', undefined, undefined, {
|
|
430
|
+
credentials: {
|
|
431
|
+
clientType: 'confidential',
|
|
432
|
+
client_id: 'confidential-client-id',
|
|
433
|
+
client_secret: 'confidential-client-secret',
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
await webex.authorization.requestAuthorizationCodeGrant({
|
|
438
|
+
code: 'authorization-code',
|
|
439
|
+
codeVerifier: 'code-verifier',
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
assert.calledOnceWithExactly(webex.request, {
|
|
443
|
+
method: 'POST',
|
|
444
|
+
uri: webex.config.credentials.tokenUrl,
|
|
445
|
+
form: {
|
|
446
|
+
grant_type: 'authorization_code',
|
|
447
|
+
redirect_uri: 'http://example.com',
|
|
448
|
+
code: 'authorization-code',
|
|
449
|
+
self_contained_token: true,
|
|
450
|
+
code_verifier: 'code-verifier',
|
|
451
|
+
},
|
|
452
|
+
auth: {
|
|
453
|
+
user: 'confidential-client-id',
|
|
454
|
+
pass: 'confidential-client-secret',
|
|
455
|
+
sendImmediately: true,
|
|
456
|
+
},
|
|
457
|
+
shouldRefreshAccessToken: false,
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
describe('refreshCallback()', () => {
|
|
463
|
+
const tokenResponse = {
|
|
464
|
+
access_token: 'access-token',
|
|
465
|
+
refresh_token: 'refresh-token',
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
it('uses the client ID in the form for a public client', async () => {
|
|
469
|
+
const webex = {request: sinon.stub().resolves({body: tokenResponse})};
|
|
470
|
+
const token = {
|
|
471
|
+
refresh_token: 'current-refresh-token',
|
|
472
|
+
config: {
|
|
473
|
+
clientType: 'public',
|
|
474
|
+
client_id: 'public-client-id',
|
|
475
|
+
redirect_uri: 'http://example.com',
|
|
476
|
+
tokenUrl: 'https://idbroker.example.com/access_token',
|
|
477
|
+
},
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
const response = await authorizationConfig.credentials.refreshCallback(webex, token);
|
|
481
|
+
|
|
482
|
+
assert.calledOnceWithExactly(webex.request, {
|
|
483
|
+
method: 'POST',
|
|
484
|
+
uri: 'https://idbroker.example.com/access_token',
|
|
485
|
+
form: {
|
|
486
|
+
grant_type: 'refresh_token',
|
|
487
|
+
redirect_uri: 'http://example.com',
|
|
488
|
+
refresh_token: 'current-refresh-token',
|
|
489
|
+
client_id: 'public-client-id',
|
|
490
|
+
},
|
|
491
|
+
addAuthHeader: false,
|
|
492
|
+
shouldRefreshAccessToken: false,
|
|
493
|
+
});
|
|
494
|
+
assert.deepEqual(response, tokenResponse);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it('uses HTTP Basic authentication for a confidential client', async () => {
|
|
498
|
+
const webex = {request: sinon.stub().resolves({body: tokenResponse})};
|
|
499
|
+
const token = {
|
|
500
|
+
refresh_token: 'current-refresh-token',
|
|
501
|
+
config: {
|
|
502
|
+
clientType: 'confidential',
|
|
503
|
+
client_id: 'confidential-client-id',
|
|
504
|
+
client_secret: 'confidential-client-secret',
|
|
505
|
+
redirect_uri: 'http://example.com',
|
|
506
|
+
tokenUrl: 'https://idbroker.example.com/access_token',
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const response = await authorizationConfig.credentials.refreshCallback(webex, token);
|
|
511
|
+
|
|
512
|
+
assert.calledOnceWithExactly(webex.request, {
|
|
513
|
+
method: 'POST',
|
|
514
|
+
uri: 'https://idbroker.example.com/access_token',
|
|
515
|
+
form: {
|
|
516
|
+
grant_type: 'refresh_token',
|
|
517
|
+
redirect_uri: 'http://example.com',
|
|
518
|
+
refresh_token: 'current-refresh-token',
|
|
519
|
+
},
|
|
520
|
+
auth: {
|
|
521
|
+
user: 'confidential-client-id',
|
|
522
|
+
pass: 'confidential-client-secret',
|
|
523
|
+
sendImmediately: true,
|
|
524
|
+
},
|
|
525
|
+
shouldRefreshAccessToken: false,
|
|
526
|
+
});
|
|
527
|
+
assert.deepEqual(response, tokenResponse);
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
|
|
309
531
|
describe('#initiateLogin()', () => {
|
|
310
532
|
it('calls #initiateAuthorizationCodeGrant()', () => {
|
|
311
533
|
const webex = makeWebex(undefined, undefined, {
|
|
@@ -1221,34 +1443,84 @@ describe('plugin-authorization-browser-first-party', () => {
|
|
|
1221
1443
|
});
|
|
1222
1444
|
|
|
1223
1445
|
describe('#_generateCodeChallenge', () => {
|
|
1224
|
-
const expectedCodeChallenge = 'code challenge';
|
|
1225
1446
|
// eslint-disable-next-line no-underscore-dangle
|
|
1226
1447
|
const safeCharacterMap = CryptoJS.enc.Base64url._safe_map;
|
|
1227
1448
|
|
|
1228
|
-
|
|
1449
|
+
function makeWebexWithRandomValues(fillRandomValues) {
|
|
1450
|
+
const getRandomValuesStub = sinon.stub().callsFake((randomValues) => {
|
|
1451
|
+
fillRandomValues(randomValues);
|
|
1229
1452
|
|
|
1230
|
-
|
|
1231
|
-
|
|
1453
|
+
return randomValues;
|
|
1454
|
+
});
|
|
1455
|
+
const webex = makeWebex(
|
|
1456
|
+
'http://example.com',
|
|
1457
|
+
undefined,
|
|
1458
|
+
undefined,
|
|
1459
|
+
{},
|
|
1460
|
+
getRandomValuesStub
|
|
1461
|
+
);
|
|
1462
|
+
|
|
1463
|
+
getRandomValuesStub.resetHistory();
|
|
1464
|
+
webex.getWindow().sessionStorage.setItem.resetHistory();
|
|
1465
|
+
|
|
1466
|
+
return {getRandomValuesStub, webex};
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
it('uses a 128-byte CSPRNG and does not use insecure random generators', () => {
|
|
1470
|
+
const {getRandomValuesStub, webex} = makeWebexWithRandomValues((randomValues) => {
|
|
1471
|
+
randomValues.fill(0);
|
|
1472
|
+
});
|
|
1473
|
+
const mathRandomStub = sinon.stub(Math, 'random');
|
|
1474
|
+
const lodashRandomStub = sinon.stub(lodash, 'random');
|
|
1475
|
+
|
|
1476
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
1477
|
+
webex.authorization._generateCodeChallenge();
|
|
1478
|
+
|
|
1479
|
+
const generatedRandomValues = getRandomValuesStub.firstCall.args[0];
|
|
1480
|
+
|
|
1481
|
+
assert.calledOnceWithExactly(getRandomValuesStub, generatedRandomValues);
|
|
1482
|
+
assert.instanceOf(generatedRandomValues, Uint8Array);
|
|
1483
|
+
assert.lengthOf(generatedRandomValues, 128);
|
|
1484
|
+
assert.notCalled(mathRandomStub);
|
|
1485
|
+
assert.notCalled(lodashRandomStub);
|
|
1486
|
+
});
|
|
1487
|
+
|
|
1488
|
+
it('generates a 128-character verifier from the base64url-safe alphabet', () => {
|
|
1489
|
+
const {webex} = makeWebexWithRandomValues((randomValues) => {
|
|
1490
|
+
randomValues.set(times(128, (index) => index));
|
|
1491
|
+
});
|
|
1492
|
+
const expectedVerifier = times(
|
|
1493
|
+
128,
|
|
1494
|
+
(index) => safeCharacterMap[index & (safeCharacterMap.length - 1)]
|
|
1495
|
+
).join('');
|
|
1232
1496
|
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1497
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
1498
|
+
webex.authorization._generateCodeChallenge();
|
|
1499
|
+
|
|
1500
|
+
const storedVerifier = webex.getWindow().sessionStorage.setItem.firstCall.args[1];
|
|
1501
|
+
|
|
1502
|
+
assert.match(storedVerifier, /^[A-Za-z0-9_-]{128}$/);
|
|
1503
|
+
assert.equal(storedVerifier, expectedVerifier);
|
|
1504
|
+
});
|
|
1505
|
+
|
|
1506
|
+
it('stores the verifier and returns its SHA-256 base64url challenge', () => {
|
|
1507
|
+
const {webex} = makeWebexWithRandomValues((randomValues) => {
|
|
1508
|
+
randomValues.fill(42);
|
|
1237
1509
|
});
|
|
1510
|
+
const expectedVerifier = safeCharacterMap[42 & (safeCharacterMap.length - 1)].repeat(128);
|
|
1511
|
+
const expectedChallenge = CryptoJS.SHA256(expectedVerifier).toString(
|
|
1512
|
+
CryptoJS.enc.Base64url
|
|
1513
|
+
);
|
|
1238
1514
|
|
|
1239
1515
|
// eslint-disable-next-line no-underscore-dangle
|
|
1240
1516
|
const codeChallenge = webex.authorization._generateCodeChallenge();
|
|
1241
1517
|
|
|
1242
|
-
assert.
|
|
1243
|
-
assert.calledWith(sha256Stub, expectedVerifier);
|
|
1244
|
-
assert.calledWith(toStringStub, CryptoJS.enc.Base64url);
|
|
1245
|
-
assert.callCount(randomStub, 128);
|
|
1246
|
-
assert.calledWith(randomStub, 0, safeCharacterMap.length - 1);
|
|
1247
|
-
assert.calledWith(
|
|
1518
|
+
assert.calledOnceWithExactly(
|
|
1248
1519
|
webex.getWindow().sessionStorage.setItem,
|
|
1249
1520
|
'oauth2-code-verifier',
|
|
1250
1521
|
expectedVerifier
|
|
1251
1522
|
);
|
|
1523
|
+
assert.equal(codeChallenge, expectedChallenge);
|
|
1252
1524
|
});
|
|
1253
1525
|
});
|
|
1254
1526
|
|