Skip to content

Commit b5896fc

Browse files
committed
fix auth client init process
1 parent bf3800c commit b5896fc

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/app/credential-internal.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export class ServiceAccountCredential implements Credential {
113113
public readonly clientEmail: string;
114114

115115
private googleAuth: GoogleAuth;
116+
private authClient: AnyAuthClient | undefined;
116117

117118
/**
118119
* Creates a new ServiceAccountCredential from the given parameters.
@@ -141,16 +142,19 @@ export class ServiceAccountCredential implements Credential {
141142
if (this.googleAuth) {
142143
return this.googleAuth;
143144
}
144-
this.googleAuth = populateGoogleAuth(this.serviceAccountPathOrObject, this.httpAgent);
145+
const { auth, client } = populateGoogleAuth(this.serviceAccountPathOrObject, this.httpAgent);
146+
this.googleAuth = auth;
147+
this.authClient = client;
145148
return this.googleAuth;
146149
}
147150

148151
public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
149152
const googleAuth = this.getGoogleAuth();
150-
const client = googleAuth.fromJSON(this.serviceAccountPathOrObject as object);
151-
//const client = await googleAuth.getClient();
152-
await client.getAccessToken();
153-
const credentials = client.credentials;
153+
if (this.authClient === undefined) {
154+
this.authClient = await googleAuth.getClient();
155+
}
156+
await this.authClient.getAccessToken();
157+
const credentials = this.authClient.credentials;
154158
return populateCredential(credentials);
155159
}
156160
}
@@ -219,6 +223,7 @@ class ServiceAccount {
219223
export class RefreshTokenCredential implements Credential {
220224

221225
private googleAuth: GoogleAuth;
226+
private authClient: AnyAuthClient | undefined;
222227

223228
/**
224229
* Creates a new RefreshTokenCredential from the given parameters.
@@ -245,15 +250,19 @@ export class RefreshTokenCredential implements Credential {
245250
if (this.googleAuth) {
246251
return this.googleAuth;
247252
}
248-
this.googleAuth = populateGoogleAuth(this.refreshTokenPathOrObject, this.httpAgent);
253+
const { auth, client } = populateGoogleAuth(this.refreshTokenPathOrObject, this.httpAgent);
254+
this.googleAuth = auth;
255+
this.authClient = client;
249256
return this.googleAuth;
250257
}
251258

252259
public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
253260
const googleAuth = this.getGoogleAuth();
254-
const client = await googleAuth.getClient();
255-
await client.getAccessToken();
256-
const credentials = client.credentials;
261+
if (this.authClient === undefined) {
262+
this.authClient = await googleAuth.getClient();
263+
}
264+
await this.authClient.getAccessToken();
265+
const credentials = this.authClient.credentials;
257266
return populateCredential(credentials);
258267
}
259268
}
@@ -313,6 +322,7 @@ class RefreshToken {
313322
export class ImpersonatedServiceAccountCredential implements Credential {
314323

315324
private googleAuth: GoogleAuth;
325+
private authClient: AnyAuthClient | undefined;
316326

317327
/**
318328
* Creates a new ImpersonatedServiceAccountCredential from the given parameters.
@@ -339,15 +349,19 @@ export class ImpersonatedServiceAccountCredential implements Credential {
339349
if (this.googleAuth) {
340350
return this.googleAuth;
341351
}
342-
this.googleAuth = populateGoogleAuth(this.impersonatedServiceAccountPathOrObject, this.httpAgent);
352+
const { auth, client } = populateGoogleAuth(this.impersonatedServiceAccountPathOrObject, this.httpAgent);
353+
this.googleAuth = auth;
354+
this.authClient = client;
343355
return this.googleAuth;
344356
}
345357

346358
public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
347359
const googleAuth = this.getGoogleAuth();
348-
const client = await googleAuth.getClient();
349-
await client.getAccessToken();
350-
const credentials = client.credentials;
360+
if (this.authClient === undefined) {
361+
this.authClient = await googleAuth.getClient();
362+
}
363+
await this.authClient.getAccessToken();
364+
const credentials = this.authClient.credentials;
351365
return populateCredential(credentials);
352366
}
353367
}
@@ -430,8 +444,10 @@ function copyAttr(to: { [key: string]: any }, from: { [key: string]: any }, key:
430444
/**
431445
* Populate google-auth-library GoogleAuth credentials type.
432446
*/
433-
function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent): GoogleAuth {
434-
const googleAuth = new GoogleAuth({
447+
function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent)
448+
: { auth: GoogleAuth, client: AnyAuthClient | undefined } {
449+
let client: AnyAuthClient | undefined;
450+
const auth = new GoogleAuth({
435451
scopes: SCOPES,
436452
clientOptions: {
437453
transporterOptions: {
@@ -448,9 +464,9 @@ function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent): Google
448464
'Service account must be an object.',
449465
);
450466
}
451-
googleAuth.fromJSON(keyFile);
467+
client = auth.fromJSON(keyFile);
452468
}
453-
return googleAuth;
469+
return { auth, client };
454470
}
455471

456472
/**

0 commit comments

Comments
 (0)