-
Notifications
You must be signed in to change notification settings - Fork 378
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
Conversation
@stephenplusplus could you take a look? |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
Nice! Would you mind showing some demos of authing with ADC, a key file, credentials object, pem/p12? |
Sure! Does this cover it? |
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
});
}; |
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? |
Oops. No, not at all, it shouldn't make a difference 👍 |
There was a problem hiding this 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?
examples/auto/adc.js
Outdated
@@ -0,0 +1,32 @@ | |||
// Copyright 2017, Google, Inc. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/envDetect.ts
Outdated
let env: GCPEnv; | ||
|
||
export async function getEnv() { | ||
if (!env) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
/** | ||
* Path to a .json, .pem, or .p12 key file | ||
*/ | ||
keyFilename?: string; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
/** | ||
* 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
} 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/envDetect.ts
Outdated
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This adds most of the features available in google-auto-auth.
Resolves #54!