Skip to content

Commit

Permalink
Add support for proxy in AuthenticationClient (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck authored Jan 25, 2023
1 parent d4025bb commit 7a629df
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/auth/DatabaseAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 {OAuthAuthenticator} oauth OAuthAuthenticator instance.
*/
constructor(options, oauth) {
Expand All @@ -29,6 +30,7 @@ class DatabaseAuthenticator {
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
};

this.oauth = oauth;
Expand Down
2 changes: 2 additions & 0 deletions src/auth/OAuthAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class OAuthAuthenticator {
* @param {string} [options.clientAssertionSigningKey] Private key used to sign the client assertion JWT.
* @param {string} [options.clientAssertionSigningAlg] Default 'RS256'.
* @param {boolean} [options.__bypassIdTokenValidation] Whether the id_token should be validated or not
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
*/
constructor(options) {
if (!options) {
Expand All @@ -54,6 +55,7 @@ class OAuthAuthenticator {
errorCustomizer: SanitizedError,
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
};

this.oauth = new RestClient(`${options.baseUrl}/oauth/:type`, clientOptions);
Expand Down
2 changes: 2 additions & 0 deletions src/auth/PasswordlessAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PasswordlessAuthenticator {
* @param {string} [options.clientSecret] Default client secret.
* @param {string} [options.clientAssertionSigningKey] Private key used to sign the client assertion JWT.
* @param {string} [options.clientAssertionSigningAlg] Default 'RS256'.
* @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
* @param {OAuthAuthenticator} oauth OAuthAuthenticator instance.
*/
constructor(options, oauth) {
Expand All @@ -48,6 +49,7 @@ class PasswordlessAuthenticator {
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
proxy: options.proxy,
};

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 @@ -42,6 +42,7 @@ class AuthenticationClient {
* @param {string} [options.supportedAlgorithms] Algorithms that your application expects to receive
* @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'
*/
constructor(options) {
if (!options || typeof options !== 'object') {
Expand All @@ -67,6 +68,7 @@ class AuthenticationClient {
baseUrl: util.format(BASE_URL_FORMAT, options.domain),
supportedAlgorithms: options.supportedAlgorithms,
__bypassIdTokenValidation: options.__bypassIdTokenValidation,
proxy: options.proxy,
};

if (options.telemetry !== false) {
Expand Down
2 changes: 2 additions & 0 deletions src/management/ManagementTokenProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ManagementTokenProvider {
* @param {boolean} [options.enableCache=true] Enabled or Disable Cache
* @param {number} [options.cacheTTLInSeconds] By default the `expires_in` value will be used to determine the cached time of the token, this can be overridden.
* @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'
*/
constructor(options) {
if (!options || typeof options !== 'object') {
Expand Down Expand Up @@ -74,6 +75,7 @@ class ManagementTokenProvider {
telemetry: this.options.telemetry,
clientInfo: this.options.clientInfo,
headers: this.options.headers,
proxy: this.options.proxy,
};
this.authenticationClient = new AuthenticationClient(authenticationClientOptions);

Expand Down
134 changes: 134 additions & 0 deletions test/auth/database-auth.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { expect } = require('chai');
const nock = require('nock');
const sinon = require('sinon');
const { Client } = require('rest-facade');
const proxyquire = require('proxyquire');

const DOMAIN = 'tenant.auth0.com';
const API_URL = `https://${DOMAIN}`;
Expand Down Expand Up @@ -181,6 +184,41 @@ describe('DatabaseAuthenticator', () => {
await this.authenticator.signIn(userData);
expect(request.isDone()).to.be.true;
});

it('should make request with proxy', async () => {
nock.cleanAll();

const spy = sinon.spy();

class MockClient extends Client {
constructor(...args) {
spy(...args);
super(...args);
}
}
const MockAuthenticator = proxyquire(`../../src/auth/DatabaseAuthenticator`, {
'rest-facade': {
Client: MockClient,
},
});

const request = nock(API_URL).post(path).reply(200);

const authenticator = new MockAuthenticator(
{
...validOptions,
proxy: 'http://proxy',
},
new OAuth(validOptions)
);

return authenticator.signIn(userData).then(() => {
sinon.assert.calledWithMatch(spy, API_URL, {
proxy: 'http://proxy',
});
expect(request.isDone()).to.be.true;
});
});
});

describe('#signUp', () => {
Expand Down Expand Up @@ -273,6 +311,38 @@ describe('DatabaseAuthenticator', () => {
await this.authenticator.signUp(userData);
expect(request.isDone()).to.be.true;
});

it('should make request with proxy', async () => {
nock.cleanAll();

const spy = sinon.spy();

class MockClient extends Client {
constructor(...args) {
spy(...args);
super(...args);
}
}
const MockAuthenticator = proxyquire(`../../src/auth/DatabaseAuthenticator`, {
'rest-facade': {
Client: MockClient,
},
});

const request = nock(API_URL).post(path).reply(200);

const authenticator = new MockAuthenticator(
{ ...validOptions, proxy: 'http://proxy' },
new OAuth(validOptions)
);

return authenticator.signUp(userData).then(() => {
sinon.assert.calledWithMatch(spy, API_URL, {
proxy: 'http://proxy',
});
expect(request.isDone()).to.be.true;
});
});
});

describe('#changePassword', () => {
Expand Down Expand Up @@ -368,6 +438,38 @@ describe('DatabaseAuthenticator', () => {
await this.authenticator.changePassword(userData);
expect(request.isDone()).to.be.true;
});

it('should make request with proxy', async () => {
nock.cleanAll();

const spy = sinon.spy();

class MockClient extends Client {
constructor(...args) {
spy(...args);
super(...args);
}
}
const MockAuthenticator = proxyquire(`../../src/auth/DatabaseAuthenticator`, {
'rest-facade': {
Client: MockClient,
},
});

const request = nock(API_URL).post(path).reply(200);

const authenticator = new MockAuthenticator(
{ ...validOptions, proxy: 'http://proxy' },
new OAuth(validOptions)
);

return authenticator.changePassword(userData).then(() => {
sinon.assert.calledWithMatch(spy, API_URL, {
proxy: 'http://proxy',
});
expect(request.isDone()).to.be.true;
});
});
});

describe('#requestChangePasswordEmail', () => {
Expand Down Expand Up @@ -450,5 +552,37 @@ describe('DatabaseAuthenticator', () => {
await this.authenticator.requestChangePasswordEmail(userData);
expect(request.isDone()).to.be.true;
});

it('should make request with proxy', async () => {
nock.cleanAll();

const spy = sinon.spy();

class MockClient extends Client {
constructor(...args) {
spy(...args);
super(...args);
}
}
const MockAuthenticator = proxyquire(`../../src/auth/DatabaseAuthenticator`, {
'rest-facade': {
Client: MockClient,
},
});

const request = nock(API_URL).post(path).reply(200);

const authenticator = new MockAuthenticator(
{ ...validOptions, proxy: 'http://proxy' },
new OAuth(validOptions)
);

return authenticator.requestChangePasswordEmail(userData).then(() => {
sinon.assert.calledWithMatch(spy, API_URL, {
proxy: 'http://proxy',
});
expect(request.isDone()).to.be.true;
});
});
});
});
Loading

0 comments on commit 7a629df

Please sign in to comment.