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 auto auth features #281

Merged
merged 6 commits into from
Feb 20, 2018
Merged

feat: add auto auth features #281

merged 6 commits into from
Feb 20, 2018

Conversation

JustinBeckwith
Copy link
Contributor

@JustinBeckwith JustinBeckwith commented Feb 10, 2018

This adds most of the features available in google-auto-auth.

Resolves #54!

@JustinBeckwith
Copy link
Contributor Author

@stephenplusplus could you take a look?

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 10, 2018
@JustinBeckwith JustinBeckwith requested a review from a team February 10, 2018 04:37
@codecov-io
Copy link

codecov-io commented Feb 10, 2018

Codecov Report

Merging #281 into master will increase coverage by 2.11%.
The diff coverage is 95.58%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #281      +/-   ##
==========================================
+ Coverage   92.21%   94.33%   +2.11%     
==========================================
  Files          13       14       +1     
  Lines         835      900      +65     
  Branches      178      188      +10     
==========================================
+ Hits          770      849      +79     
+ Misses         65       51      -14
Impacted Files Coverage Δ
src/auth/envDetect.ts 93.54% <93.54%> (ø)
src/auth/googleauth.ts 93.67% <97.29%> (+7.83%) ⬆️
src/auth/oauth2client.ts 93.88% <0%> (+0.35%) ⬆️

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 4d0c49d...0241109. Read the comment docs.

@stephenplusplus
Copy link
Contributor

Nice! Would you mind showing some demos of authing with ADC, a key file, credentials object, pem/p12?

@JustinBeckwith
Copy link
Contributor Author

Sure! Does this cover it?

@stephenplusplus
Copy link
Contributor

LGTM. Here's a general look at how this would be used from GC/* libs. Let me know if anything looks wack.

const GoogleAuth = require('google-auth-library').GoogleAuth;

function Service(cfg) {
  // cfg may or may not have a `keyFilename` or `credentials` object:
  this.authClient = new GoogleAuth(cfg);
}

Service.prototype.request = function(reqOpts) {
  this.authClient.authorizeRequest(reqOpts, function(err, authorizedReqOpts) {
    // make request with authorizedReqOpts
  });
};

@JustinBeckwith
Copy link
Contributor Author

Looks great. Only thing to note is that I decided to not support callbacks with new APIs. This is totally up for debate though. Is using promises a showstopper?

@stephenplusplus
Copy link
Contributor

Oops. No, not at all, it shouldn't make a difference 👍

Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

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

Can you add tests?

@@ -0,0 +1,32 @@
// Copyright 2017, Google, Inc.

This comment was marked as spam.

This comment was marked as spam.

let env: GCPEnv;

export async function getEnv() {
if (!env) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

constructor(opts?: GoogleAuthOptions) {
opts = opts || {};
this._cachedProjectId = opts.projectId || null;
this.keyfileName = opts.keyFilename;

This comment was marked as spam.

This comment was marked as spam.

/**
* Path to a .json, .pem, or .p12 key file
*/
keyFilename?: string;

This comment was marked as spam.

This comment was marked as spam.

if (!this.cachedCredential) {
if (this.keyfileName) {
const filePath = path.resolve(process.cwd(), this.keyfileName);
console.log(filePath);

This comment was marked as spam.

This comment was marked as spam.

/**
* Determine the compute environment in which the code is running.
*/
async getEnv(): Promise<GCPEnv> {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

} else if (this.jsonContent) {
this.cachedCredential = await this.fromJSON(this.jsonContent);
} else {
await this.getApplicationDefaultAsync();

This comment was marked as spam.

This comment was marked as spam.

@JustinBeckwith
Copy link
Contributor Author

I think this is in it's final form, and ready for a good look over :) @google/google-node-team

/**
* Import the GoogleAuth library, and create a new GoogleAuth client.
*/
const { auth } = require('google-auth-library');

This comment was marked as spam.

This comment was marked as spam.

await gcpMetadata.instance('attributes/cluster-name');
return true;
} catch (e) {
console.error(e);

This comment was marked as spam.

This comment was marked as spam.

async getClient() {
if (!this.cachedCredential) {
if (this.keyFilename) {
const filePath = path.resolve(process.cwd(), this.keyFilename);

This comment was marked as spam.

This comment was marked as spam.

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.

8 participants