diff --git a/src/auth/oauth2client.ts b/src/auth/oauth2client.ts index a365277a..ec2212d7 100644 --- a/src/auth/oauth2client.ts +++ b/src/auth/oauth2client.ts @@ -404,12 +404,6 @@ export interface VerifyIdTokenOptions { maxExpiry?: number; } -export interface OAuth2ClientOptions extends RefreshOptions { - clientId?: string; - clientSecret?: string; - redirectUri?: string; -} - export interface RefreshOptions { // Eagerly refresh unexpired tokens when they are within this many // milliseconds from expiring". @@ -423,6 +417,13 @@ export interface RefreshOptions { forceRefreshOnFailure?: boolean; } +export interface OAuth2ClientOptions extends RefreshOptions { + clientId?: string; + clientSecret?: string; + redirectUri?: string; + credentials?: Credentials; +} + export class OAuth2Client extends AuthClient { private redirectUri?: string; private certificateCache: Certificates = {}; @@ -474,6 +475,7 @@ export class OAuth2Client extends AuthClient { this.eagerRefreshThresholdMillis = opts.eagerRefreshThresholdMillis || 5 * 60 * 1000; this.forceRefreshOnFailure = !!opts.forceRefreshOnFailure; + this.credentials = opts.credentials || {}; } protected static readonly GOOGLE_TOKEN_INFO_URL = diff --git a/test/test.oauth2.ts b/test/test.oauth2.ts index 752c7ee4..146bbe65 100644 --- a/test/test.oauth2.ts +++ b/test/test.oauth2.ts @@ -23,7 +23,7 @@ import * as path from 'path'; import * as qs from 'querystring'; import * as sinon from 'sinon'; -import {CodeChallengeMethod, OAuth2Client} from '../src'; +import {CodeChallengeMethod, Credentials, OAuth2Client} from '../src'; import {LoginTicket} from '../src/auth/loginticket'; nock.disableNetConnect(); @@ -1593,5 +1593,32 @@ describe('oauth2', () => { /No access token is returned by the refreshHandler callback./ ); }); + + it('should accept and attempt to use an `access_token`', async () => { + const credentials: Credentials = {access_token: 'my-access-token'}; + const url = 'http://example.com'; + + const client = new OAuth2Client({credentials}); + + const scope = nock(url, { + reqheaders: { + Authorization: `Bearer ${credentials.access_token}`, + }, + }) + .get('/') + .reply(200); + + // We want the credentials object to be reference equal + assert.strict.equal(client.credentials, credentials); + + await client.request({url}); + + scope.done(); + + // The access token should still be available after use + assert.strict.deepEqual(await client.getAccessToken(), { + token: credentials.access_token, + }); + }); }); });