Skip to content

Commit

Permalink
fix auth client init process
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Nov 4, 2024
1 parent bf3800c commit 392987e
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/app/credential-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class ServiceAccountCredential implements Credential {
public readonly clientEmail: string;

private googleAuth: GoogleAuth;
private authClient: AnyAuthClient | undefined;

/**
* Creates a new ServiceAccountCredential from the given parameters.
Expand Down Expand Up @@ -141,16 +142,19 @@ export class ServiceAccountCredential implements Credential {
if (this.googleAuth) {
return this.googleAuth;
}
this.googleAuth = populateGoogleAuth(this.serviceAccountPathOrObject, this.httpAgent);
const { auth, client } = populateGoogleAuth(this.serviceAccountPathOrObject, this.httpAgent);
this.googleAuth = auth;
this.authClient = client;
return this.googleAuth;
}

public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
const googleAuth = this.getGoogleAuth();
const client = googleAuth.fromJSON(this.serviceAccountPathOrObject as object);
//const client = await googleAuth.getClient();
await client.getAccessToken();
const credentials = client.credentials;
if(this.authClient === undefined) {

Check failure on line 153 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / stage_release

Expected space(s) after "if"

Check failure on line 153 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space(s) after "if"

Check failure on line 153 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected space(s) after "if"

Check failure on line 153 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Expected space(s) after "if"
this.authClient = await googleAuth.getClient();
}
await this.authClient.getAccessToken();
const credentials = this.authClient.credentials;
return populateCredential(credentials);
}
}
Expand Down Expand Up @@ -219,6 +223,7 @@ class ServiceAccount {
export class RefreshTokenCredential implements Credential {

private googleAuth: GoogleAuth;
private authClient: AnyAuthClient | undefined;

/**
* Creates a new RefreshTokenCredential from the given parameters.
Expand All @@ -245,15 +250,19 @@ export class RefreshTokenCredential implements Credential {
if (this.googleAuth) {
return this.googleAuth;
}
this.googleAuth = populateGoogleAuth(this.refreshTokenPathOrObject, this.httpAgent);
const { auth, client } = populateGoogleAuth(this.refreshTokenPathOrObject, this.httpAgent);
this.googleAuth = auth;
this.authClient = client;
return this.googleAuth;
}

public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
const googleAuth = this.getGoogleAuth();
const client = await googleAuth.getClient();
await client.getAccessToken();
const credentials = client.credentials;
if(this.authClient === undefined) {

Check failure on line 261 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / stage_release

Expected space(s) after "if"

Check failure on line 261 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space(s) after "if"

Check failure on line 261 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected space(s) after "if"

Check failure on line 261 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Expected space(s) after "if"
this.authClient = await googleAuth.getClient();
}
await this.authClient.getAccessToken();
const credentials = this.authClient.credentials;
return populateCredential(credentials);
}
}
Expand Down Expand Up @@ -313,6 +322,7 @@ class RefreshToken {
export class ImpersonatedServiceAccountCredential implements Credential {

private googleAuth: GoogleAuth;
private authClient: AnyAuthClient | undefined;

/**
* Creates a new ImpersonatedServiceAccountCredential from the given parameters.
Expand All @@ -339,15 +349,19 @@ export class ImpersonatedServiceAccountCredential implements Credential {
if (this.googleAuth) {
return this.googleAuth;
}
this.googleAuth = populateGoogleAuth(this.impersonatedServiceAccountPathOrObject, this.httpAgent);
const { auth, client } = populateGoogleAuth(this.impersonatedServiceAccountPathOrObject, this.httpAgent);
this.googleAuth = auth;
this.authClient = client;
return this.googleAuth;
}

public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
const googleAuth = this.getGoogleAuth();
const client = await googleAuth.getClient();
await client.getAccessToken();
const credentials = client.credentials;
if(this.authClient === undefined) {

Check failure on line 360 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / stage_release

Expected space(s) after "if"

Check failure on line 360 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space(s) after "if"

Check failure on line 360 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected space(s) after "if"

Check failure on line 360 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Expected space(s) after "if"
this.authClient = await googleAuth.getClient();
}
await this.authClient.getAccessToken();
const credentials = this.authClient.credentials;
return populateCredential(credentials);
}
}
Expand Down Expand Up @@ -430,8 +444,9 @@ function copyAttr(to: { [key: string]: any }, from: { [key: string]: any }, key:
/**
* Populate google-auth-library GoogleAuth credentials type.
*/
function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent): GoogleAuth {
const googleAuth = new GoogleAuth({
function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent): { auth: GoogleAuth, client: AnyAuthClient | undefined } {

Check failure on line 447 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / stage_release

This line has a length of 132. Maximum allowed is 120

Check failure on line 447 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

This line has a length of 132. Maximum allowed is 120

Check failure on line 447 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 132. Maximum allowed is 120

Check failure on line 447 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

This line has a length of 132. Maximum allowed is 120
var client;

Check failure on line 448 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / stage_release

Unexpected var, use let or const instead

Check failure on line 448 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected var, use let or const instead

Check failure on line 448 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected var, use let or const instead

Check failure on line 448 in src/app/credential-internal.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Unexpected var, use let or const instead
const auth = new GoogleAuth({
scopes: SCOPES,
clientOptions: {
transporterOptions: {
Expand All @@ -448,9 +463,9 @@ function populateGoogleAuth(keyFile: string | object, httpAgent?: Agent): Google
'Service account must be an object.',
);
}
googleAuth.fromJSON(keyFile);
client = auth.fromJSON(keyFile);
}
return googleAuth;
return { auth, client };
}

/**
Expand Down

0 comments on commit 392987e

Please sign in to comment.