Skip to content

Commit

Permalink
feat: add configuration for using persistent connections (#919)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Mcgrath <[email protected]>
  • Loading branch information
alaczi and adamjmcgrath authored Aug 29, 2023
1 parent 3371afc commit 01b9336
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/auth/DatabaseAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class DatabaseAuthenticator {
* @param {object} options Authenticator options.
* @param {string} options.baseUrl The auth0 account URL.
* @param {string} [options.clientId] Default client ID.
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
* @param {boolean} [options.keepAlive] Keep the connection alive
* @param {OAuthAuthenticator} oauth OAuthAuthenticator instance.
*/
constructor(options, oauth) {
Expand All @@ -31,6 +32,7 @@ class DatabaseAuthenticator {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
keepAlive: options.keepAlive,
};

this.oauth = oauth;
Expand Down
1 change: 1 addition & 0 deletions src/auth/OAuthAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class OAuthAuthenticator {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
keepAlive: options.keepAlive,
};

this.oauth = new RestClient(`${options.baseUrl}/oauth/:type`, clientOptions);
Expand Down
1 change: 1 addition & 0 deletions src/auth/PasswordlessAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PasswordlessAuthenticator {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
keepAlive: options.keepAlive,
};

this.oauth = oauth;
Expand Down
2 changes: 2 additions & 0 deletions src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class AuthenticationClient {
* @param {boolean} [options.__bypassIdTokenValidation] Whether the id_token should be validated or not
* @param {object} [options.headers] Additional headers that will be added to the outgoing requests.
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
* @param {boolean} [options.keepAlive] Keep the http connections alive.
*/
constructor(options) {
if (!options || typeof options !== 'object') {
Expand All @@ -69,6 +70,7 @@ class AuthenticationClient {
supportedAlgorithms: options.supportedAlgorithms,
__bypassIdTokenValidation: options.__bypassIdTokenValidation,
proxy: options.proxy,
keepAlive: options.keepAlive,
};

if (options.telemetry !== false) {
Expand Down
1 change: 1 addition & 0 deletions src/management/BaseManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BaseManager {
proxy: options.proxy,
query: { repeatParams: false },
includeResponseHeaders: options.includeResponseHeaders,
keepAlive: options.keepAlive,
};

const usersAuth0RestClient = new Auth0RestClient(
Expand Down
4 changes: 3 additions & 1 deletion src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ class ManagementClient {
* @param {number} [options.retry.maxRetries=10] Retry failed requests X times.
* @param {object} [options.headers] Additional headers that will be added to the outgoing requests.
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
* @param {boolean} [options.includeResponseHeaders] Include the response headers in the payload in the format `{ data, headers }`.
* @param {boolean} [options.includeResponseHeaders] Include the response headers in the payload in the format `{ data, headers }`.
* @param {boolean} [options.keepAlive] Keep the http connections alive.
*/
constructor(options) {
if (!options || typeof options !== 'object') {
Expand Down Expand Up @@ -172,6 +173,7 @@ class ManagementClient {
managerOptions.retry = options.retry;
managerOptions.includeResponseHeaders = options.includeResponseHeaders;
managerOptions.proxy = options.proxy;
managerOptions.keepAlive = options.keepAlive;

/**
* Simple abstraction for performing CRUD operations on the
Expand Down
11 changes: 11 additions & 0 deletions test/auth/authentication-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ describe('AuthenticationClient', () => {
expect(client.users.headers).to.contain(customHeaders);
expect(client.tokens.headers).to.contain(customHeaders);
});
it('should configure instances with keepAlive if the option is passwd', () => {
const client = new AuthenticationClient({
token: 'token',
domain: 'auth0.com',
keepAlive: true,
});

expect(client.oauth.oauth.options.keepAlive).to.equal(true);
expect(client.database.dbConnections.options.keepAlive).to.equal(true);
expect(client.passwordless.passwordless.options.keepAlive).to.equal(true);
});
});

describe(`verifySMSCode`, () => {
Expand Down
28 changes: 28 additions & 0 deletions test/auth0-rest-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,32 @@ describe('Auth0RestClient', () => {
proxy: 'http://proxy',
});
});

it('should use keepAlive if it is passed in the options', async function () {
const spy = sinon.spy();
class MockClient extends Client {
constructor(...args) {
spy(...args);
super(...args);
}
}
const RestClient = proxyquire('../src/Auth0RestClient', {
'rest-facade': {
Client: MockClient,
},
});
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });

const options = {
headers: {},
keepAlive: true,
};

const client = new RestClient(`${API_URL}/some-resource`, options, this.providerMock);
const data = await client.getAll();
expect(data).to.deep.equal({ data: 'value' });
sinon.assert.calledWithMatch(spy, 'https://tenant.auth0.com/some-resource', {
keepAlive: true,
});
});
});
94 changes: 94 additions & 0 deletions test/management/management-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,100 @@ describe('ManagementClient', () => {
'Auth0-Client'
);
});
it('should configure instances with keepAlive if the option is passwd', () => {
const client = new ManagementClient({
token: 'token',
domain: 'auth0.com',
keepAlive: true,
});

expect(client.clients.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.clientGrants.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.grants.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.users.users.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.users.multifactor.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.users.identities.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.users.userLogs.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.users.enrollments.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.users.usersByEmail.restClient.restClient.options.keepAlive).to.equal(true);
expect(
client.users.recoveryCodeRegenerations.restClient.restClient.options.keepAlive
).to.equal(true);
expect(
client.users.invalidateRememberBrowsers.restClient.restClient.options.keepAlive
).to.equal(true);
expect(client.users.roles.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.users.permissions.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.guardian.enrollments.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.guardian.tickets.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.guardian.factors.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.guardian.factorsTemplates.restClient.restClient.options.keepAlive).to.equal(
true
);
expect(client.guardian.factorsProviders.restClient.restClient.options.keepAlive).to.equal(
true
);
expect(
client.guardian.factorsPhoneSelectedProvider.restClient.restClient.options.keepAlive
).to.equal(true);
expect(
client.guardian.factorsPhoneMessageTypes.restClient.restClient.options.keepAlive
).to.equal(true);

expect(client.customDomains.resource.restClient.restClient.options.keepAlive).to.equal(
true
);
expect(
client.customDomains.vefifyResource.restClient.restClient.options.keepAlive
).to.equal(true);

expect(client.connections.resource.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.deviceCredentials.resource.restClient.restClient.options.keepAlive).to.equal(
true
);

expect(client.rules.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.blacklistedTokens.resource.restClient.restClient.options.keepAlive).to.equal(
true
);

expect(client.emailProvider.resource.restClient.restClient.options.keepAlive).to.equal(
true
);

expect(client.emailTemplates.resource.restClient.restClient.options.keepAlive).to.equal(
true
);

expect(client.stats.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.tenant.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.jobs.jobs.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.jobs.usersExports.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.tickets.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.logs.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.resourceServers.resource.restClient.restClient.options.keepAlive).to.equal(
true
);

expect(client.emailTemplates.resource.restClient.restClient.options.keepAlive).to.equal(
true
);
expect(client.rulesConfigs.resource.restClient.restClient.options.keepAlive).to.equal(true);

expect(client.roles.resource.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.roles.permissions.restClient.restClient.options.keepAlive).to.equal(true);
expect(client.roles.users.restClient.restClient.options.keepAlive).to.equal(true);
});
});

before(function () {
Expand Down

0 comments on commit 01b9336

Please sign in to comment.