Skip to content

Commit

Permalink
fix: two auth bugs (#980)
Browse files Browse the repository at this point in the history
* fix: auth bugs
  • Loading branch information
rgwozdz authored Apr 19, 2024
1 parent 5cb4c20 commit 90228e9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-numbers-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@koopjs/koop-core": patch
---

- auth error code can be set from provider but defaults to 401
5 changes: 5 additions & 0 deletions .changeset/silver-eagles-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@koopjs/output-geoservices": patch
---

- owningSystemUrl inclusion is a constructor option
2 changes: 1 addition & 1 deletion packages/core/src/data-provider/extend-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
3 changes: 3 additions & 0 deletions packages/output-geoservices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions packages/output-geoservices/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GeoServices {
#useHttpForTokenUrl = false;
#authInfo;
#logger;
#includeOwningSystemUrl;

static type = 'output';
static version = require('../package.json').version;
Expand Down Expand Up @@ -95,6 +96,7 @@ class GeoServices {
isTokenBasedSecurity: true,
};

this.#includeOwningSystemUrl = options.includeOwningSystemUrl || false;
this.#useHttpForTokenUrl = this.#getHttpSetting(options, model);

FeatureServer.setLogger({ logger: this.#logger });
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 25 additions & 6 deletions packages/output-geoservices/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ describe('Output Geoservices', () => {
reqMock,
resMock,
{
owningSystemUrl: 'https://some-host.com/api/v1/provider-name',
authInfo: { food: 'baz' },
},
]);
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -383,14 +378,38 @@ 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',
},
},
]);
});

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', () => {
Expand Down

0 comments on commit 90228e9

Please sign in to comment.