-
Notifications
You must be signed in to change notification settings - Fork 371
/
test.jwt.ts
786 lines (697 loc) Β· 25.2 KB
/
test.jwt.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// Copyright 2013 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.
import * as assert from 'assert';
import {describe, it, beforeEach, afterEach} from 'mocha';
import * as fs from 'fs';
import * as jws from 'jws';
import * as nock from 'nock';
import * as sinon from 'sinon';
import {GoogleAuth, JWT} from '../src';
import {CredentialRequest, JWTInput} from '../src/auth/credentials';
describe('jwt', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const keypair = require('keypair');
const PEM_PATH = './test/fixtures/private.pem';
const PEM_CONTENTS = fs.readFileSync(PEM_PATH, 'utf8');
const P12_PATH = './test/fixtures/key.p12';
nock.disableNetConnect();
// Creates a standard JSON credentials object for testing.
function createJSON() {
return {
private_key_id: 'key123',
private_key: 'privatekey',
client_email: 'hello@youarecool.com',
client_id: 'client123',
type: 'service_account',
};
}
function createRefreshJSON() {
return {
client_secret: 'privatekey',
client_id: 'client123',
refresh_token: 'refreshtoken',
type: 'authorized_user',
};
}
function createGTokenMock(body: CredentialRequest) {
return nock('https://www.googleapis.com')
.post('/oauth2/v4/token')
.reply(200, body);
}
// set up the test json and the jwt instance being tested.
let jwt: JWT;
let json: JWTInput;
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
json = createJSON();
jwt = new JWT();
sandbox = sinon.createSandbox();
});
afterEach(() => {
nock.cleanAll();
sandbox.restore();
});
it('should create a dummy refresh token string', () => {
// It is important that the compute client is created with a refresh token
// value filled in, or else the rest of the logic will not work.
const jwt = new JWT();
assert.strictEqual('jwt-placeholder', jwt.credentials.refresh_token);
});
it('should get an initial access token', done => {
const jwt = new JWT(
'foo@serviceaccount.com',
PEM_PATH,
undefined,
['http://bar', 'http://foo'],
'bar@subjectaccount.com'
);
const scope = createGTokenMock({access_token: 'initial-access-token'});
jwt.authorize((err, creds) => {
scope.done();
assert.strictEqual(err, null);
assert.notStrictEqual(creds, null);
assert.strictEqual('foo@serviceaccount.com', jwt.gtoken!.iss);
assert.strictEqual(PEM_PATH, jwt.gtoken!.keyFile);
assert.strictEqual(
['http://bar', 'http://foo'].join(' '),
jwt.gtoken!.scope
);
assert.strictEqual('bar@subjectaccount.com', jwt.gtoken!.sub);
assert.strictEqual('initial-access-token', jwt.credentials.access_token);
assert.strictEqual(creds!.access_token, jwt.credentials.access_token);
assert.strictEqual(creds!.refresh_token, jwt.credentials.refresh_token);
assert.strictEqual(creds!.token_type, jwt.credentials.token_type);
assert.strictEqual('jwt-placeholder', jwt.credentials.refresh_token);
assert.strictEqual(PEM_CONTENTS, jwt.key);
assert.strictEqual('foo@serviceaccount.com', jwt.email);
done();
});
});
it('should accept scope as string', done => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: 'http://foo',
subject: 'bar@subjectaccount.com',
});
const scope = createGTokenMock({access_token: 'initial-access-token'});
jwt.authorize((err, creds) => {
scope.done();
assert.strictEqual('http://foo', jwt.gtoken!.scope);
done();
});
});
it('can get obtain new access token when scopes are set', done => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const scope = createGTokenMock({access_token: 'initial-access-token'});
jwt.getAccessToken((err, got) => {
scope.done();
assert.strictEqual(null, err, 'no error was expected: got\n' + err);
assert.strictEqual(
'initial-access-token',
got,
'the access token was wrong: ' + got
);
done();
});
});
it('should emit an event for tokens', done => {
const accessToken = 'initial-access-token';
const scope = createGTokenMock({access_token: accessToken});
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt
.on('tokens', tokens => {
assert.strictEqual(tokens.access_token, accessToken);
scope.done();
done();
})
.getAccessToken();
});
it('can obtain new access token when scopes are set', async () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const wantedToken = 'abc123';
const want = `Bearer ${wantedToken}`;
const scope = createGTokenMock({access_token: wantedToken});
const headers = await jwt.getRequestHeaders();
scope.done();
assert.strictEqual(
want,
headers.Authorization,
`the authorization header was wrong: ${headers.Authorization}`
);
});
it('gets a jwt header access token', async () => {
const keys = keypair(512 /* bitsize of private key */);
const email = 'foo@serviceaccount.com';
const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: keys.private,
subject: 'ignored@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const testUri = 'http:/example.com/my_test_service';
const got = await jwt.getRequestHeaders(testUri);
assert.notStrictEqual(null, got, 'the creds should be present');
const decoded = jws.decode(got.Authorization.replace('Bearer ', ''));
assert.deepStrictEqual({alg: 'RS256', typ: 'JWT'}, decoded.header);
const payload = decoded.payload;
assert.strictEqual(email, payload.iss);
assert.strictEqual(email, payload.sub);
assert.strictEqual(testUri, payload.aud);
});
it('gets a jwt header access token with key id', async () => {
const keys = keypair(512 /* bitsize of private key */);
const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: keys.private,
keyId: '101',
subject: 'ignored@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const testUri = 'http:/example.com/my_test_service';
const got = await jwt.getRequestHeaders(testUri);
assert.notStrictEqual(null, got, 'the creds should be present');
const decoded = jws.decode(got.Authorization.replace('Bearer ', ''));
assert.deepStrictEqual(
{alg: 'RS256', typ: 'JWT', kid: '101'},
decoded.header
);
});
it('should accept additionalClaims', async () => {
const keys = keypair(512 /* bitsize of private key */);
const someClaim = 'cat-on-my-desk';
const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: keys.private,
subject: 'ignored@subjectaccount.com',
additionalClaims: {someClaim},
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const testUri = 'http:/example.com/my_test_service';
const got = await jwt.getRequestHeaders(testUri);
assert.notStrictEqual(null, got, 'the creds should be present');
const decoded = jws.decode(got.Authorization.replace('Bearer ', ''));
const payload = decoded.payload;
assert.strictEqual(testUri, payload.aud);
assert.strictEqual(someClaim, payload.someClaim);
});
it('should accept additionalClaims that include a target_audience', async () => {
const keys = keypair(512 /* bitsize of private key */);
const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: keys.private,
subject: 'ignored@subjectaccount.com',
additionalClaims: {target_audience: 'applause'},
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const testUri = 'http:/example.com/my_test_service';
const scope = createGTokenMock({id_token: 'abc123'});
const got = await jwt.getRequestHeaders(testUri);
scope.done();
assert.notStrictEqual(null, got, 'the creds should be present');
const decoded = got.Authorization.replace('Bearer ', '');
assert.strictEqual(decoded, 'abc123');
});
it('should refresh token if missing access token', done => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const scope = createGTokenMock({access_token: 'abc123'});
jwt.request({url: 'http://bar'}, () => {
scope.done();
assert.strictEqual('abc123', jwt.credentials.access_token);
done();
});
});
it('should unify the promise when refreshing the token', async () => {
// Mock a single call to the token server, and 3 calls to the example
// endpoint. This makes sure that refreshToken is called only once.
const scopes = [
createGTokenMock({access_token: 'abc123'}),
nock('http://example.com').get('/').thrice().reply(200),
];
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
await Promise.all([
jwt.request({url: 'http://example.com'}),
jwt.request({url: 'http://example.com'}),
jwt.request({url: 'http://example.com'}),
]);
scopes.forEach(s => s.done());
assert.strictEqual('abc123', jwt.credentials.access_token);
});
it('should clear the cached refresh token promise after completion', async () => {
// Mock 2 calls to the token server and 2 calls to the example endpoint.
// This makes sure that the token endpoint is invoked twice, preventing
// the promise from getting cached for too long.
const scopes = [
createGTokenMock({access_token: 'abc123'}),
createGTokenMock({access_token: 'abc123'}),
nock('http://example.com').get('/').twice().reply(200),
];
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'refresh-token-placeholder'};
await jwt.request({url: 'http://example.com'});
jwt.credentials.access_token = null;
await jwt.request({url: 'http://example.com'});
scopes.forEach(s => s.done());
assert.strictEqual('abc123', jwt.credentials.access_token);
});
it('should refresh token if expired', done => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {
access_token: 'woot',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() - 1000,
};
const scope = createGTokenMock({access_token: 'abc123'});
jwt.request({url: 'http://bar'}, () => {
scope.done();
assert.strictEqual('abc123', jwt.credentials.access_token);
done();
});
});
it('should refresh if access token will expired soon and time to refresh before expiration is set', done => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
eagerRefreshThresholdMillis: 1000,
});
jwt.credentials = {
access_token: 'woot',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() + 800,
};
const scope = createGTokenMock({access_token: 'abc123'});
jwt.request({url: 'http://bar'}, () => {
scope.done();
assert.strictEqual('abc123', jwt.credentials.access_token);
done();
});
});
it('should not refresh if access token will not expire soon and time to refresh before expiration is set', done => {
const scope = createGTokenMock({access_token: 'abc123', expires_in: 10000});
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
eagerRefreshThresholdMillis: 1000,
});
jwt.credentials = {
access_token: 'initial-access-token',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() + 5000,
};
jwt.request({url: 'http://bar'}, () => {
assert.strictEqual('initial-access-token', jwt.credentials.access_token);
assert.strictEqual(false as boolean, scope.isDone());
done();
});
});
it('should refresh token if the server returns 403', done => {
nock('http://example.com').get('/access').twice().reply(403);
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://example.com'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {
access_token: 'woot',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() + 5000,
};
const scope = createGTokenMock({access_token: 'abc123'});
jwt.request({url: 'http://example.com/access'}, () => {
scope.done();
assert.strictEqual('abc123', jwt.credentials.access_token);
done();
});
});
it('should not refresh if not expired', done => {
const scope = createGTokenMock({access_token: 'abc123', expires_in: 10000});
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {
access_token: 'initial-access-token',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() + 5000,
};
jwt.request({url: 'http://bar'}, () => {
assert.strictEqual('initial-access-token', jwt.credentials.access_token);
assert.strictEqual(false as boolean, scope.isDone());
done();
});
});
it('should assume access token is not expired', done => {
const scope = createGTokenMock({access_token: 'abc123', expires_in: 10000});
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {
access_token: 'initial-access-token',
refresh_token: 'jwt-placeholder',
};
jwt.request({url: 'http://bar'}, () => {
assert.strictEqual('initial-access-token', jwt.credentials.access_token);
assert.strictEqual(false as boolean, scope.isDone());
done();
});
});
it('should return expiry_date in milliseconds', async () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: PEM_PATH,
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
jwt.credentials = {refresh_token: 'jwt-placeholder'};
const scope = createGTokenMock({access_token: 'token', expires_in: 100});
jwt.credentials.access_token = null;
await jwt.getRequestHeaders();
scope.done();
const dateInMillis = new Date().getTime();
assert.strictEqual(
dateInMillis.toString().length,
jwt.credentials.expiry_date!.toString().length
);
});
it('createScoped should clone stuff', () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
keyId: '101',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
const clone = jwt.createScoped('x');
assert.strictEqual(jwt.email, clone.email);
assert.strictEqual(jwt.keyFile, clone.keyFile);
assert.strictEqual(jwt.key, clone.key);
assert.strictEqual(jwt.keyId, clone.keyId);
assert.strictEqual(jwt.subject, clone.subject);
});
it('createScoped should handle string scope', () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
const clone = jwt.createScoped('newscope');
assert.strictEqual('newscope', clone.scopes);
});
it('createScoped should handle array scope', () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
const clone = jwt.createScoped(['gorilla', 'chimpanzee', 'orangutan']);
assert.strictEqual(3, clone.scopes!.length);
assert.strictEqual('gorilla', clone.scopes![0]);
assert.strictEqual('chimpanzee', clone.scopes![1]);
assert.strictEqual('orangutan', clone.scopes![2]);
});
it('createScoped should handle null scope', () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
const clone = jwt.createScoped();
assert.strictEqual(undefined, clone.scopes);
});
it('createScoped should set scope when scope was null', () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
subject: 'bar@subjectaccount.com',
});
const clone = jwt.createScoped('hi');
assert.strictEqual('hi', clone.scopes);
});
it('createScoped should handle nulls', () => {
const jwt = new JWT();
const clone = jwt.createScoped('hi');
assert.strictEqual(jwt.email, undefined);
assert.strictEqual(jwt.keyFile, undefined);
assert.strictEqual(jwt.key, undefined);
assert.strictEqual(jwt.subject, undefined);
assert.strictEqual('hi', clone.scopes);
});
it('createScoped should not return the original instance', () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
keyFile: '/path/to/key.pem',
scopes: ['http://bar', 'http://foo'],
subject: 'bar@subjectaccount.com',
});
const clone = jwt.createScoped('hi');
assert.notStrictEqual(jwt, clone);
});
it('fromJson should error on null json', () => {
assert.throws(() => {
// Test verifies invalid parameter tests, which requires cast to any.
// tslint:disable-next-line no-any
(jwt as any).fromJSON(null);
});
});
it('fromJson should error on empty json', () => {
assert.throws(() => {
jwt.fromJSON({});
});
});
it('fromJson should error on missing client_email', () => {
delete json.client_email;
assert.throws(() => {
jwt.fromJSON(json);
});
});
it('fromJson should error on missing private_key', () => {
delete json.private_key;
assert.throws(() => {
jwt.fromJSON(json);
});
});
it('fromJson should create JWT with client_email', () => {
jwt.fromJSON(json);
assert.strictEqual(json.client_email, jwt.email);
});
it('fromJson should create JWT with private_key', () => {
jwt.fromJSON(json);
assert.strictEqual(json.private_key, jwt.key);
});
it('fromJson should create JWT with private_key_id', () => {
jwt.fromJSON(json);
assert.strictEqual(json.private_key_id, jwt.keyId);
});
it('fromJson should create JWT with null scopes', () => {
jwt.fromJSON(json);
assert.strictEqual(undefined, jwt.scopes);
});
it('fromJson should create JWT with null subject', () => {
jwt.fromJSON(json);
assert.strictEqual(undefined, jwt.subject);
});
it('fromJson should create JWT with null keyFile', () => {
jwt.fromJSON(json);
assert.strictEqual(undefined, jwt.keyFile);
});
it('should error on missing client_id', () => {
const json = createRefreshJSON();
delete json.client_id;
const jwt = new JWT();
assert.throws(() => {
jwt.fromJSON(json);
});
});
it('should error on missing client_secret', () => {
const json = createRefreshJSON();
delete json.client_secret;
const jwt = new JWT();
assert.throws(() => {
jwt.fromJSON(json);
});
});
it('should error on missing refresh_token', () => {
const json = createRefreshJSON();
delete json.refresh_token;
const jwt = new JWT();
assert.throws(() => {
jwt.fromJSON(json);
});
});
it('fromStream should error on null stream', done => {
// Test verifies invalid parameter tests, which requires cast to any.
// tslint:disable-next-line no-any
(jwt as any).fromStream(null, (err: Error) => {
assert.strictEqual(true, err instanceof Error);
done();
});
});
it('fromStream should read the stream and create a jwt', done => {
// Read the contents of the file into a json object.
const fileContents = fs.readFileSync(
'./test/fixtures/private.json',
'utf-8'
);
const json = JSON.parse(fileContents);
// Now open a stream on the same file.
const stream = fs.createReadStream('./test/fixtures/private.json');
// And pass it into the fromStream method.
jwt.fromStream(stream, err => {
assert.strictEqual(undefined, err);
// Ensure that the correct bits were pulled from the stream.
assert.strictEqual(json.private_key, jwt.key);
assert.strictEqual(json.private_key_id, jwt.keyId);
assert.strictEqual(json.client_email, jwt.email);
assert.strictEqual(undefined, jwt.keyFile);
assert.strictEqual(undefined, jwt.subject);
assert.strictEqual(undefined, jwt.scopes);
done();
});
});
it('fromAPIKey should error without api key', () => {
assert.throws(() => {
// Test verifies invalid parameter tests, which requires cast to any.
// tslint:disable-next-line no-any
(jwt as any).fromAPIKey(undefined);
});
});
it('fromAPIKey should error with invalid api key type', () => {
const KEY = 'test';
assert.throws(() => {
// Test verifies invalid parameter tests, which requires cast to any.
// tslint:disable-next-line no-any
jwt.fromAPIKey({key: KEY} as any);
});
});
it('fromAPIKey should set the .apiKey property on the instance', () => {
const KEY = 'test';
jwt.fromAPIKey(KEY);
assert.strictEqual(jwt.apiKey, KEY);
});
it('getCredentials should handle a key', async () => {
const jwt = new JWT({key: PEM_CONTENTS});
const {private_key} = await jwt.getCredentials();
assert.strictEqual(private_key, PEM_CONTENTS);
});
it('getCredentials should handle a p12 keyFile', async () => {
const jwt = new JWT({keyFile: P12_PATH});
const {private_key, client_email} = await jwt.getCredentials();
assert(private_key);
assert.strictEqual(client_email, undefined);
});
it('getCredentials should handle a json keyFile', async () => {
const jwt = new JWT();
jwt.fromJSON(json);
const {private_key, client_email} = await jwt.getCredentials();
assert.strictEqual(private_key, json.private_key);
assert.strictEqual(client_email, json.client_email);
});
it('getRequestHeaders populates x-goog-user-project for JWT client', async () => {
const auth = new GoogleAuth({
credentials: Object.assign(
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../../test/fixtures/service-account-with-quota.json'),
{
private_key: keypair(512 /* bitsize of private key */).private,
}
),
});
const client = await auth.getClient();
assert(client instanceof JWT);
// If a URL isn't provided to authorize, the OAuth2Client super class is
// executed, which was already exercised.
const headers = await client.getRequestHeaders(
'http:/example.com/my_test_service'
);
assert.strictEqual(headers['x-goog-user-project'], 'fake-quota-project');
});
it('should return an ID token for fetchIdToken', async () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: PEM_CONTENTS,
subject: 'ignored@subjectaccount.com',
});
const scope = createGTokenMock({id_token: 'abc123'});
const idtoken = await jwt.fetchIdToken('a-target-audience');
scope.done();
assert.strictEqual(idtoken, 'abc123');
});
it('should throw an error if ID token is not set', async () => {
const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: PEM_CONTENTS,
subject: 'ignored@subjectaccount.com',
});
const scope = createGTokenMock({access_token: 'a-token'});
try {
await jwt.fetchIdToken('a-target-audience');
} catch {
scope.done();
return;
}
assert.fail('failed to throw');
});
});