From 90228e9bbac05276b51e6dff5a69cf24a5dc091e Mon Sep 17 00:00:00 2001 From: Rich Gwozdz Date: Fri, 19 Apr 2024 11:00:33 -0700 Subject: [PATCH] fix: two auth bugs (#980) * fix: auth bugs --- .changeset/rich-numbers-judge.md | 5 +++ .changeset/silver-eagles-explain.md | 5 +++ .../core/src/data-provider/extend-model.js | 2 +- packages/output-geoservices/README.md | 3 ++ packages/output-geoservices/src/index.js | 13 +++++--- packages/output-geoservices/src/index.spec.js | 31 +++++++++++++++---- 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 .changeset/rich-numbers-judge.md create mode 100644 .changeset/silver-eagles-explain.md diff --git a/.changeset/rich-numbers-judge.md b/.changeset/rich-numbers-judge.md new file mode 100644 index 000000000..e78780883 --- /dev/null +++ b/.changeset/rich-numbers-judge.md @@ -0,0 +1,5 @@ +--- +"@koopjs/koop-core": patch +--- + +- auth error code can be set from provider but defaults to 401 diff --git a/.changeset/silver-eagles-explain.md b/.changeset/silver-eagles-explain.md new file mode 100644 index 000000000..80caa2477 --- /dev/null +++ b/.changeset/silver-eagles-explain.md @@ -0,0 +1,5 @@ +--- +"@koopjs/output-geoservices": patch +--- + +- owningSystemUrl inclusion is a constructor option diff --git a/packages/core/src/data-provider/extend-model.js b/packages/core/src/data-provider/extend-model.js index 4b1c523fe..ab8abde5f 100644 --- a/packages/core/src/data-provider/extend-model.js +++ b/packages/core/src/data-provider/extend-model.js @@ -205,7 +205,7 @@ module.exports = function extendModel( try { await this.authorize(req); } catch (error) { - error.code = 401; + error.code = error.code || 401; return { error }; } diff --git a/packages/output-geoservices/README.md b/packages/output-geoservices/README.md index 44e056d53..04cbed96a 100644 --- a/packages/output-geoservices/README.md +++ b/packages/output-geoservices/README.md @@ -69,6 +69,9 @@ You can leverage your own custom logger instance, but it must adhere to the Wins } ``` +### `includeOwningSystemUrl (boolean)` +If `true`, adds `owningSystemUrl` to the `rest/info` response. Defaults to false. + ## Routes ```js diff --git a/packages/output-geoservices/src/index.js b/packages/output-geoservices/src/index.js index e906c6be3..fae58ef3b 100644 --- a/packages/output-geoservices/src/index.js +++ b/packages/output-geoservices/src/index.js @@ -42,6 +42,7 @@ class GeoServices { #useHttpForTokenUrl = false; #authInfo; #logger; + #includeOwningSystemUrl; static type = 'output'; static version = require('../package.json').version; @@ -95,6 +96,7 @@ class GeoServices { isTokenBasedSecurity: true, }; + this.#includeOwningSystemUrl = options.includeOwningSystemUrl || false; this.#useHttpForTokenUrl = this.#getHttpSetting(options, model); FeatureServer.setLogger({ logger: this.#logger }); @@ -177,10 +179,13 @@ class GeoServices { authInfo.tokenServicesUrl = this.#buildTokensUrl(req.headers.host, req.baseUrl); } - FeatureServer.route(req, res, { - owningSystemUrl: this.#buildOwningSystemUrl(req.headers.host, req.baseUrl), - authInfo, - }); + const data = { authInfo }; + + if (this.#includeOwningSystemUrl) { + data.owningSystemUrl = this.#buildOwningSystemUrl(req.headers.host, req.baseUrl); + } + + FeatureServer.route(req, res, data); } #buildTokensUrl(host, baseUrl) { diff --git a/packages/output-geoservices/src/index.spec.js b/packages/output-geoservices/src/index.spec.js index 1b3fc5994..84850de8d 100644 --- a/packages/output-geoservices/src/index.spec.js +++ b/packages/output-geoservices/src/index.spec.js @@ -261,7 +261,6 @@ describe('Output Geoservices', () => { reqMock, resMock, { - owningSystemUrl: 'https://some-host.com/api/v1/provider-name', authInfo: { food: 'baz' }, }, ]); @@ -279,7 +278,6 @@ describe('Output Geoservices', () => { reqMock, resMock, { - owningSystemUrl: 'https://some-host.com/api/v1/provider-name', authInfo: { isTokenBasedSecurity: true, tokenServicesUrl: 'https://some-host.com/api/v1/provider-name/rest/generateToken', @@ -302,7 +300,6 @@ describe('Output Geoservices', () => { reqMock, resMock, { - owningSystemUrl: 'http://some-host.com/api/v1/provider-name', authInfo: { isTokenBasedSecurity: true, tokenServicesUrl: 'http://some-host.com/api/v1/provider-name/rest/generateToken', @@ -325,7 +322,6 @@ describe('Output Geoservices', () => { reqMock, resMock, { - owningSystemUrl: 'http://some-host.com/api/v1/provider-name', authInfo: { isTokenBasedSecurity: true, tokenServicesUrl: 'http://some-host.com/api/v1/provider-name/rest/generateToken', @@ -353,7 +349,6 @@ describe('Output Geoservices', () => { reqMock, resMock, { - owningSystemUrl: 'http://some-host.com/api/v1/provider-name', authInfo: { isTokenBasedSecurity: true, tokenServicesUrl: 'http://some-host.com/api/v1/provider-name/rest/generateToken', @@ -383,7 +378,6 @@ describe('Output Geoservices', () => { reqMock, resMock, { - owningSystemUrl: 'http://some-host.com/api/v1/provider-name', authInfo: { isTokenBasedSecurity: true, tokenServicesUrl: 'http://some-host.com/api/v1/provider-name/rest/generateToken', @@ -391,6 +385,31 @@ describe('Output Geoservices', () => { }, ]); }); + + test('should include owningSystemUrl', async () => { + const modelMock = { + namespace: 'provider-name', + pull: jest.fn(async () => 'someData'), + authenticationSpecification: () => { + return {}; + }, + }; + + const output = new OutputGeoServices(modelMock, { includeOwningSystemUrl: true }); + await output.restInfoHandler(reqMock, resMock); + expect(FeatureServer.route.mock.calls.length).toBe(1); + expect(FeatureServer.route.mock.calls[0]).toEqual([ + reqMock, + resMock, + { + owningSystemUrl: 'https://some-host.com/api/v1/provider-name', + authInfo: { + isTokenBasedSecurity: true, + tokenServicesUrl: 'https://some-host.com/api/v1/provider-name/rest/generateToken', + }, + }, + ]); + }); }); describe('generateToken', () => {