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 1 commit
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
2 changes: 2 additions & 0 deletions src/auth/baseexternalclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export abstract class BaseExternalAccountClient extends AuthClient {
public projectNumber: string | null;
public readonly eagerRefreshThresholdMillis: number;
public readonly forceRefreshOnFailure: boolean;
public useJWTAccessAlways?: boolean;
public defaultServicePath?: string;

/**
* Instantiate a BaseExternalAccountClient instance using the provided JSON
Expand Down
10 changes: 10 additions & 0 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 @@ -456,6 +458,8 @@ export class GoogleAuth {
} else {
(options as JWTOptions).scopes = this.scopes;
client = new JWT(options);
client.defaultServicePath = this.defaultServicePath;
client.useJWTAccessAlways = this.useJWTAccessAlways;
client.defaultScopes = this.defaultScopes;
client.fromJSON(json);
}
Expand Down Expand Up @@ -489,6 +493,8 @@ export class GoogleAuth {
(options as JWTOptions).scopes = this.scopes;
client = new JWT(options);
client.defaultScopes = this.defaultScopes;
client.defaultServicePath = this.defaultServicePath;
client.useJWTAccessAlways = this.useJWTAccessAlways;
client.fromJSON(json);
}
// cache both raw data used to instantiate client and client itself.
Expand Down Expand Up @@ -564,6 +570,8 @@ export class GoogleAuth {
keyFile: this.keyFilename,
});
this.cachedCredential = client;
client.defaultServicePath = this.defaultServicePath;
client.useJWTAccessAlways = this.useJWTAccessAlways;
return resolve(client);
}
} catch (err) {
Expand All @@ -583,6 +591,8 @@ export class GoogleAuth {
options = options || {};
const client = new JWT(options);
client.fromAPIKey(apiKey);
client.defaultServicePath = this.defaultServicePath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a special edge case where we don't need to set useJWTAccessAlways, where we're providing an explicit API key to use (which I'm betting is why we don't set defaultServicePath either).

client.useJWTAccessAlways = this.useJWTAccessAlways;
return client;
}

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
2 changes: 2 additions & 0 deletions src/auth/refreshclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class UserRefreshClient extends OAuth2Client {
// In a future gts release, the _propertyName rule will be lifted.
// This is also a hard one because `this.refreshToken` is a function.
_refreshToken?: string | null;
public useJWTAccessAlways?: boolean;
public defaultServicePath?: string;

/**
* User Refresh Token credentials.
Expand Down
19 changes: 19 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,25 @@ 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.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.defaultServicePath, 'a/b/c');
});

it('fromAPIKey should set useJWTAccessAlways', () => {
auth.useJWTAccessAlways = true;
const client = auth.fromAPIKey(API_KEY);
assert.ok(client.useJWTAccessAlways);
});

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