From 699282f726871318ec44d03beb73fb7ed8e25480 Mon Sep 17 00:00:00 2001 From: hideto0710 Date: Sat, 16 Jan 2021 15:07:50 +0900 Subject: [PATCH 1/3] call addSharedMetadataHeaders --- src/auth/oauth2client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auth/oauth2client.ts b/src/auth/oauth2client.ts index 47809098..20572d33 100644 --- a/src/auth/oauth2client.ts +++ b/src/auth/oauth2client.ts @@ -786,7 +786,7 @@ export class OAuth2Client extends AuthClient { const headers = { Authorization: thisCreds.token_type + ' ' + thisCreds.access_token, }; - return {headers}; + return {headers: this.addSharedMetadataHeaders(headers)}; } if (this.apiKey) { From 9b83b6dcb74359ce2c664fd6a8f94318b19208b2 Mon Sep 17 00:00:00 2001 From: hideto0710 Date: Sun, 17 Jan 2021 09:01:31 +0900 Subject: [PATCH 2/3] add test --- test/test.refresh.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test.refresh.ts b/test/test.refresh.ts index 8fd59cc5..ecc72e51 100644 --- a/test/test.refresh.ts +++ b/test/test.refresh.ts @@ -152,4 +152,22 @@ describe('refresh', () => { assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project'); req.done(); }); + + it('getRequestHeaders should populate x-goog-user-project header if quota_project_id present and token has not expired', async () => { + const stream = fs.createReadStream( + './test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json' + ); + const eagerRefreshThresholdMillis = 10; + const refresh = new UserRefreshClient({ + eagerRefreshThresholdMillis, + }); + await refresh.fromStream(stream); + refresh.credentials = { + access_token: 'woot', + refresh_token: 'jwt-placeholder', + expiry_date: new Date().getTime() + eagerRefreshThresholdMillis + 1000, + }; + const headers = await refresh.getRequestHeaders(); + assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project'); + }); }); From e9d301c4dd200e598b0ad262d14f4cc4b9541899 Mon Sep 17 00:00:00 2001 From: hideto0710 Date: Fri, 22 Jan 2021 19:50:37 +0900 Subject: [PATCH 3/3] add test for expired token --- test/test.refresh.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test.refresh.ts b/test/test.refresh.ts index ecc72e51..2e06d7de 100644 --- a/test/test.refresh.ts +++ b/test/test.refresh.ts @@ -170,4 +170,23 @@ describe('refresh', () => { const headers = await refresh.getRequestHeaders(); assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project'); }); + + it('getRequestHeaders should populate x-goog-user-project header if quota_project_id present and token has expired', async () => { + const req = nock('https://oauth2.googleapis.com') + .post('/token') + .reply(200, {}); + const stream = fs.createReadStream( + './test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json' + ); + const refresh = new UserRefreshClient(); + await refresh.fromStream(stream); + refresh.credentials = { + access_token: 'woot', + refresh_token: 'jwt-placeholder', + expiry_date: new Date().getTime() - 1, + }; + const headers = await refresh.getRequestHeaders(); + assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project'); + req.done(); + }); });