@@ -22,7 +22,7 @@ import * as querystring from 'querystring';
22
22
import * as stream from 'stream' ;
23
23
import * as formatEcdsa from 'ecdsa-sig-formatter' ;
24
24
25
- import { createCrypto , JwkCertificate , hasBrowserCrypto } from '../crypto/crypto' ;
25
+ import { createCrypto , JwkCertificate } from '../crypto/crypto' ;
26
26
import { BodyResponseCallback } from '../transporters' ;
27
27
28
28
import { AuthClient , AuthClientOptions } from './authclient' ;
@@ -64,6 +64,9 @@ export enum CodeChallengeMethod {
64
64
}
65
65
66
66
export enum CertificateFormat {
67
+ /**
68
+ * @deprecated - Use JWK.
69
+ */
67
70
PEM = 'PEM' ,
68
71
JWK = 'JWK' ,
69
72
}
@@ -402,6 +405,7 @@ export interface VerifyIdTokenOptions {
402
405
idToken : string ;
403
406
audience ?: string | string [ ] ;
404
407
maxExpiry ?: number ;
408
+ certificateFormat ?: CertificateFormat ;
405
409
}
406
410
407
411
export interface OAuth2ClientEndpoints {
@@ -433,12 +437,12 @@ export interface OAuth2ClientEndpoints {
433
437
* The base endpoint to revoke tokens.
434
438
*
435
439
* @example
436
- * 'https://oauth2.googleapis. com/revoke'
440
+ * 'https://www.accounts.google. com/o/oauth2 /revoke'
437
441
*/
438
442
oauth2RevokeUrl : string | URL ;
439
443
440
444
/**
441
- * Sign on certificates in PEM format.
445
+ * Sign on certificates in the legacy PEM format.
442
446
*
443
447
* @example
444
448
* 'https://www.googleapis.com/oauth2/v1/certs'
@@ -461,6 +465,8 @@ export interface OAuth2ClientEndpoints {
461
465
* 'https://www.gstatic.com/iap/verify/public_key'
462
466
*/
463
467
oauth2IapPublicKeyUrl : string | URL ;
468
+
469
+ [ endpoint : string ] : string | URL ;
464
470
}
465
471
466
472
export interface OAuth2ClientOptions extends AuthClientOptions {
@@ -487,7 +493,7 @@ export class OAuth2Client extends AuthClient {
487
493
private redirectUri ?: string ;
488
494
private certificateCache : Certificates = { } ;
489
495
private certificateExpiry : Date | null = null ;
490
- private certificateCacheFormat : CertificateFormat = CertificateFormat . PEM ;
496
+ private certificateCacheFormat : CertificateFormat = CertificateFormat . JWK ;
491
497
protected refreshTokenPromises = new Map < string , Promise < GetTokenResponse > > ( ) ;
492
498
readonly endpoints : Readonly < OAuth2ClientEndpoints > ;
493
499
readonly issuers : string [ ] ;
@@ -534,7 +540,7 @@ export class OAuth2Client extends AuthClient {
534
540
tokenInfoUrl : 'https://oauth2.googleapis.com/tokeninfo' ,
535
541
oauth2AuthBaseUrl : 'https://accounts.google.com/o/oauth2/v2/auth' ,
536
542
oauth2TokenUrl : 'https://oauth2.googleapis.com/token' ,
537
- oauth2RevokeUrl : 'https://oauth2.googleapis. com/revoke' ,
543
+ oauth2RevokeUrl : 'https://www.accounts.google. com/o/oauth2 /revoke' ,
538
544
oauth2FederatedSignonPemCertsUrl :
539
545
'https://www.googleapis.com/oauth2/v1/certs' ,
540
546
oauth2FederatedSignonJwkCertsUrl :
@@ -659,7 +665,6 @@ export class OAuth2Client extends AuthClient {
659
665
private async getTokenAsync (
660
666
options : GetTokenOptions
661
667
) : Promise < GetTokenResponse > {
662
- const url = this . endpoints . oauth2TokenUrl . toString ( ) ;
663
668
const values = {
664
669
code : options . code ,
665
670
client_id : options . client_id || this . _clientId ,
@@ -670,7 +675,7 @@ export class OAuth2Client extends AuthClient {
670
675
} ;
671
676
const res = await this . transporter . request < CredentialRequest > ( {
672
677
method : 'POST' ,
673
- url,
678
+ url : this . endpoints . oauth2TokenUrl ,
674
679
data : querystring . stringify ( values ) ,
675
680
headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
676
681
} ) ;
@@ -720,7 +725,7 @@ export class OAuth2Client extends AuthClient {
720
725
if ( ! refreshToken ) {
721
726
throw new Error ( 'No refresh token is set.' ) ;
722
727
}
723
- const url = this . endpoints . oauth2TokenUrl . toString ( ) ;
728
+
724
729
const data = {
725
730
refresh_token : refreshToken ,
726
731
client_id : this . _clientId ,
@@ -734,7 +739,7 @@ export class OAuth2Client extends AuthClient {
734
739
// request for new token
735
740
res = await this . transporter . request < CredentialRequest > ( {
736
741
method : 'POST' ,
737
- url,
742
+ url : this . endpoints . oauth2TokenUrl ,
738
743
data : querystring . stringify ( data ) ,
739
744
headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
740
745
} ) ;
@@ -854,7 +859,7 @@ export class OAuth2Client extends AuthClient {
854
859
855
860
protected async getRequestMetadataAsync (
856
861
// eslint-disable-next-line @typescript-eslint/no-unused-vars
857
- url ?: string | null
862
+ url ?: string | URL | null
858
863
) : Promise < RequestMetadataResponse > {
859
864
const thisCreds = this . credentials ;
860
865
if (
@@ -1136,10 +1141,12 @@ export class OAuth2Client extends AuthClient {
1136
1141
if ( ! options . idToken ) {
1137
1142
throw new Error ( 'The verifyIdToken method requires an ID Token' ) ;
1138
1143
}
1139
- const response = await this . getFederatedSignonCertsAsync ( ) ;
1144
+ const { certs} = await this . getFederatedSignonCertsAsync (
1145
+ options . certificateFormat
1146
+ ) ;
1140
1147
const login = await this . verifySignedJwtWithCertsAsync (
1141
1148
options . idToken ,
1142
- response . certs ,
1149
+ certs ,
1143
1150
options . audience ,
1144
1151
this . issuers ,
1145
1152
options . maxExpiry
@@ -1182,54 +1189,52 @@ export class OAuth2Client extends AuthClient {
1182
1189
* are certificates in either PEM or JWK format.
1183
1190
* @param callback Callback supplying the certificates
1184
1191
*/
1185
- getFederatedSignonCerts ( ) : Promise < FederatedSignonCertsResponse > ;
1192
+ getFederatedSignonCerts (
1193
+ format : CertificateFormat
1194
+ ) : Promise < FederatedSignonCertsResponse > ;
1186
1195
getFederatedSignonCerts ( callback : GetFederatedSignonCertsCallback ) : void ;
1187
1196
getFederatedSignonCerts (
1188
- callback ?: GetFederatedSignonCertsCallback
1197
+ callbackOrFormat ?: CertificateFormat | GetFederatedSignonCertsCallback
1189
1198
) : Promise < FederatedSignonCertsResponse > | void {
1190
- if ( callback ) {
1199
+ if ( typeof callbackOrFormat === 'function' ) {
1200
+ const callback = callbackOrFormat ;
1201
+
1191
1202
this . getFederatedSignonCertsAsync ( ) . then (
1192
1203
r => callback ( null , r . certs , r . res ) ,
1193
1204
callback
1194
1205
) ;
1195
1206
} else {
1196
- return this . getFederatedSignonCertsAsync ( ) ;
1207
+ const format = callbackOrFormat ;
1208
+ return this . getFederatedSignonCertsAsync ( format ) ;
1197
1209
}
1198
1210
}
1199
1211
1200
- async getFederatedSignonCertsAsync ( ) : Promise < FederatedSignonCertsResponse > {
1212
+ async getFederatedSignonCertsAsync (
1213
+ format : CertificateFormat = CertificateFormat . JWK
1214
+ ) : Promise < FederatedSignonCertsResponse > {
1201
1215
const nowTime = new Date ( ) . getTime ( ) ;
1202
- const format = hasBrowserCrypto ( )
1203
- ? CertificateFormat . JWK
1204
- : CertificateFormat . PEM ;
1216
+
1205
1217
if (
1206
1218
this . certificateExpiry &&
1207
1219
nowTime < this . certificateExpiry . getTime ( ) &&
1208
1220
this . certificateCacheFormat === format
1209
1221
) {
1210
1222
return { certs : this . certificateCache , format} ;
1211
1223
}
1212
- let res : GaxiosResponse ;
1213
- let url : string ;
1224
+
1225
+ let url : string | URL ;
1214
1226
switch ( format ) {
1215
1227
case CertificateFormat . PEM :
1216
- url = this . endpoints . oauth2FederatedSignonPemCertsUrl . toString ( ) ;
1228
+ url = this . endpoints . oauth2FederatedSignonPemCertsUrl ;
1217
1229
break ;
1218
1230
case CertificateFormat . JWK :
1219
- url = this . endpoints . oauth2FederatedSignonJwkCertsUrl . toString ( ) ;
1231
+ url = this . endpoints . oauth2FederatedSignonJwkCertsUrl ;
1220
1232
break ;
1221
1233
default :
1222
1234
throw new Error ( `Unsupported certificate format ${ format } ` ) ;
1223
1235
}
1224
- try {
1225
- res = await this . transporter . request ( { url} ) ;
1226
- } catch ( e ) {
1227
- if ( e instanceof Error ) {
1228
- e . message = `Failed to retrieve verification certificates: ${ e . message } ` ;
1229
- }
1230
1236
1231
- throw e ;
1232
- }
1237
+ const res : GaxiosResponse = await this . transporter . request ( { url} ) ;
1233
1238
1234
1239
const cacheControl = res ? res . headers [ 'cache-control' ] : undefined ;
1235
1240
let cacheAge = - 1 ;
@@ -1287,10 +1292,11 @@ export class OAuth2Client extends AuthClient {
1287
1292
1288
1293
async getIapPublicKeysAsync ( ) : Promise < IapPublicKeysResponse > {
1289
1294
let res : GaxiosResponse ;
1290
- const url = this . endpoints . oauth2IapPublicKeyUrl . toString ( ) ;
1291
1295
1292
1296
try {
1293
- res = await this . transporter . request ( { url} ) ;
1297
+ res = await this . transporter . request ( {
1298
+ url : this . endpoints . oauth2IapPublicKeyUrl ,
1299
+ } ) ;
1294
1300
} catch ( e ) {
1295
1301
if ( e instanceof Error ) {
1296
1302
e . message = `Failed to retrieve verification certificates: ${ e . message } ` ;
0 commit comments