Skip to content

Commit

Permalink
feat: Extend API Key Support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead committed Jul 11, 2024
1 parent e31a831 commit c67e15b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
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:
```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
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

0 comments on commit c67e15b

Please sign in to comment.