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!: expose GoogleAuth constructor on AuthPlus class #154

Merged
merged 3 commits into from Jul 24, 2019

Conversation

bcoe
Copy link
Contributor

@bcoe bcoe commented Jul 24, 2019

BREAKING CHANGE: pulls in breaking API changes in google-auth-library. getProjectId() and getProjectId() have been modified to make the impact of these changes less noticeable on the legacy googleapis module (getClient() is idempotent, but getProjectId() will use the last configuration).

At the end of the day, this allows us to avoid updating cloud.google.com and a a large number of auto generated samples that follow this format (not to mention stack overflow):

     * function authorize(callback) {
     *   google.auth.getClient({
     *     scopes: ['https://www.googleapis.com/auth/cloud-platform']
     *   }).then(client => {
     *     callback(client);
     *   }).catch(err => {
     *     console.error('authentication failed: ', err);
     *   });
     * }

I'm hoping this will give us the best of both worlds:

  • where gRPC clients move towards the fully idempotent API (along with clients like PubSub).
  • documentation in googleapis can be updated to teach the new API surface, however.
  • we avoid breaking gobs of legacy code floating around a variety of tutorials.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jul 24, 2019
@codecov
Copy link

codecov bot commented Jul 24, 2019

Codecov Report

Merging #154 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #154   +/-   ##
=======================================
  Coverage   71.42%   71.42%           
=======================================
  Files           2        2           
  Lines         119      119           
  Branches       38       38           
=======================================
  Hits           85       85           
  Misses         16       16           
  Partials       18       18

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d77c70e...7ea15d1. Read the comment docs.

Copy link
Contributor

@JustinBeckwith JustinBeckwith left a comment

Choose a reason for hiding this comment

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

I think I'm just more confused than anything about what's happening here

src/authplus.ts Outdated
OAuth2Client,
UserRefreshClient,
} from 'google-auth-library';
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
Copy link
Contributor

Choose a reason for hiding this comment

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

Could I trouble you to go back into google-auth-library, and land a PR to export this properly?

* Override getClient(), memoizing an instance of auth for
* subsequent calls to getProjectId().
*/
async getClient(
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this exactly the kind of thing we want to avoid? Is this a method we would expect end users to consume?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want people to use this method, but I think it's a bit too sweeping to remove the method at this time. Here's why I think this implementation, for the benefit of googlepapis, is a bit better:

  1. if I call getClient() multiple times, the prior call will not have an effect on the values returned by the second call (because a new GoogleAuth() is created in between).
  2. if I call getClient(opts); getProjectId(); getClient(); getProjectId() the cached response from the first getClient won't have an effect on the second getProjectId().

src/authplus.ts Outdated Show resolved Hide resolved
@@ -20,4 +28,37 @@ export class AuthPlus extends GoogleAuth {
Compute = Compute;
// tslint:disable-next-line: variable-name
OAuth2 = OAuth2Client;
// tslint:disable-next-line: variable-name
GoogleAuth = GoogleAuth;
Copy link
Contributor

Choose a reason for hiding this comment

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

So this class extends GoogleAuth, and we're adding a reference to GoogleAuth itself? My head is spinnin a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm adding a reference to GoogleAuth, for the same reason that we added Oauth2, and Compute, so that we can advertise the approach:

const auth = new google.auth.GoogleAuth(opts);
const client = auth.getClient();

This allows me to advocate this methodology in the README as the safe approach to using the client going forward, also the API surface ends up being more consistent:

const oauth2Client = new google.auth.OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);

@bcoe bcoe merged commit 7d7a961 into master Jul 24, 2019
@bcoe bcoe deleted the update-google-auth branch July 24, 2019 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants