@webex/internal-plugin-identity 0.0.0-next.1
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 +8 -0
- package/babel.config.js +3 -0
- package/dist/config.js +14 -0
- package/dist/config.js.map +1 -0
- package/dist/constants.js +23 -0
- package/dist/constants.js.map +1 -0
- package/dist/identity.js +185 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/types/config.d.ts +7 -0
- package/dist/types/constants.d.ts +11 -0
- package/dist/types/identity.d.ts +45 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/types.d.ts +25 -0
- package/dist/types/utils.d.ts +31 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.js +132 -0
- package/dist/utils.js.map +1 -0
- package/jest.config.js +3 -0
- package/package.json +51 -0
- package/src/config.ts +5 -0
- package/src/constants.ts +102 -0
- package/src/identity.ts +164 -0
- package/src/index.ts +18 -0
- package/src/types.ts +29 -0
- package/src/utils.ts +95 -0
- package/test/unit/spec/identity.ts +198 -0
- package/test/unit/spec/utils.ts +160 -0
- package/tsconfig.json +4 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2026 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
import 'jsdom-global/register';
|
|
5
|
+
import {assert} from '@webex/test-helper-chai';
|
|
6
|
+
import MockWebex from '@webex/test-helper-mock-webex';
|
|
7
|
+
import sinon from 'sinon';
|
|
8
|
+
import Identity from '../../../src/identity';
|
|
9
|
+
import {
|
|
10
|
+
WEBEX_CA_FEDRAMP_ROOTS,
|
|
11
|
+
WEBEX_CA_INTEGRATION_ROOTS,
|
|
12
|
+
WEBEX_CA_PRODUCTION_ROOTS,
|
|
13
|
+
} from '@webex/internal-plugin-identity/src/constants';
|
|
14
|
+
import * as identityUtils from '../../../src/utils';
|
|
15
|
+
|
|
16
|
+
// 'ABC' -> base64 'QUJD'; 'DEF' -> base64 'REVG'.
|
|
17
|
+
const LEAF_PEM = '-----BEGIN CERTIFICATE-----\nQUJD\n-----END CERTIFICATE-----';
|
|
18
|
+
const ROOT_PEM = '-----BEGIN CERTIFICATE-----\nREVG\n-----END CERTIFICATE-----';
|
|
19
|
+
|
|
20
|
+
describe('plugin-identity', () => {
|
|
21
|
+
describe('Identity', () => {
|
|
22
|
+
let webex;
|
|
23
|
+
let webexRequest;
|
|
24
|
+
let generateCsrStub;
|
|
25
|
+
let identity;
|
|
26
|
+
let privKeyDer;
|
|
27
|
+
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
privKeyDer = new Uint8Array(100);
|
|
30
|
+
for (let i = 0; i < privKeyDer.length; i += 1) {
|
|
31
|
+
privKeyDer[i] = i;
|
|
32
|
+
}
|
|
33
|
+
generateCsrStub = sinon
|
|
34
|
+
.stub(identityUtils, 'generateCsr')
|
|
35
|
+
.resolves({privKeyDer, csr: 'BASE64CSR'});
|
|
36
|
+
webex = MockWebex();
|
|
37
|
+
webexRequest = sinon.stub().resolves({body: `${LEAF_PEM}\n${ROOT_PEM}`});
|
|
38
|
+
webex.request = webexRequest;
|
|
39
|
+
webex.internal = {
|
|
40
|
+
services: {isIntegrationEnvironment: sinon.stub().returns(false)},
|
|
41
|
+
};
|
|
42
|
+
identity = new Identity({webex}, {parent: webex});
|
|
43
|
+
sinon.stub(identityUtils, 'isCertificateExpiringSoon').returns(false);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
sinon.restore();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('getTrustAnchors', () => {
|
|
51
|
+
it('returns the Webex CA root and empty user identity anchors', () => {
|
|
52
|
+
assert.deepEqual(identity.getTrustAnchors(), {
|
|
53
|
+
webexCaRoots: `${WEBEX_CA_PRODUCTION_ROOTS}\n${WEBEX_CA_FEDRAMP_ROOTS}`,
|
|
54
|
+
userIdentityRoots: '',
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('returns the integration CA roots for integration environments', () => {
|
|
59
|
+
webex.internal.services.isIntegrationEnvironment.returns(true);
|
|
60
|
+
|
|
61
|
+
assert.deepEqual(identity.getTrustAnchors(), {
|
|
62
|
+
webexCaRoots: WEBEX_CA_INTEGRATION_ROOTS,
|
|
63
|
+
userIdentityRoots: '',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('defaults to production trust anchors when the environment cannot be determined', () => {
|
|
68
|
+
webex.internal = undefined;
|
|
69
|
+
|
|
70
|
+
assert.deepEqual(identity.getTrustAnchors(), {
|
|
71
|
+
webexCaRoots: `${WEBEX_CA_PRODUCTION_ROOTS}\n${WEBEX_CA_FEDRAMP_ROOTS}`,
|
|
72
|
+
userIdentityRoots: '',
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('getCredentials', () => {
|
|
78
|
+
it('creates a credential cache for each plugin instance', () => {
|
|
79
|
+
const secondWebex = MockWebex();
|
|
80
|
+
const secondIdentity = new Identity({webex: secondWebex}, {parent: secondWebex});
|
|
81
|
+
|
|
82
|
+
assert.notStrictEqual(identity.credentialsCache, secondIdentity.credentialsCache);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('generates a CSR, submits it to the CA and returns the raw key + cert chain', async () => {
|
|
86
|
+
const credentials = await identity.getCredentials('user-1');
|
|
87
|
+
|
|
88
|
+
assert.calledOnceWithExactly(generateCsrStub, 'user-1');
|
|
89
|
+
|
|
90
|
+
const args = webexRequest.firstCall.args[0];
|
|
91
|
+
|
|
92
|
+
assert.equal(args.method, 'POST');
|
|
93
|
+
assert.equal(args.service, 'webex-certificate-authority');
|
|
94
|
+
assert.equal(args.resource, 'certificates');
|
|
95
|
+
assert.deepEqual(args.headers, {'include-root-cert': 'false'});
|
|
96
|
+
assert.deepEqual(args.body, {csr: 'BASE64CSR'});
|
|
97
|
+
|
|
98
|
+
// The raw EC P-256 key is bytes 36..67 of the PKCS#8 DER.
|
|
99
|
+
assert.deepEqual(Array.from(credentials.privateKey), Array.from(privKeyDer.slice(36, 68)));
|
|
100
|
+
assert.equal(credentials.certChain.length, 2);
|
|
101
|
+
assert.deepEqual(Array.from(credentials.certChain[0]), [65, 66, 67]);
|
|
102
|
+
assert.deepEqual(Array.from(credentials.certChain[1]), [68, 69, 70]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('caches credentials per contact and only requests once', async () => {
|
|
106
|
+
const first = await identity.getCredentials('user-1');
|
|
107
|
+
const second = await identity.getCredentials('user-1');
|
|
108
|
+
|
|
109
|
+
assert.equal(first, second);
|
|
110
|
+
assert.calledOnce(generateCsrStub);
|
|
111
|
+
assert.calledOnce(webexRequest);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('shares a single request for concurrent cache-miss callers', async () => {
|
|
115
|
+
const [first, second] = await Promise.all([
|
|
116
|
+
identity.getCredentials('user-1'),
|
|
117
|
+
identity.getCredentials('user-1'),
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
assert.equal(first, second);
|
|
121
|
+
assert.calledOnce(generateCsrStub);
|
|
122
|
+
assert.calledOnce(webexRequest);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('rejects and does not cache a response without a certificate chain', async () => {
|
|
126
|
+
webexRequest.onFirstCall().resolves({body: '<html>certificate not found</html>'});
|
|
127
|
+
webexRequest.onSecondCall().resolves({body: `${LEAF_PEM}\n${ROOT_PEM}`});
|
|
128
|
+
|
|
129
|
+
await assert.isRejected(
|
|
130
|
+
identity.getCredentials('user-1'),
|
|
131
|
+
/Certificate authority response did not contain a certificate chain/
|
|
132
|
+
);
|
|
133
|
+
assert.isFalse(identity.credentialsCache.has('user-1'));
|
|
134
|
+
|
|
135
|
+
const credentials = await identity.getCredentials('user-1');
|
|
136
|
+
|
|
137
|
+
assert.equal(credentials.certChain.length, 2);
|
|
138
|
+
assert.calledTwice(generateCsrStub);
|
|
139
|
+
assert.calledTwice(webexRequest);
|
|
140
|
+
// The empty-chain short-circuit must skip the expiry check entirely.
|
|
141
|
+
assert.notCalled(identityUtils.isCertificateExpiringSoon);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('does not cache a failed CSR generation and never calls the CA', async () => {
|
|
145
|
+
generateCsrStub.onFirstCall().rejects(new Error('CSR failed'));
|
|
146
|
+
|
|
147
|
+
await assert.isRejected(identity.getCredentials('user-1'), /CSR failed/);
|
|
148
|
+
|
|
149
|
+
assert.notCalled(webexRequest);
|
|
150
|
+
|
|
151
|
+
const credentials = await identity.getCredentials('user-1');
|
|
152
|
+
|
|
153
|
+
assert.equal(credentials.certChain.length, 2);
|
|
154
|
+
assert.calledTwice(generateCsrStub);
|
|
155
|
+
assert.calledOnce(webexRequest);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('renews credentials when the cached leaf certificate is within one day of expiry', async () => {
|
|
159
|
+
identityUtils.isCertificateExpiringSoon.onCall(0).returns(true);
|
|
160
|
+
|
|
161
|
+
const first = await identity.getCredentials('user-1');
|
|
162
|
+
const renewed = await identity.getCredentials('user-1');
|
|
163
|
+
const cachedRenewal = await identity.getCredentials('user-1');
|
|
164
|
+
|
|
165
|
+
assert.notEqual(first, renewed);
|
|
166
|
+
assert.equal(renewed, cachedRenewal);
|
|
167
|
+
assert.calledTwice(generateCsrStub);
|
|
168
|
+
assert.calledTwice(webexRequest);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('shares one renewal when concurrent callers find a near-expiry certificate', async () => {
|
|
172
|
+
await identity.getCredentials('user-1');
|
|
173
|
+
identityUtils.isCertificateExpiringSoon.returns(true);
|
|
174
|
+
|
|
175
|
+
const [first, second] = await Promise.all([
|
|
176
|
+
identity.getCredentials('user-1'),
|
|
177
|
+
identity.getCredentials('user-1'),
|
|
178
|
+
]);
|
|
179
|
+
|
|
180
|
+
assert.equal(first, second);
|
|
181
|
+
assert.calledTwice(generateCsrStub);
|
|
182
|
+
assert.calledTwice(webexRequest);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('does not cache a failed attempt', async () => {
|
|
186
|
+
webexRequest.onFirstCall().rejects(new Error('CA down'));
|
|
187
|
+
webexRequest.onSecondCall().resolves({body: LEAF_PEM});
|
|
188
|
+
|
|
189
|
+
await assert.isRejected(identity.getCredentials('user-1'), /CA down/);
|
|
190
|
+
|
|
191
|
+
const credentials = await identity.getCredentials('user-1');
|
|
192
|
+
|
|
193
|
+
assert.equal(credentials.certChain.length, 1);
|
|
194
|
+
assert.calledTwice(webexRequest);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2026 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import 'jsdom-global/register';
|
|
6
|
+
import * as asn1js from 'asn1js';
|
|
7
|
+
import * as pkijs from 'pkijs';
|
|
8
|
+
import {assert} from '@webex/test-helper-chai';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
generateCsr,
|
|
12
|
+
isCertificateExpiringSoon,
|
|
13
|
+
pemToUint8Array,
|
|
14
|
+
pemToUint8Arrays,
|
|
15
|
+
} from '../../../src/utils';
|
|
16
|
+
|
|
17
|
+
async function makeCertificate(notAfter: Date): Promise<Uint8Array> {
|
|
18
|
+
const certificate = new pkijs.Certificate();
|
|
19
|
+
|
|
20
|
+
certificate.version = 2;
|
|
21
|
+
certificate.serialNumber = new asn1js.Integer({value: 1});
|
|
22
|
+
certificate.issuer.typesAndValues.push(
|
|
23
|
+
new pkijs.AttributeTypeAndValue({
|
|
24
|
+
type: '2.5.4.3',
|
|
25
|
+
value: new asn1js.Utf8String({value: 'Test'}),
|
|
26
|
+
})
|
|
27
|
+
);
|
|
28
|
+
certificate.subject.typesAndValues.push(
|
|
29
|
+
new pkijs.AttributeTypeAndValue({
|
|
30
|
+
type: '2.5.4.3',
|
|
31
|
+
value: new asn1js.Utf8String({value: 'Test'}),
|
|
32
|
+
})
|
|
33
|
+
);
|
|
34
|
+
certificate.notBefore.value = new Date('2026-01-01T00:00:00Z');
|
|
35
|
+
certificate.notAfter.value = notAfter;
|
|
36
|
+
|
|
37
|
+
const keyPair = await crypto.subtle.generateKey(
|
|
38
|
+
{name: 'ECDSA', namedCurve: 'P-256'},
|
|
39
|
+
true,
|
|
40
|
+
['sign', 'verify']
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
await certificate.subjectPublicKeyInfo.importKey(keyPair.publicKey);
|
|
44
|
+
await certificate.sign(keyPair.privateKey, 'SHA-256');
|
|
45
|
+
|
|
46
|
+
return new Uint8Array(certificate.toSchema().toBER(false));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('identity utils', () => {
|
|
50
|
+
describe('isCertificateExpiringSoon', () => {
|
|
51
|
+
it('returns true when the leaf certificate expires within one day', async () => {
|
|
52
|
+
const now = new Date('2026-10-08T12:00:00Z');
|
|
53
|
+
const certificate = await makeCertificate(new Date(now.getTime() + 23 * 60 * 60 * 1000));
|
|
54
|
+
|
|
55
|
+
assert.isTrue(isCertificateExpiringSoon(certificate, now.getTime()));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('returns false when the leaf certificate expires after one day', async () => {
|
|
59
|
+
const now = new Date('2026-10-08T12:00:00Z');
|
|
60
|
+
const certificate = await makeCertificate(new Date(now.getTime() + 25 * 60 * 60 * 1000));
|
|
61
|
+
|
|
62
|
+
assert.isFalse(isCertificateExpiringSoon(certificate, now.getTime()));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('treats an unreadable certificate as needing renewal', () => {
|
|
66
|
+
assert.isTrue(isCertificateExpiringSoon(new Uint8Array([1, 2, 3])));
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('treats a parseable non-certificate structure as needing renewal', () => {
|
|
70
|
+
// A DER-encoded INTEGER parses as valid BER (offset !== -1) but fails pkijs certificate
|
|
71
|
+
// construction, exercising the catch branch rather than the offset === -1 guard above.
|
|
72
|
+
assert.isTrue(isCertificateExpiringSoon(new Uint8Array([0x02, 0x01, 0x05])));
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('returns false when the certificate expires exactly at the renewal window boundary', async () => {
|
|
76
|
+
const now = new Date('2026-10-08T12:00:00Z');
|
|
77
|
+
const certificate = await makeCertificate(new Date(now.getTime() + 24 * 60 * 60 * 1000));
|
|
78
|
+
|
|
79
|
+
assert.isFalse(isCertificateExpiringSoon(certificate, now.getTime()));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('uses the current time when no reference time is provided', async () => {
|
|
83
|
+
const certificate = await makeCertificate(new Date(Date.now() + 10 * 24 * 60 * 60 * 1000));
|
|
84
|
+
|
|
85
|
+
assert.isFalse(isCertificateExpiringSoon(certificate));
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('generateCsr', () => {
|
|
90
|
+
it('generates a P-256 CSR with the contact ID as subject and a valid signature', async () => {
|
|
91
|
+
const contactId = 'user@example.com';
|
|
92
|
+
const {privKeyDer, csr} = await generateCsr(contactId);
|
|
93
|
+
const csrBytes = Uint8Array.from(atob(csr), (character) => character.charCodeAt(0));
|
|
94
|
+
const parsedCsr = asn1js.fromBER(csrBytes.buffer);
|
|
95
|
+
const request = new pkijs.CertificationRequest({schema: parsedCsr.result});
|
|
96
|
+
|
|
97
|
+
assert.instanceOf(privKeyDer, Uint8Array);
|
|
98
|
+
assert.notEqual(parsedCsr.offset, -1);
|
|
99
|
+
assert.equal(request.subject.typesAndValues[0].value.valueBlock.value, `email:${contactId}`);
|
|
100
|
+
assert.isTrue(await request.verify());
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('pemToUint8Array', () => {
|
|
105
|
+
it('decodes one PEM certificate block to DER bytes', () => {
|
|
106
|
+
const pem = '-----BEGIN CERTIFICATE-----\nQU\nJD\n-----END CERTIFICATE-----';
|
|
107
|
+
|
|
108
|
+
assert.deepEqual(Array.from(pemToUint8Array(pem)), [65, 66, 67]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('decodes a PEM block that uses CRLF line endings', () => {
|
|
112
|
+
const pem = '-----BEGIN CERTIFICATE-----\r\nQUJD\r\n-----END CERTIFICATE-----';
|
|
113
|
+
|
|
114
|
+
assert.deepEqual(Array.from(pemToUint8Array(pem)), [65, 66, 67]);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('pemToUint8Arrays', () => {
|
|
119
|
+
it('decodes a single-certificate chain', () => {
|
|
120
|
+
const pem = '-----BEGIN CERTIFICATE-----\nQUJD\n-----END CERTIFICATE-----';
|
|
121
|
+
|
|
122
|
+
assert.deepEqual(
|
|
123
|
+
pemToUint8Arrays(pem).map((certificate) => Array.from(certificate)),
|
|
124
|
+
[[65, 66, 67]]
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('decodes each certificate in a concatenated PEM chain', () => {
|
|
129
|
+
const pem = [
|
|
130
|
+
'-----BEGIN CERTIFICATE-----\nQUJD\n-----END CERTIFICATE-----',
|
|
131
|
+
'-----BEGIN CERTIFICATE-----\nREVG\n-----END CERTIFICATE-----',
|
|
132
|
+
].join('\n');
|
|
133
|
+
|
|
134
|
+
assert.deepEqual(
|
|
135
|
+
pemToUint8Arrays(pem).map((certificate) => Array.from(certificate)),
|
|
136
|
+
[
|
|
137
|
+
[65, 66, 67],
|
|
138
|
+
[68, 69, 70],
|
|
139
|
+
]
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('returns an empty chain when the response has no certificate blocks', () => {
|
|
144
|
+
assert.deepEqual(pemToUint8Arrays('no certificates'), []);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('splits adjacent certificate blocks that are not newline-separated', () => {
|
|
148
|
+
const pem =
|
|
149
|
+
'-----BEGIN CERTIFICATE-----\nQUJD\n-----END CERTIFICATE----------BEGIN CERTIFICATE-----\nREVG\n-----END CERTIFICATE-----';
|
|
150
|
+
|
|
151
|
+
assert.deepEqual(
|
|
152
|
+
pemToUint8Arrays(pem).map((certificate) => Array.from(certificate)),
|
|
153
|
+
[
|
|
154
|
+
[65, 66, 67],
|
|
155
|
+
[68, 69, 70],
|
|
156
|
+
]
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
package/tsconfig.json
ADDED