-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathapp-check.spec.ts
More file actions
369 lines (333 loc) · 13.6 KB
/
Copy pathapp-check.spec.ts
File metadata and controls
369 lines (333 loc) · 13.6 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*!
* @license
* Copyright 2021 Google LLC
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import * as _ from 'lodash';
import * as chai from 'chai';
import * as sinon from 'sinon';
import * as mocks from '../../resources/mocks';
import { FirebaseApp } from '../../../src/app/firebase-app';
import { AppCheck } from '../../../src/app-check/index';
import { AppCheckApiClient, FirebaseAppCheckError } from '../../../src/app-check/app-check-api-client-internal';
import { AppCheckTokenGenerator } from '../../../src/app-check/token-generator';
import { HttpClient } from '../../../src/utils/api-request';
import { ServiceAccountSigner } from '../../../src/utils/crypto-signer';
import { AppCheckTokenVerifier } from '../../../src/app-check/token-verifier';
const expect = chai.expect;
describe('AppCheck', () => {
const INTERNAL_ERROR = new FirebaseAppCheckError('internal-error', 'message');
const APP_ID = '1:1234:android:1234';
const TEST_TOKEN_TO_EXCHANGE = 'signed-custom-token';
let appCheck: AppCheck;
let mockApp: FirebaseApp;
let mockCredentialApp: FirebaseApp;
// Stubs used to simulate underlying api calls.
let stubs: sinon.SinonStub[] = [];
before(() => {
mockApp = mocks.app();
mockCredentialApp = mocks.mockCredentialApp();
appCheck = new AppCheck(mockApp);
});
after(() => {
return mockApp.delete();
});
afterEach(() => {
_.forEach(stubs, (stub) => stub.restore());
stubs = [];
});
describe('Constructor', () => {
const invalidApps = [null, NaN, 0, 1, true, false, '', 'a', [], [1, 'a'], {}, { a: 1 }, _.noop];
invalidApps.forEach((invalidApp) => {
it('should throw given invalid app: ' + JSON.stringify(invalidApp), () => {
expect(() => {
const appCheckAny: any = AppCheck;
return new appCheckAny(invalidApp);
}).to.throw(
'First argument passed to admin.appCheck() must be a valid Firebase app '
+ 'instance.');
});
});
it('should throw given no app', () => {
expect(() => {
const appCheckAny: any = AppCheck;
return new appCheckAny();
}).to.throw(
'First argument passed to admin.appCheck() must be a valid Firebase app '
+ 'instance.');
});
it('should reject when initialized without project ID', () => {
// Project ID not set in the environment.
delete process.env.GOOGLE_CLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
+ 'account credentials or set project ID as an app option. Alternatively, set the '
+ 'GOOGLE_CLOUD_PROJECT environment variable.';
const appCheckWithoutProjectId = new AppCheck(mockCredentialApp);
const stub = sinon.stub(AppCheckTokenGenerator.prototype, 'createCustomToken')
.resolves(TEST_TOKEN_TO_EXCHANGE);
stubs.push(stub);
return appCheckWithoutProjectId.createToken(APP_ID)
.should.eventually.rejectedWith(noProjectId);
});
it('should reject when failed to contact the Metadata server', () => {
// Remove the Project ID to force a request to the Metadata server
delete process.env.GOOGLE_CLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
const appCheckWithoutProjectId = new AppCheck(mockCredentialApp);
const stub = sinon.stub(HttpClient.prototype, 'send')
.rejects(new Error('network error.'));
stubs.push(stub);
const expected = 'Failed to determine service account. Make sure to initialize the SDK '
+ 'with a service account credential. Alternatively specify a service account with '
+ 'iam.serviceAccounts.signBlob permission. Original error: '
+ 'Error: network error.';
return appCheckWithoutProjectId.createToken(APP_ID)
.should.eventually.be.rejectedWith(expected);
});
it('should reject when failed to sign the token', () => {
const expected = 'sign error';
const stub = sinon.stub(ServiceAccountSigner.prototype, 'sign')
.rejects(new Error(expected));
stubs.push(stub);
return appCheck.createToken(APP_ID)
.should.eventually.be.rejectedWith(expected);
});
it('should not throw given a valid app', () => {
expect(() => {
return new AppCheck(mockApp);
}).not.to.throw();
});
});
describe('app', () => {
it('returns the app from the constructor', () => {
// We expect referential equality here
expect(appCheck.app).to.equal(mockApp);
});
});
describe('createToken', () => {
it('should propagate API errors', () => {
const stub = sinon
.stub(AppCheckApiClient.prototype, 'exchangeToken')
.rejects(INTERNAL_ERROR);
stubs.push(stub);
return appCheck.createToken(APP_ID)
.should.eventually.be.rejected.and.deep.equal(INTERNAL_ERROR);
});
it('should propagate API errors with custom options', () => {
const stub = sinon
.stub(AppCheckApiClient.prototype, 'exchangeToken')
.rejects(INTERNAL_ERROR);
stubs.push(stub);
return appCheck.createToken(APP_ID, { ttlMillis: 1800000 })
.should.eventually.be.rejected.and.deep.equal(INTERNAL_ERROR);
});
it('should resolve with AppCheckToken on success', () => {
const response = { token: 'token', ttlMillis: 3000 };
const stub = sinon
.stub(AppCheckApiClient.prototype, 'exchangeToken')
.resolves(response);
stubs.push(stub);
return appCheck.createToken(APP_ID)
.then((token) => {
expect(token.token).equals('token');
expect(token.ttlMillis).equals(3000);
});
});
it('should resolve with AppCheckToken on success with limitedUse', () => {
const response = { token: 'token', ttlMillis: 3000 };
const stub = sinon
.stub(AppCheckApiClient.prototype, 'exchangeToken')
.resolves(response);
stubs.push(stub);
return appCheck.createToken(APP_ID, { limitedUse: true })
.then((token) => {
expect(token.token).equals('token');
expect(token.ttlMillis).equals(3000);
expect(stub).to.have.been.calledOnce.and.calledWith(sinon.match.string, APP_ID, { limitedUse: true });
});
});
it('should resolve with AppCheckToken on success with limitedUse and jti', () => {
const response = { token: 'token', ttlMillis: 3000 };
const stub = sinon
.stub(AppCheckApiClient.prototype, 'exchangeToken')
.resolves(response);
stubs.push(stub);
return appCheck.createToken(APP_ID, { limitedUse: true, jti: 'test-jti' })
.then((token) => {
expect(token.token).equals('token');
expect(token.ttlMillis).equals(3000);
expect(stub).to.have.been.calledOnce.and.calledWith(
sinon.match.string, APP_ID, { limitedUse: true, jti: 'test-jti' });
});
});
});
describe('verifyToken', () => {
it('should propagate API errors', () => {
const stub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.rejects(INTERNAL_ERROR);
stubs.push(stub);
return appCheck.verifyToken('token')
.should.eventually.be.rejected.and.deep.equal(INTERNAL_ERROR);
});
it('should resolve with VerifyAppCheckTokenResponse on success', () => {
const response = {
sub: 'app-id',
iss: 'https://firebaseappcheck.googleapis.com/123456',
app_id: 'app-id',
aud: ['123456', 'project-id'],
exp: 1617741496,
iat: 1516239022,
};
const stub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.resolves(response);
stubs.push(stub);
return appCheck.verifyToken('token')
.then((tokenResponse) => {
expect(tokenResponse.appId).equals('app-id');
expect(tokenResponse.token).equals(response);
expect(tokenResponse.alreadyConsumed).equals(undefined);
});
});
it('should throw given an invalid options', () => {
[null, 100, -100, 'abc', [], true].forEach((invalidOptions) => {
expect(() => {
return appCheck.verifyToken('token', invalidOptions as any)
}).to.throw(
'VerifyAppCheckTokenOptions must be a non-null object.');
});
});
it('should call verifyReplayProtection when consume is set to true', () => {
const response = {
sub: 'app-id',
iss: 'https://firebaseappcheck.googleapis.com/123456',
app_id: 'app-id',
aud: ['123456', 'project-id'],
exp: 1617741496,
iat: 1516239022,
};
const verifierStub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.resolves(response);
const replayStub = sinon
.stub(AppCheckApiClient.prototype, 'verifyReplayProtection')
.resolves(true);
stubs.push(verifierStub);
stubs.push(replayStub);
return appCheck.verifyToken('token', { consume: true })
.then((tokenResponse) => {
expect(tokenResponse.appId).equals('app-id');
expect(tokenResponse.token).equals(response);
expect(tokenResponse.alreadyConsumed).equals(true);
expect(verifierStub).to.have.been.calledOnce.and.calledWith('token');
expect(replayStub).to.have.been.calledOnce.and.calledWith('token');
});
});
it('should not call verifyReplayProtection when consume is set to false', () => {
const response = {
sub: 'app-id',
iss: 'https://firebaseappcheck.googleapis.com/123456',
app_id: 'app-id',
aud: ['123456', 'project-id'],
exp: 1617741496,
iat: 1516239022,
};
const verifierStub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.resolves(response);
const replayStub = sinon
.stub(AppCheckApiClient.prototype, 'verifyReplayProtection')
.resolves(true);
stubs.push(verifierStub);
stubs.push(replayStub);
return appCheck.verifyToken('token', { consume: false })
.then((tokenResponse) => {
expect(tokenResponse.appId).equals('app-id');
expect(tokenResponse.token).equals(response);
expect(tokenResponse.alreadyConsumed).equals(undefined);
expect(verifierStub).to.have.been.calledOnce.and.calledWith('token');
expect(replayStub).to.not.have.been.called;
});
});
it('should not call verifyReplayProtection when consume is set to undefined', () => {
const response = {
sub: 'app-id',
iss: 'https://firebaseappcheck.googleapis.com/123456',
app_id: 'app-id',
aud: ['123456', 'project-id'],
exp: 1617741496,
iat: 1516239022,
};
const verifierStub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.resolves(response);
const replayStub = sinon
.stub(AppCheckApiClient.prototype, 'verifyReplayProtection')
.resolves(true);
stubs.push(verifierStub);
stubs.push(replayStub);
return appCheck.verifyToken('token', { consume: undefined })
.then((tokenResponse) => {
expect(tokenResponse.appId).equals('app-id');
expect(tokenResponse.token).equals(response);
expect(tokenResponse.alreadyConsumed).equals(undefined);
expect(verifierStub).to.have.been.calledOnce.and.calledWith('token');
expect(replayStub).to.not.have.been.called;
});
});
it('should not call verifyReplayProtection for an invalid token when consume is set to true', () => {
const verifierStub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.rejects(INTERNAL_ERROR);
const replayStub = sinon
.stub(AppCheckApiClient.prototype, 'verifyReplayProtection')
.resolves(true);
stubs.push(verifierStub);
stubs.push(replayStub);
appCheck.verifyToken('token', { consume: true })
.should.eventually.be.rejected.and.deep.equal(INTERNAL_ERROR);
expect(verifierStub).to.have.been.calledOnce.and.calledWith('token');
return expect(replayStub).to.not.have.been.called;
});
it('should resolve with VerifyAppCheckTokenResponse on success with alreadyConsumed set', () => {
const response = {
sub: 'app-id',
iss: 'https://firebaseappcheck.googleapis.com/123456',
app_id: 'app-id',
aud: ['123456', 'project-id'],
exp: 1617741496,
iat: 1516239022,
};
const verifierStub = sinon
.stub(AppCheckTokenVerifier.prototype, 'verifyToken')
.resolves(response);
const replayStub = sinon
.stub(AppCheckApiClient.prototype, 'verifyReplayProtection')
.resolves(false);
stubs.push(verifierStub);
stubs.push(replayStub);
return appCheck.verifyToken('token', { consume: true })
.then((tokenResponse) => {
expect(tokenResponse.appId).equals('app-id');
expect(tokenResponse.token).equals(response);
expect(tokenResponse.alreadyConsumed).equals(false);
expect(verifierStub).to.have.been.calledOnce.and.calledWith('token');
expect(replayStub).to.have.been.calledOnce.and.calledWith('token');
});
});
});
});