Skip to content

Commit

Permalink
feat: allow passing constructor options to getClient (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Jan 31, 2019
1 parent a4618f9 commit 9d97aeb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import {Compute} from './computeclient';
import {CredentialBody, JWTInput} from './credentials';
import {GCPEnv, getEnv} from './envDetect';
import {JWT, JWTOptions} from './jwtclient';
import {Headers, OAuth2Client, RefreshOptions} from './oauth2client';
import {UserRefreshClient} from './refreshclient';
import {Headers, OAuth2Client, OAuth2ClientOptions, RefreshOptions} from './oauth2client';
import {UserRefreshClient, UserRefreshClientOptions} from './refreshclient';

export interface ProjectIdCallback {
(err?: Error|null, projectId?: string|null): void;
Expand Down Expand Up @@ -67,6 +67,11 @@ export interface GoogleAuthOptions {
*/
credentials?: CredentialBody;

/**
* Options object passed to the constructor of the client
*/
clientOptions?: JWTOptions|OAuth2ClientOptions|UserRefreshClientOptions;

/**
* Required scopes for the desired API request
*/
Expand Down Expand Up @@ -107,6 +112,7 @@ export class GoogleAuth {

private keyFilename?: string;
private scopes?: string|string[];
private clientOptions?: RefreshOptions;

/**
* Export DefaultTransporter as a static property of the class.
Expand All @@ -119,6 +125,7 @@ export class GoogleAuth {
this.keyFilename = opts.keyFilename || opts.keyFile;
this.scopes = opts.scopes;
this.jsonContent = opts.credentials || null;
this.clientOptions = opts.clientOptions;
}

/**
Expand Down Expand Up @@ -395,6 +402,7 @@ export class GoogleAuth {
/**
* Create a credentials instance using the given input options.
* @param json The input object.
* @param options The JWT or UserRefresh options for the client
* @returns JWT or UserRefresh Client with data
*/
fromJSON(json: JWTInput, options?: RefreshOptions): JWT|UserRefreshClient {
Expand Down Expand Up @@ -678,16 +686,19 @@ export class GoogleAuth {
options.keyFilename || options.keyFile || this.keyFilename;
this.scopes = options.scopes || this.scopes;
this.jsonContent = options.credentials || this.jsonContent;
this.clientOptions = options.clientOptions;
}
if (!this.cachedCredential) {
if (this.jsonContent) {
this.cachedCredential = await this.fromJSON(this.jsonContent);
this.cachedCredential =
await this.fromJSON(this.jsonContent, this.clientOptions);
} else if (this.keyFilename) {
const filePath = path.resolve(this.keyFilename);
const stream = fs.createReadStream(filePath);
this.cachedCredential = await this.fromStreamAsync(stream);
this.cachedCredential =
await this.fromStreamAsync(stream, this.clientOptions);
} else {
await this.getApplicationDefaultAsync();
await this.getApplicationDefaultAsync(this.clientOptions);
}
}
return this.cachedCredential!;
Expand Down
17 changes: 17 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1401,4 +1401,21 @@ describe('googleauth', () => {
await auth.getDefaultProjectId();
assert.strictEqual(count, 0);
});

it('should pass options to the JWT constructor via getClient', async () => {
const subject = 'science!';
const auth = new GoogleAuth({keyFilename: './test/fixtures/private.json'});
const client = await auth.getClient({clientOptions: {subject}}) as JWT;
assert.strictEqual(client.subject, subject);
});

it('should pass options to the JWT constructor via constructor', async () => {
const subject = 'science!';
const auth = new GoogleAuth({
keyFilename: './test/fixtures/private.json',
clientOptions: {subject}
});
const client = await auth.getClient() as JWT;
assert.strictEqual(client.subject, subject);
});
});

0 comments on commit 9d97aeb

Please sign in to comment.