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: Extend API Key Support #1835

Merged
merged 29 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c67e15b
feat: Extend API Key Support
d-goog Jul 11, 2024
cfcc2a2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 11, 2024
0b1710e
feat: Support `apiKey` as an ADC fallback
d-goog Jul 11, 2024
e54f54a
Merge branch 'extend-apikey-support' of github.com:googleapis/google-…
d-goog Jul 11, 2024
ae834cb
Merge branch 'main' of github.com:googleapis/google-auth-library-node…
d-goog Jul 11, 2024
d6c51f9
refactor: Move `apiKey` to base client options
d-goog Aug 2, 2024
7a5c54c
docs: clarity
d-goog Aug 2, 2024
247f384
refactor: API Key Support
d-goog Aug 2, 2024
81a10b9
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 2, 2024
70b7eb6
fix: type
d-goog Aug 2, 2024
ecc40ef
Merge branch 'extend-apikey-support' of github.com:googleapis/google-…
d-goog Aug 2, 2024
65c69a7
feat: Export Error Messages
d-goog Aug 2, 2024
d4bf043
test: Add tests for API Key Support
d-goog Aug 2, 2024
db65b50
Merge branch 'main' of github.com:googleapis/google-auth-library-node…
d-goog Aug 2, 2024
66829d8
test: cleanup
d-goog Aug 2, 2024
5f47a63
docs: Clarifications
d-goog Aug 2, 2024
12ae76c
refactor: streamline
d-goog Aug 6, 2024
f5779bf
Merge branch 'main' of github.com:googleapis/google-auth-library-node…
d-goog Aug 14, 2024
0bba085
chore: merge cleanup
d-goog Aug 14, 2024
5f9eaef
Merge branch 'main' into extend-apikey-support
d-goog Aug 16, 2024
4191d61
docs(sample): Add API Key Sample
d-goog Aug 16, 2024
70ab441
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 16, 2024
e4d3e5d
chore: OCD
d-goog Aug 16, 2024
1bd5a38
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 16, 2024
b0ce42f
Apply suggestions from code review
d-goog Aug 19, 2024
e6dfc6d
Merge branch 'main' into extend-apikey-support
d-goog Aug 19, 2024
4eba6d2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 19, 2024
b7d6a91
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 19, 2024
16521ce
Merge branch 'extend-apikey-support' of https://github.com/googleapis…
gcf-owl-bot[bot] Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .readme-partials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ body: |-

This method will throw if the token is invalid.

#### Using an API Key

An API key can be provided to the constructor:
```js
const client = new OAuth2Client({
apiKey: 'my-api-key'
});
```

Note, classes that extend from this can utilize this parameter as well, such as `JWT` and `UserRefreshClient`.

Additionally, an API key it can be used in `GoogleAuth` via the `clientOptions` parameter and will be passed to any generated `OAuth2Client` instances:
danielbankhead marked this conversation as resolved.
Show resolved Hide resolved
```js
const auth = new GoogleAuth({
clientOptions: {
apiKey: 'my-api-key'
}
})
```

danielbankhead marked this conversation as resolved.
Show resolved Hide resolved
## JSON Web Tokens
The Google Developers Console provides a `.json` file that you can use to configure a JWT auth client and authenticate your requests, for example when using a service account.

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ console.log(tokenInfo.scopes);

This method will throw if the token is invalid.

#### Using an API Key

An API key can be provided to the constructor:
```js
const client = new OAuth2Client({
apiKey: 'my-api-key'
});
```

Note, classes that extend from this can utilize this parameter as well, such as `JWT` and `UserRefreshClient`.
danielbankhead marked this conversation as resolved.
Show resolved Hide resolved

Additionally, an API key it can be used in `GoogleAuth` via the `clientOptions` parameter and will be passed to any generated `OAuth2Client` instances:
```js
const auth = new GoogleAuth({
clientOptions: {
apiKey: 'my-api-key'
}
})
```

## JSON Web Tokens
The Google Developers Console provides a `.json` file that you can use to configure a JWT auth client and authenticate your requests, for example when using a service account.

Expand Down
9 changes: 8 additions & 1 deletion src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
}

private async getApplicationDefaultAsync(
options: AuthClientOptions = {}
options: AuthClientOptions | OAuth2ClientOptions = {}
): Promise<ADCResponse> {
// If we've already got a cached credential, return it.
// This will also preserve one's configured quota project, in case they
Expand Down Expand Up @@ -451,6 +451,13 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
);
}

// No ADC, but perhaps there's an API key available...
if ('apiKey' in options && options.apiKey) {
danielbankhead marked this conversation as resolved.
Show resolved Hide resolved
const client = await this.fromAPIKey(options.apiKey, options);
client.scopes = this.scopes;
return await this.prepareAndCacheADC(client, quotaProjectIdOverride);
}

throw new Error(
'Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.'
);
Expand Down
5 changes: 5 additions & 0 deletions src/auth/oauth2client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,10 @@ export interface OAuth2ClientEndpoints {
}

export interface OAuth2ClientOptions extends AuthClientOptions {
/**
* An API key to use, optional.
*/
apiKey?: string;
clientId?: string;
clientSecret?: string;
redirectUri?: string;
Expand Down Expand Up @@ -559,6 +563,7 @@ export class OAuth2Client extends AuthClient {
this._clientId = opts.clientId;
this._clientSecret = opts.clientSecret;
this.redirectUri = opts.redirectUri;
this.apiKey = opts.apiKey;

this.endpoints = {
tokenInfoUrl: 'https://oauth2.googleapis.com/tokeninfo',
Expand Down
4 changes: 3 additions & 1 deletion test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,12 +1423,14 @@ describe('googleauth', () => {
});

it('should pass options to the JWT constructor via constructor', async () => {
const apiKey = 'my-api-key';
const subject = 'science!';
const auth = new GoogleAuth({
keyFilename: './test/fixtures/private.json',
clientOptions: {subject},
clientOptions: {apiKey, subject},
});
const client = (await auth.getClient()) as JWT;
assert.strictEqual(client.apiKey, apiKey);
assert.strictEqual(client.subject, subject);
});

Expand Down
7 changes: 7 additions & 0 deletions test/test.oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ describe('oauth2', () => {
sandbox.restore();
});

it('should accept and set an `apiKey`', () => {
const API_KEY = 'TEST_API_KEY';
const client = new OAuth2Client({apiKey: API_KEY});

assert.equal(client.apiKey, API_KEY);
});

it('should generate a valid consent page url', done => {
const opts = {
access_type: ACCESS_TYPE,
Expand Down
Loading