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

fix: Rename auth to authClient & Use Generics for AuthClient #1371

Merged
merged 3 commits into from
Feb 17, 2022
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
6 changes: 3 additions & 3 deletions samples/downscopedclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ async function main() {
const cabClient = new DownscopedClient(client, cab);

// OAuth 2.0 Client
const oauth2Client = new OAuth2Client();
const authClient = new OAuth2Client();
// Define a refreshHandler that will be used to refresh the downscoped token
// when it expires.
oauth2Client.refreshHandler = async () => {
authClient.refreshHandler = async () => {
const refreshedAccessToken = await cabClient.getAccessToken();
return {
access_token: refreshedAccessToken.token,
Expand All @@ -77,7 +77,7 @@ async function main() {

const storageOptions = {
projectId,
authClient: new GoogleAuth({auth: oauth2Client}),
authClient: new GoogleAuth({authClient}),
};

const storage = new Storage(storageOptions);
Expand Down
13 changes: 6 additions & 7 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export interface ADCResponse {
projectId: string | null;
}

export interface GoogleAuthOptions {
export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
/**
* An `AuthClient` to use
*/
auth?: AuthClient;
authClient?: T;
/**
* Path to a .json, .pem, or .p12 key file
*/
Expand Down Expand Up @@ -115,7 +115,7 @@ export interface GoogleAuthOptions {
export const CLOUD_SDK_CLIENT_ID =
'764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com';

export class GoogleAuth {
export class GoogleAuth<T extends AuthClient = JSONClient> {
transporter?: Transporter;

/**
Expand All @@ -139,8 +139,7 @@ export class GoogleAuth {
// To save the contents of the JSON credential file
jsonContent: JWTInput | ExternalAccountClientOptions | null = null;

cachedCredential: JSONClient | Impersonated | Compute | AuthClient | null =
null;
cachedCredential: JSONClient | Impersonated | Compute | T | null = null;

/**
* Scopes populated by the client library by default. We differentiate between
Expand All @@ -156,11 +155,11 @@ export class GoogleAuth {
*/
static DefaultTransporter = DefaultTransporter;

constructor(opts?: GoogleAuthOptions) {
constructor(opts?: GoogleAuthOptions<T>) {
opts = opts || {};

this._cachedProjectId = opts.projectId || null;
this.cachedCredential = opts.auth || null;
this.cachedCredential = opts.authClient || null;
this.keyFilename = opts.keyFilename || opts.keyFile;
this.scopes = opts.scopes;
this.jsonContent = opts.credentials || null;
Expand Down
4 changes: 1 addition & 3 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,7 @@ describe('googleauth', () => {

const authClient = new MyAuthClient();

const auth = new GoogleAuth({
auth: authClient,
});
const auth = new GoogleAuth({authClient});

assert.equal(auth.cachedCredential, authClient);
assert.equal(await auth.getClient(), authClient);
Expand Down