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

Fixing proxy on Axios #176

Merged
merged 1 commit into from
Sep 22, 2020
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
120 changes: 116 additions & 4 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
"eslint-plugin-babel": "^5.3.0",
"express": "^4.17.1",
"express-jwt": "^6.0.0",
"http-proxy-agent": "^4.0.1",
"https-proxy-agent": "^5.0.0",
"koa": "^2.12.1",
"koa-jwt": "^3.6.0",
"mocha": "^6.2.3",
"nock": "^10.0.6",
"nyc": "^15.1.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"proxy": "^1.0.2",
"rimraf": "^2.7.1",
"supertest": "^3.4.2",
"ts-node": "^8.10.2",
Expand Down
33 changes: 18 additions & 15 deletions src/wrappers/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import http from 'http';
import https from 'https';
import urlUtil from 'url';
import url from 'url';
import httpProxyAgent from 'https-proxy-agent';
import httpsProxyAgent from 'https-proxy-agent';
import { request } from 'axios';

export default function(options, cb) {
Expand All @@ -10,24 +12,25 @@ export default function(options, cb) {
timeout: options.timeout
};

if (options.proxy) {
const proxy = urlUtil.parse(options.proxy);
const [ username, password ] = proxy.auth.split(':');

requestOptions.proxy = {
host: proxy.hostname,
port: proxy.port,
auth: { username, password }
};
}

if (options.agentOptions || options.strictSSL != undefined) {
if (options.proxy || options.agentOptions || options.strictSSL != undefined) {
const agentOptions = {
...(options.strictSSL != undefined) && { rejectUnauthorized: options.strictSSL },
...(options.headers && { headers: options.headers }),
...options.agentOptions
};
requestOptions.httpAgent = new http.Agent(agentOptions);
requestOptions.httpsAgent = new https.Agent(agentOptions);

if (options.proxy) {
// Axios proxy workaround: https://github.com/axios/axios/issues/2072
const proxy = url.parse(options.proxy);

requestOptions.proxy = false; //proxyParsed
const proxyAgentOptions = { ...agentOptions, ...proxy };
requestOptions.httpAgent = new httpProxyAgent(proxyAgentOptions);
requestOptions.httpsAgent = new httpsProxyAgent(proxyAgentOptions);
} else {
requestOptions.httpAgent = new http.Agent(agentOptions);
requestOptions.httpsAgent = new https.Agent(agentOptions);
}
}

request(requestOptions)
Expand Down
91 changes: 74 additions & 17 deletions tests/jwksClient.tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import http from 'http';
import Proxy from 'proxy';
import nock from 'nock';
import { expect } from 'chai';

Expand Down Expand Up @@ -183,24 +185,79 @@ describe('JwksClient', () => {
});
});

it('should use a proxy if specified', done => {
const proxy = 'my-proxy-server:2815';
const expectedError = { message: 'expectedError' };
nock(`http://${proxy}`)
.get(() => true)
.replyWithError(expectedError);

const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`,
requestHeaders: {
'User-Agent': 'My-bot'
},
proxy: `http://username:password@${proxy}`
describe('when using a proxy', () => {
let server;
let proxy;
let proxyPort;
let serverPort;
let serverAddress;
let proxyAddress;

before((done) => {
server = http.createServer();
server.listen(() => {
serverPort = server.address().port;
serverAddress = `http://localhost:${serverPort}`;
done();
});
});

client.getKeys((err) => {
expect(err).to.equal(expectedError);
done();

before((done) => {
proxy = Proxy();
proxy.listen(() => {
proxyPort = proxy.address().port;
proxyAddress = `http://localhost:${proxyPort}`;
done();
});
});

after((done) => {
server.once('close', () => done());
server.close();
});

after((done) => {
proxy.once('close', () => done());
proxy.close();
});

it('should properly handle a proxied request', (done) => {
const expectedKeys = {
keys: [
{
alg: 'RS256',
kty: 'RSA',
use: 'sig',
x5c: [ 'pk1' ],
kid: 'ABC'
},
{
alg: 'RS256',
kty: 'RSA',
use: 'sig',
x5c: [],
kid: '123'
}
]
};
server.once('request', (req, res) => res.end(JSON.stringify(expectedKeys)));

const client = new JwksClient({ jwksUri: serverAddress, proxy: proxyAddress });
client.getKeys((err, keys) => {
expect(keys).to.eql(expectedKeys.keys);
done();
});
});

it('should fail when proxy address is not reachable', (done) => {
proxyAddress = 'http://wrongAddress';
server.once('request', (req, res) => res.end(JSON.stringify(expectedKeys)));

const client = new JwksClient({ jwksUri: serverAddress, proxy: proxyAddress });
client.getKeys((err) => {
expect(err.code).to.eql('ENOTFOUND');
done();
});
});
});
});
Expand Down