Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxy): ⚡ Adds an option to opt out from token fowrarding #231

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions libs/proxy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@

<!-- /TOC -->

## 0.7.0 (2022-05-18)

### Features

- Adds an option to opt out from token fowrarding (per service).

```typescript
services: [
{
id: 'THIRD_PARTY_SERVICE',
url: 'https://some-service.com/some-endpoint',
forwardToken: false,
},
];
```

## 0.6.0 (2021-11-4)

### Features
Expand Down
16 changes: 16 additions & 0 deletions libs/proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ Whether synchronously or asynchronously, the module takes two parameters (both o

> A service can also take an optional `config`, which is the same signature as the parent one, allowing you to override configuration for that particular service !

### Token forwarding

By default, OAuth token are forwarded to the service call being proxied. You can disable this by setting the `forwardToken` property to `false` in the service configuration.

```typescript
services: [
{
id: 'THIRD_PARTY_SERVICE',
url: 'https://some-service.com/some-endpoint',
forwardToken: false,
},
];
```

> &#x26A0; Be careful to only forward tokens to internal services. Don't forward the token to third party services.

### Default module configuration

If you do not provide any, the default proxy configuration for this module can be found in [proxy.constants.ts](./src/proxy.constants.ts), under `defaultProxyOptions`
Expand Down
2 changes: 1 addition & 1 deletion libs/proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@finastra/nestjs-proxy",
"version": "0.6.0",
"version": "0.7.0",
"contributors": [
"David Boclé <david.bocle@finastra.com>"
],
Expand Down
1 change: 1 addition & 0 deletions libs/proxy/src/interfaces/proxy.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Service {
id: string;
url: string;
config?: server.ServerOptions;
forwardToken?: boolean;
}

export interface ProxyModuleOptions {
Expand Down
22 changes: 22 additions & 0 deletions libs/proxy/src/services/proxy.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const services = [
id: 'test',
url: 'https://test.io/subpath',
},
{
id: 'THIRD_PARTY_SERVICE',
url: 'https://some-service.com/some-endpoint',
forwardToken: false,
},
];
const mockProxyModuleOptions = {
services,
Expand Down Expand Up @@ -119,6 +124,23 @@ describe('ProxyService', () => {
expect((spy.mock.calls[0][2] as any).headers).toHaveProperty('authorization');
});

it('should not call proxy with token', () => {
const req = createMock<Request>();
const res = createMock<Response>();
req.query = {
serviceId: services[1].id,
};
req.user = {
authTokens: {
accessToken: 'test',
},
};

const spy = jest.spyOn(proxy, 'web');
service.proxyRequest(req, res);
expect((spy.mock.calls[0][2] as any).headers).not.toHaveProperty('authorization');
});

it('should send a 500 if error comes from proxy', done => {
const req = createMock<Request>();
const res = createMock<Response>();
Expand Down
8 changes: 7 additions & 1 deletion libs/proxy/src/services/proxy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export class ProxyService {
if (services.has(serviceId)) {
const service = services.get(serviceId);
const baseUrl = service.url;
return this.doProxy(req, res, target ? concatPath(baseUrl, prefix, target) : baseUrl, token, service.config);
return this.doProxy(
req,
res,
target ? concatPath(baseUrl, prefix, target) : baseUrl,
service.forwardToken === false ? null : token,
service.config,
);
} else {
const error = `Could not find serviceId ${serviceId}`;
this.logger.warn(error);
Expand Down
1 change: 1 addition & 0 deletions src/configs/proxy-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class ProxyConfigService implements ProxyModuleOptionsFactory {
{
id: 'jsonplaceholder',
url: 'https://jsonplaceholder.typicode.com',
forwardToken: false,
},
];

Expand Down