Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useJWTAccessAlways and defaultServicePath variable #1204

Merged
merged 5 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ package-lock.json
yarn.lock
dist/
__pycache__
.DS_Store
18 changes: 14 additions & 4 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export class GoogleAuth {
* @private
*/
private checkIsGCE?: boolean = undefined;
useJWTAccessAlways?: boolean;
defaultServicePath?: string;

// Note: this properly is only public to satisify unit tests.
// https://github.com/Microsoft/TypeScript/issues/5228
Expand Down Expand Up @@ -150,6 +152,15 @@ export class GoogleAuth {
this.clientOptions = opts.clientOptions;
}

// GAPIC client libraries should always use self-signed JWTs. The following
// variables are set on the JWT client in order to indicate the type of library,
// and sign the JWT with the correct audience and scopes (if not supplied).
setGapicJWTValues(client: JWT) {
client.defaultServicePath = this.defaultServicePath;
client.useJWTAccessAlways = this.useJWTAccessAlways;
client.defaultScopes = this.defaultScopes;
}

/**
* Obtains the default project ID for the application.
* @param callback Optional callback
Expand Down Expand Up @@ -265,7 +276,6 @@ export class GoogleAuth {
await this._tryGetApplicationCredentialsFromEnvironmentVariable(options);
if (credential) {
if (credential instanceof JWT) {
credential.defaultScopes = this.defaultScopes;
credential.scopes = this.scopes;
} else if (credential instanceof BaseExternalAccountClient) {
credential.scopes = this.getAnyScopes();
Expand All @@ -281,7 +291,6 @@ export class GoogleAuth {
);
if (credential) {
if (credential instanceof JWT) {
credential.defaultScopes = this.defaultScopes;
credential.scopes = this.scopes;
} else if (credential instanceof BaseExternalAccountClient) {
credential.scopes = this.getAnyScopes();
Expand Down Expand Up @@ -456,7 +465,7 @@ export class GoogleAuth {
} else {
(options as JWTOptions).scopes = this.scopes;
client = new JWT(options);
client.defaultScopes = this.defaultScopes;
this.setGapicJWTValues(client);
client.fromJSON(json);
}
return client;
Expand Down Expand Up @@ -488,7 +497,7 @@ export class GoogleAuth {
} else {
(options as JWTOptions).scopes = this.scopes;
client = new JWT(options);
client.defaultScopes = this.defaultScopes;
this.setGapicJWTValues(client);
client.fromJSON(json);
}
// cache both raw data used to instantiate client and client itself.
Expand Down Expand Up @@ -564,6 +573,7 @@ export class GoogleAuth {
keyFile: this.keyFilename,
});
this.cachedCredential = client;
this.setGapicJWTValues(client);
return resolve(client);
}
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/auth/jwtclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class JWT extends OAuth2Client implements IdTokenProvider {
subject?: string;
gtoken?: GoogleToken;
additionalClaims?: {};

useJWTAccessAlways?: boolean;
defaultServicePath?: string;
private access?: JWTAccess;

/**
Expand Down
13 changes: 13 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@ describe('googleauth', () => {
const result = auth.fromJSON(json);
assert.strictEqual(undefined, (result as JWT).scopes);
});
it('fromJSON should set useJWTAccessAlways with private key', () => {
auth.useJWTAccessAlways = true;
const json = createJwtJSON();
const result = auth.fromJSON(json);
assert.ok((result as JWT).useJWTAccessAlways);
});

it('fromJSON should set default service path with private key', () => {
auth.defaultServicePath = 'a/b/c';
const json = createJwtJSON();
const result = auth.fromJSON(json);
assert.strictEqual((result as JWT).defaultServicePath, 'a/b/c');
});

it('fromJSON should create JWT with null subject', () => {
const json = createJwtJSON();
Expand Down