-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathencryption.js
More file actions
235 lines (194 loc) · 8.38 KB
/
encryption.js
File metadata and controls
235 lines (194 loc) · 8.38 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
228
229
230
231
232
233
234
235
/*!
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
*/
/* eslint-disable no-underscore-dangle */
import Url from 'url';
import {assert} from '@webex/test-helper-chai';
import MockWebex from '@webex/test-helper-mock-webex';
import sinon from 'sinon';
import Encryption from '@webex/internal-plugin-encryption';
describe('internal-plugin-encryption', () => {
describe('download', () => {
let webex;
beforeEach(() => {
webex = new MockWebex({
children: {
encryption: Encryption,
},
});
});
describe('check _fetchDownloadUrl()', () => {
const fileArray = [
{
url: 'https://files-api-intb1.ciscospark.com/v1/spaces/a0cba376-fc05-4b88-af4b-cfffa7465f9a/contents/1d3931e7-9e31-46bc-8084-d766a8f72c99/versions/5fa9caf87a98410aae49e0173856a974/bytes',
options: undefined
},
{
url: 'https://files-api-intb2.ciscospark.com/v1/spaces/a0cba376-fc05-4b88-af4b-cfffa7465f9a/contents/1d3931e7-9e31-46bc-8084-d766a8f72c99/versions/5fa9caf87a98410aae49e0173856a974/bytes',
options: {params: {allow: false}}
},
{
url: 'https://www.test-api.com/v1/spaces/test-path-name-space/contents/test-path-name-contents/versions/test-version/bytes',
options: {useFileService: true}
},
{
url: 'http://www.test-api.com/v1/spaces/test-path-name-space/contents/test-path-name-contents/versions/test-version/bytes',
options: {useFileService: true, params: {allow: false}}
},
];
let spyStub;
beforeEach(() => {
const returnStub = (obj) => Promise.resolve(obj);
spyStub = sinon.stub(webex.internal.encryption, 'request').callsFake(returnStub);
fileArray.forEach((file) => webex.internal.encryption._fetchDownloadUrl(file.url, file.options));
});
it('verifying file service uris', () => {
assert.equal(
spyStub.args[0][0].uri,
'https://files-api-intb1.ciscospark.com/v1/download/endpoints'
);
assert.equal(
spyStub.args[1][0].uri,
'https://files-api-intb2.ciscospark.com/v1/download/endpoints'
);
assert.equal(spyStub.args[2][0].uri, 'https://www.test-api.com/v1/download/endpoints');
assert.equal(spyStub.args[3][0].uri, 'https://www.test-api.com/v1/download/endpoints');
});
it('verifying https', () => {
assert.equal(Url.parse(spyStub.args[0][0].uri).protocol, 'https:');
assert.equal(Url.parse(spyStub.args[1][0].uri).protocol, 'https:');
assert.equal(Url.parse(spyStub.args[2][0].uri).protocol, 'https:');
assert.equal(Url.parse(spyStub.args[3][0].uri).protocol, 'https:');
});
it('verifying endpoints', () => {
assert.equal(spyStub.args[0][0].body.endpoints[0], fileArray[0].url);
assert.equal(spyStub.args[1][0].body.endpoints[0], fileArray[1].url);
assert.equal(spyStub.args[2][0].body.endpoints[0], fileArray[2].url);
assert.equal(spyStub.args[3][0].body.endpoints[0], fileArray[3].url);
});
afterEach(() => {
spyStub.resetHistory();
});
});
});
describe('decryptBinaryData', () => {
let webex;
beforeEach(() => {
webex = new MockWebex({
children: {
encryption: Encryption,
},
});
});
describe('check decryptBinaryData()', () => {
const testKey = 'https://kms.example.com/keys/test-key-id';
const testJWE = 'eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..test.encrypted.data';
const testOptions = {onBehalfOf: 'test-user-uuid'};
const mockJwk = {kty: 'oct', k: 'test-key-material'};
const mockKey = {jwk: mockJwk};
const mockDecryptedResult = {payload: Buffer.from('decrypted binary data')};
let getKeyStub;
let joseDecryptStub;
beforeEach(() => {
getKeyStub = sinon.stub(webex.internal.encryption, 'getKey').resolves(mockKey);
// Mock the jose.JWE.createDecrypt chain
const mockDecryptor = {
decrypt: sinon.stub().resolves(mockDecryptedResult)
};
joseDecryptStub = sinon.stub(require('node-jose').JWE, 'createDecrypt').returns(mockDecryptor);
});
it('should call getKey and jose.JWE.createDecrypt with correct parameters', async () => {
await webex.internal.encryption.decryptBinaryData(testKey, testJWE, testOptions);
assert.equal(getKeyStub.calledOnce, true);
assert.equal(getKeyStub.args[0][0], testKey);
assert.deepEqual(getKeyStub.args[0][1], testOptions);
assert.equal(joseDecryptStub.calledOnce, true);
assert.equal(joseDecryptStub.args[0][0], mockJwk);
});
it('should call decrypt with ciphertext', async () => {
await webex.internal.encryption.decryptBinaryData(testKey, testJWE, testOptions);
const mockDecryptor = joseDecryptStub.returnValues[0];
assert.equal(mockDecryptor.decrypt.calledOnce, true);
assert.equal(mockDecryptor.decrypt.args[0][0], testJWE);
});
it('should return the payload buffer', async () => {
const result = await webex.internal.encryption.decryptBinaryData(testKey, testJWE, testOptions);
assert.equal(result, mockDecryptedResult.payload);
assert.equal(Buffer.isBuffer(result), true);
});
it('should work without options parameter', async () => {
await webex.internal.encryption.decryptBinaryData(testKey, testJWE);
assert.equal(getKeyStub.calledOnce, true);
assert.equal(getKeyStub.args[0][0], testKey);
assert.equal(getKeyStub.args[0][1], undefined);
});
afterEach(() => {
getKeyStub.restore();
joseDecryptStub.restore();
});
});
});
describe('encryptBinaryData', () => {
let webex;
beforeEach(() => {
webex = new MockWebex({
children: {
encryption: Encryption,
},
});
});
describe('check encryptBinaryData()', () => {
const testKey = 'https://kms.example.com/keys/test-key-id';
const testData = Buffer.from('binary data to encrypt');
const testOptions = {onBehalfOf: 'test-user-uuid'};
const mockJwk = {kty: 'oct', k: 'test-key-material'};
const mockKey = {jwk: mockJwk};
const mockEncryptedJWE = 'eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..encrypted.data.here';
let getKeyStub;
let joseEncryptStub;
let mockEncryptor;
beforeEach(() => {
getKeyStub = sinon.stub(webex.internal.encryption, 'getKey').resolves(mockKey);
// Mock the jose.JWE.createEncrypt chain
mockEncryptor = {
final: sinon.stub().resolves(mockEncryptedJWE)
};
joseEncryptStub = sinon.stub(require('node-jose').JWE, 'createEncrypt').returns(mockEncryptor);
});
it('should call getKey and jose.JWE.createEncrypt with correct parameters', async () => {
await webex.internal.encryption.encryptBinaryData(testKey, testData, testOptions);
assert.equal(getKeyStub.calledOnce, true);
assert.equal(getKeyStub.args[0][0], testKey);
assert.deepEqual(getKeyStub.args[0][1], testOptions);
assert.equal(joseEncryptStub.calledOnce, true);
assert.deepEqual(joseEncryptStub.args[0][1], {
key: mockJwk,
header: {
alg: 'dir',
},
reference: null,
});
});
it('should call final with buffer', async () => {
await webex.internal.encryption.encryptBinaryData(testKey, testData, testOptions);
assert.equal(mockEncryptor.final.calledOnce, true);
assert.equal(Buffer.isBuffer(mockEncryptor.final.args[0][0]), true);
});
it('should return the encrypted JWE string', async () => {
const result = await webex.internal.encryption.encryptBinaryData(testKey, testData, testOptions);
assert.equal(result, mockEncryptedJWE);
assert.equal(typeof result, 'string');
});
it('should work without options parameter', async () => {
await webex.internal.encryption.encryptBinaryData(testKey, testData);
assert.equal(getKeyStub.calledOnce, true);
assert.equal(getKeyStub.args[0][0], testKey);
assert.equal(getKeyStub.args[0][1], undefined);
});
afterEach(() => {
getKeyStub.restore();
joseEncryptStub.restore();
});
});
});
});