Skip to content

Commit

Permalink
chore: accept options objects in constructors (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jan 9, 2018
1 parent f388b8c commit bc5ddd6
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 118 deletions.
28 changes: 10 additions & 18 deletions examples/jwt.js
Expand Up @@ -13,7 +13,7 @@

'use strict';

const JWTClient = require('../build/src/index').JWT;
const {JWT} = require('../build/src/index');

/**
* The JWT authorization is ideal for performing server-to-server
Expand All @@ -26,23 +26,15 @@ const JWTClient = require('../build/src/index').JWT;
const keys = require('./jwt.keys.json');

async function main() {
try {
const client = await getJWTClient();
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({ url });
console.log(res.data);
} catch (e) {
console.error(e);
}
process.exit();
}

async function getJWTClient() {
const client = new JWTClient(keys.client_email, null, keys.private_key, [
'https://www.googleapis.com/auth/cloud-platform'
]);
const client = new JWT({
email: keys.client_email,
key: keys.private_key,
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
await client.authorize();
return client;
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion src/auth/computeclient.ts
Expand Up @@ -58,7 +58,7 @@ export class Compute extends OAuth2Client {
*/
protected async refreshToken(refreshToken?: string|
null): Promise<GetTokenResponse> {
const url = this.opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
const url = this.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
let res: AxiosResponse<CredentialRequest>|null = null;
// request for new token
try {
Expand Down
45 changes: 33 additions & 12 deletions src/auth/jwtclient.ts
Expand Up @@ -23,12 +23,20 @@ import {GetTokenResponse, OAuth2Client, RequestMetadataResponse} from './oauth2c

const isString = require('lodash.isstring');

export interface JWTOptions {
email?: string;
keyFile?: string;
key?: string;
scopes?: string|string[];
subject?: string;
}

export class JWT extends OAuth2Client {
email?: string;
keyFile?: string|null;
key?: string|null;
scopes?: string|string[]|null;
scope?: string|null;
keyFile?: string;
key?: string;
scopes?: string|string[];
scope?: string;
subject?: string;
gtoken: GoogleToken;

Expand All @@ -44,15 +52,22 @@ export class JWT extends OAuth2Client {
* @param {string=} subject impersonated account's email address.
* @constructor
*/
constructor(options: JWTOptions);
constructor(
email?: string, keyFile?: string, key?: string, scopes?: string|string[],
subject?: string);
constructor(
email?: string, keyFile?: string|null, key?: string|null,
scopes?: string|string[]|null, subject?: string) {
optionsOrEmail?: string|JWTOptions, keyFile?: string, key?: string,
scopes?: string|string[], subject?: string) {
super();
this.email = email;
this.keyFile = keyFile;
this.key = key;
this.scopes = scopes;
this.subject = subject;
const opts = (optionsOrEmail && typeof optionsOrEmail === 'object') ?
optionsOrEmail :
{email: optionsOrEmail, keyFile, key, scopes, subject};
this.email = opts.email;
this.keyFile = opts.keyFile;
this.key = opts.key;
this.scopes = opts.scopes;
this.subject = opts.subject;
this.credentials = {refresh_token: 'jwt-placeholder', expiry_date: 1};
}

Expand All @@ -62,7 +77,13 @@ export class JWT extends OAuth2Client {
* @return {object} The cloned instance.
*/
createScoped(scopes?: string|string[]) {
return new JWT(this.email, this.keyFile, this.key, scopes, this.subject);
return new JWT({
email: this.email,
keyFile: this.keyFile,
key: this.key,
scopes,
subject: this.subject
});
}

/**
Expand Down
41 changes: 32 additions & 9 deletions src/auth/oauth2client.ts
Expand Up @@ -216,11 +216,20 @@ export interface VerifyIdTokenOptions {
maxExpiry?: number;
}

export interface OAuth2ClientOptions {
clientId?: string;
clientSecret?: string;
redirectUri?: string;
authBaseUrl?: string;
tokenUrl?: string;
}

export class OAuth2Client extends AuthClient {
private redirectUri?: string;
private certificateCache: {}|null|undefined = null;
private certificateExpiry: Date|null = null;
protected opts: AuthClientOpts;
protected authBaseUrl?: string;
protected tokenUrl?: string;

// TODO: refactor tests to make this private
_clientId?: string;
Expand All @@ -241,14 +250,28 @@ export class OAuth2Client extends AuthClient {
* @param {Object=} opts optional options for overriding the given parameters.
* @constructor
*/
constructor(options: OAuth2ClientOptions);
constructor(
clientId?: string, clientSecret?: string, redirectUri?: string,
opts: AuthClientOpts = {}) {
opts?: AuthClientOpts);
constructor(
optionsOrClientId?: string|OAuth2ClientOptions, clientSecret?: string,
redirectUri?: string, authClientOpts: AuthClientOpts = {}) {
super();
this._clientId = clientId;
this._clientSecret = clientSecret;
this.redirectUri = redirectUri;
this.opts = opts;
const opts = (optionsOrClientId && typeof optionsOrClientId === 'object') ?
optionsOrClientId :
{
clientId: optionsOrClientId,
clientSecret,
redirectUri,
tokenUrl: authClientOpts.tokenUrl,
authBaseUrl: authClientOpts.authBaseUrl
};
this._clientId = opts.clientId;
this._clientSecret = opts.clientSecret;
this.redirectUri = opts.redirectUri;
this.authBaseUrl = opts.authBaseUrl;
this.tokenUrl = opts.tokenUrl;
this.credentials = {};
}

Expand Down Expand Up @@ -310,7 +333,7 @@ export class OAuth2Client extends AuthClient {
opts.scope = opts.scope.join(' ');
}
const rootUrl =
this.opts.authBaseUrl || OAuth2Client.GOOGLE_OAUTH2_AUTH_BASE_URL_;
this.authBaseUrl || OAuth2Client.GOOGLE_OAUTH2_AUTH_BASE_URL_;

return rootUrl + '?' + querystring.stringify(opts);
}
Expand Down Expand Up @@ -364,7 +387,7 @@ export class OAuth2Client extends AuthClient {

private async getTokenAsync(options: GetTokenOptions):
Promise<GetTokenResponse> {
const url = this.opts.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_;
const url = this.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_;
const values = {
code: options.code,
client_id: this._clientId,
Expand Down Expand Up @@ -396,7 +419,7 @@ export class OAuth2Client extends AuthClient {
*/
protected async refreshToken(refreshToken?: string|
null): Promise<GetTokenResponse> {
const url = this.opts.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_;
const url = this.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_;
const data = {
refresh_token: refreshToken,
client_id: this._clientId,
Expand Down

0 comments on commit bc5ddd6

Please sign in to comment.