diff --git a/src/service.ts b/src/service.ts index e2d5f312..c262039c 100644 --- a/src/service.ts +++ b/src/service.ts @@ -69,6 +69,7 @@ export interface ServiceOptions extends GoogleAuthOptions { token?: string; timeout?: number; // http.request.options.timeout userAgent?: string; + useAuthWithCustomEndpoint?: boolean; } export class Service { diff --git a/src/util.ts b/src/util.ts index 189fa553..88f756bf 100644 --- a/src/util.ts +++ b/src/util.ts @@ -138,6 +138,11 @@ export interface MakeAuthenticatedRequestFactoryConfig */ customEndpoint?: boolean; + /** + * If true, will authenticate when using a custom endpoint. Default: false. + */ + useAuthWithCustomEndpoint?: boolean; + /** * Account email address, required for PEM/P12 usage. */ @@ -572,6 +577,7 @@ export class Util { * (default: true) * @param {object=} config.credentials - Credentials object. * @param {boolean=} config.customEndpoint - If true, just return the provided request options. Default: false. + * @param {boolean=} config.useAuthWithCustomEndpoint - If true, will authenticate when using a custom endpoint. Default: false. * @param {string=} config.email - Account email address, required for PEM/P12 usage. * @param {number=} config.maxRetries - Maximum number of automatic retries attempted before returning the error. (default: 3) * @param {string=} config.keyFile - Path to a .json, .pem, or .p12 keyfile. @@ -697,7 +703,7 @@ export class Util { // auth client, it could be incorrect. new Promise(resolve => resolve(config.projectId)) : authClient.getProjectId(), - reqConfig.customEndpoint + reqConfig.customEndpoint && reqConfig.useAuthWithCustomEndpoint !== true ? // Using a custom API override. Do not use `google-auth-library` for // authentication. (ex: connecting to a local Datastore server) new Promise(resolve => resolve(reqOpts)) diff --git a/test/util.ts b/test/util.ts index 56cce4b9..0d16f944 100644 --- a/test/util.ts +++ b/test/util.ts @@ -835,6 +835,33 @@ describe('common/util', () => { }); }); + describe('customEndpoint (authentication attempted)', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let makeAuthenticatedRequest: any; + const config = {customEndpoint: true, useAuthWithCustomEndpoint: true}; + + beforeEach(() => { + sandbox.stub(fakeGoogleAuth, 'GoogleAuth').returns(authClient); + makeAuthenticatedRequest = util.makeAuthenticatedRequestFactory(config); + }); + + it('should authenticate requests with a custom API', done => { + const reqOpts = {a: 'b', c: 'd'}; + + stub('makeRequest', rOpts => { + assert.deepStrictEqual(rOpts, reqOpts); + done(); + }); + + authClient.authorizeRequest = async (opts: {}) => { + assert.strictEqual(opts, reqOpts); + done(); + }; + + makeAuthenticatedRequest(reqOpts, assert.ifError); + }); + }); + describe('needs authentication', () => { it('should pass correct args to authorizeRequest', done => { const fake = extend(true, authClient, {