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(patch): Added httpTimeout Option #251

Merged
merged 4 commits into from
Jun 29, 2021
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
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ interface ConfigParams {
* Additional request body properties to be sent to the `token_endpoint` during authorization code exchange or token refresh.
*/
tokenEndpointParams?: TokenParameters;

/**
* Http timeout for oidc client requests in milliseconds. Default is 5000. Minimum is 500.
*/
httpTimeout?: number;
}

interface SessionStorePayload {
Expand Down
3 changes: 2 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ async function get(config) {
}
: undefined),
};
options.timeout = 5000;
options.timeout = config.httpTimeout;
return options;
};

const applyHttpOptionsCustom = (entity) =>
(entity[custom.http_options] = defaultHttpOptions);

Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ const paramsSchema = Joi.object({
? 'none'
: 'client_secret_basic';
}),
httpTimeout: Joi.number().optional().min(500).default(5000),
});

module.exports.get = function (config = {}) {
Expand Down
51 changes: 50 additions & 1 deletion test/client.tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { assert } = require('chai').use(require('chai-as-promised'));
const { assert, expect } = require('chai').use(require('chai-as-promised'));
const { get: getConfig } = require('../lib/config');
const { get: getClient } = require('../lib/client');
const wellKnown = require('./fixture/well-known.json');
Expand Down Expand Up @@ -98,4 +98,53 @@ describe('client initialization', function () {
assert.equal(client.id_token_signed_response_alg, 'RS256');
});
});

describe('client respects httpTimeout configuration', function () {
const config = getConfig({
secret: '__test_session_secret__',
clientID: '__test_client_id__',
clientSecret: '__test_client_secret__',
issuerBaseURL: 'https://op.example.com',
baseURL: 'https://example.org',
});

function mockRequest(delay = 0) {
nock('https://op.example.com').post('/slow').delay(delay).reply(200);
}

async function invokeRequest(client) {
return await client.requestResource(
'https://op.example.com/slow',
'token',
{
method: 'POST',
headers: {
Authorization: 'Bearer foo',
},
}
);
}

it('should not timeout for default', async function () {
mockRequest(0);
const client = await getClient({ ...config });
const response = await invokeRequest(client);
assert.equal(response.statusCode, 200);
});

it('should not timeout for delay < httpTimeout', async function () {
mockRequest(1000);
const client = await getClient({ ...config, httpTimeout: 1500 });
const response = await invokeRequest(client);
assert.equal(response.statusCode, 200);
});

it('should timeout for delay > httpTimeout', async function () {
mockRequest(1500);
const client = await getClient({ ...config, httpTimeout: 500 });
await expect(invokeRequest(client)).to.be.rejectedWith(
`Timeout awaiting 'request' for 500ms`
);
});
});
});
45 changes: 45 additions & 0 deletions test/config.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ describe('get config', () => {
},
},
});

assert.deepInclude(config, {
secret: ['__test_session_secret_1__', '__test_session_secret_2__'],
session: {
Expand Down Expand Up @@ -379,6 +380,29 @@ describe('get config', () => {
}, '"session.cookie.domain" must be a string');
});

it('should fail when http timeout is invalid', function () {
assert.throws(() => {
getConfig({
...defaultConfig,
httpTimeout: 'abcd',
});
}, '"httpTimeout" must be a number');

assert.throws(() => {
getConfig({
...defaultConfig,
httpTimeout: '-100',
});
}, '"httpTimeout" must be greater than or equal to 500');

assert.throws(() => {
getConfig({
...defaultConfig,
httpTimeout: '499',
});
}, '"httpTimeout" must be greater than or equal to 500');
});

it("shouldn't allow a secret of less than 8 chars", () => {
assert.throws(
() => getConfig({ ...defaultConfig, secret: 'short' }),
Expand Down Expand Up @@ -630,6 +654,18 @@ describe('get config', () => {
);
});

it('should allow valid httpTimeout configuration', () => {
const config = (httpTimeout) => ({
...defaultConfig,
httpTimeout,
});

assert.doesNotThrow(() => config(5000));
assert.doesNotThrow(() => config(10000));
assert.doesNotThrow(() => config('5000'));
assert.doesNotThrow(() => config('10000'));
});

it('should default clientAuthMethod to none for id_token response type', () => {
{
const config = getConfig(defaultConfig);
Expand All @@ -648,6 +684,15 @@ describe('get config', () => {
}
});

it('should default httpTimeout to 5000', () => {
{
const config = getConfig(defaultConfig);
assert.deepInclude(config, {
httpTimeout: 5000,
});
}
});

it('should default clientAuthMethod to client_secret_basic for other response types', () => {
{
const config = getConfig({
Expand Down