Skip to content

Commit

Permalink
Removed proxy package and added docs on how to achieve with custom fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
joawan committed Mar 30, 2021
1 parent f96b5dd commit 2851606
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 51 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ tokenIntrospection(token).then(console.log).catch(console.warn);
| client_secret | | Client secret used to introspect |
| access_token | | Access token used to introspect, instead of client credentials |
| user_agent | | Defaults to `token-introspection` |
| proxy | | Optional url with port to proxy request through. Requires optional dependency [https-proxy-agent](https://www.npmjs.com/package/https-proxy-agent) |
| fetch | | Defaults to [node-fetch](https://github.com/bitinn/node-fetch), but you can inject [zipkin-instrumentation-fetch](https://www.npmjs.com/package/zipkin-instrumentation-fetch). |

At least one of the required configuration parameters `jwks`, `jwks_uri` or `endpoint` must be specified.

### Flexibility in fetch
As you can provide your own `fetch` implementation, it is possible override the agent `fetch` uses for various purposes.
These purpose can be things like zipkin/tracing, self signed certificates, client TLS authentication, adding a keepAlive, etc.
These purpose can be things like zipkin/tracing, self signed certificates, client TLS authentication, proxy, adding a keepAlive, etc.

```js
const customFetch = (endpoint, data) => {
data.agent = new https.Agent(...);
return fetch(endpoint, data);
const HttpsProxy = require('https-proxy-agent');
const proxy = new HttpsProxy(proxySettings);

const customFetch = (endpoint, options) => {
options.agent = proxy;
process.env.HTTPS_PROXY = proxy;
return fetch(endpoint, options);
};

const tokenIntrospection = require('token-introspection');
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@
"npm": ">=5.6.0"
},
"peerDependencies": {
"https-proxy-agent": "^5.0.0",
"node-fetch": "2.x"
},
"peerDependenciesMeta": {
"https-proxy-agent": {
"optional": true
}
},
"devDependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
Expand Down
14 changes: 1 addition & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function tokenIntrospect(opts = {}) {
client_id: '',
client_secret: '',
user_agent: 'token-introspection',
proxy: '',
fetch: null,
};

Expand All @@ -26,18 +25,7 @@ function tokenIntrospect(opts = {}) {
options.fetch = require('node-fetch');
}

let proxy = null;
if (options.proxy) {
try {
const HttpsProxy = require('https-proxy-agent');
proxy = new HttpsProxy(options.proxy);
process.env.HTTPS_PROXY = options.proxy;
} catch (e) {
throw new errors.ConfigurationError('Proxy given, but missing https-proxy-agent package');
}
}

const remoteIntrospect = remoteIntrospection({ ...options, proxy });
const remoteIntrospect = remoteIntrospection(options);
const localIntrospect = localIntrospection(options);

return async function introspect(token, tokenTypeHint) {
Expand Down
3 changes: 1 addition & 2 deletions src/remote-introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module.exports = (options) => {
Authorization: authorization,
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': options.user_agent,
},
agent: options.proxy,
}
};

return async function remoteIntrospect(token, tokenTypeHint) {
Expand Down
19 changes: 0 additions & 19 deletions test/introspection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('Remote token introspection', () => {
assert.equal(opts.headers.Authorization, 'Basic Y2xpZW50OnNlY3JldA==');
assert.equal(opts.headers['Content-Type'], 'application/x-www-form-urlencoded');
assert.equal(opts.body, 'token=token&token_type_hint=access_token');
assert.isNull(opts.agent);
return {
ok: true,
json: () => Promise.resolve({ active: true }),
Expand All @@ -56,24 +55,6 @@ describe('Remote token introspection', () => {
assert.equal(opts.headers.Authorization, 'Bearer test1234');
assert.equal(opts.headers['Content-Type'], 'application/x-www-form-urlencoded');
assert.equal(opts.body, 'token=token&token_type_hint=access_token');
assert.isNull(opts.agent);
return {
ok: true,
json: () => Promise.resolve({ active: true }),
};
},
});
return expect(introspection('token', 'access_token')).to.eventually.deep.equal({ active: true });
});

it('calls with special proxy agent if given', () => {
const introspection = new TokenIntrospection({
endpoint: 'http://example.com/oauth/introspection',
client_id: 'client',
client_secret: 'secret',
proxy: 'example.proxy.com:3128',
async fetch(url, opts) {
assert.typeOf(opts.agent, 'object');
return {
ok: true,
json: () => Promise.resolve({ active: true }),
Expand Down

0 comments on commit 2851606

Please sign in to comment.