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

Add BaseManager class #743

Merged
merged 1 commit into from
Sep 28, 2022
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
85 changes: 12 additions & 73 deletions src/management/ActionsManager.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,43 @@
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../Auth0RestClient');
const RetryRestClient = require('../RetryRestClient');
const BaseManager = require('./BaseManager');

/**
* {@link https://auth0.com/docs/api/v2#!/Actions/get_actions Actions} provide a way to extend
* Auth0 flows with custom logic.
* See the {@link https://auth0.com/docs/actions Actions documentation} for more information.
*/
class ActionsManager {
class ActionsManager extends BaseManager {
/**
* @param {object} options The client options.
* @param {string} options.baseUrl The URL of the API.
* @param {object} [options.headers] Headers to be included in all requests.
* @param {object} [options.retry] Retry Policy Config
*/
constructor(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide client options');
}

if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}

if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}

/**
* Options object for the Rest Client instance.
*
* @type {object}
*/
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false },
};
super(options);

/**
* Provides an abstraction layer for consuming the
* [Auth0 Actions endpoint]{@link https://auth0.com/docs/api/v2#!/actions}.
*
* @type {external:RestClient}
*/
const auth0RestClient = new Auth0RestClient(
`${options.baseUrl}/actions/actions/:id`,
clientOptions,
options.tokenProvider
);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
this.resource = this._getRestClient('/actions/actions/:id');

const actionsDeployRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/actions/:id/deploy`,
clientOptions,
options.tokenProvider
);
this.actionsDeploy = new RetryRestClient(actionsDeployRestClient, options.retry);
this.actionsDeploy = this._getRestClient('/actions/actions/:id/deploy');

const actionsTestRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/actions/:id/test`,
clientOptions,
options.tokenProvider
);
this.actionsTest = new RetryRestClient(actionsTestRestClient, options.retry);
this.actionsTest = this._getRestClient('/actions/actions/:id/test');

const triggersRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/triggers/:trigger_id`,
clientOptions,
options.tokenProvider
);
this.triggers = new RetryRestClient(triggersRestClient, options.retry);
this.triggers = this._getRestClient('/actions/triggers/:trigger_id');

const triggerBindingsRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/triggers/:trigger_id/bindings`,
clientOptions,
options.tokenProvider
);
this.triggerBindings = new RetryRestClient(triggerBindingsRestClient, options.retry);
this.triggerBindings = this._getRestClient('/actions/triggers/:trigger_id/bindings');

const executionsRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/executions/:execution_id`,
clientOptions,
options.tokenProvider
);
this.executions = new RetryRestClient(executionsRestClient, options.retry);
this.executions = this._getRestClient('/actions/executions/:execution_id');

const actionVersionRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/actions/:id/versions/:version_id`,
clientOptions,
options.tokenProvider
);
this.actionVersions = new RetryRestClient(actionVersionRestClient, options.retry);
this.actionVersions = this._getRestClient('/actions/actions/:id/versions/:version_id');

const deployActionVersionRestClient = new Auth0RestClient(
`${options.baseUrl}/actions/actions/:id/versions/:version_id/deploy`,
clientOptions,
options.tokenProvider
this.actionVersionDeploy = this._getRestClient(
'/actions/actions/:id/versions/:version_id/deploy'
);
this.actionVersionDeploy = new RetryRestClient(deployActionVersionRestClient, options.retry);
}

/**
Expand Down
45 changes: 8 additions & 37 deletions src/management/AttackProtectionManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../Auth0RestClient');
const RetryRestClient = require('../RetryRestClient');
const BaseManager = require('./BaseManager');

/**
* Simple facade for consuming a REST API endpoint.
Expand All @@ -9,7 +7,7 @@ const RetryRestClient = require('../RetryRestClient');
* @see https://github.com/ngonzalvez/rest-facade
*/

class AttackProtectionManager {
class AttackProtectionManager extends BaseManager {
/**
* @class
* Abstracts interaction with the attack-protection endpoints.
Expand All @@ -21,44 +19,17 @@ class AttackProtectionManager {
* @param {object} [options.tokenProvider] Management API Token Provider
*/
constructor(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide manager options');
}
super(options);

if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}
this.bruteForceProtection = this._getRestClient('/attack-protection/brute-force-protection');

if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}

const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false },
};

const bruteForceProtection = new Auth0RestClient(
`${options.baseUrl}/attack-protection/brute-force-protection`,
clientOptions,
options.tokenProvider
);
this.bruteForceProtection = new RetryRestClient(bruteForceProtection, options.retry);

const suspiciousIpThrottling = new Auth0RestClient(
`${options.baseUrl}/attack-protection/suspicious-ip-throttling`,
clientOptions,
options.tokenProvider
this.suspiciousIpThrottling = this._getRestClient(
'/attack-protection/suspicious-ip-throttling'
);
this.suspiciousIpThrottling = new RetryRestClient(suspiciousIpThrottling, options.retry);

const breachedPasswordDetection = new Auth0RestClient(
`${options.baseUrl}/attack-protection/breached-password-detection`,
clientOptions,
options.tokenProvider
this.breachedPasswordDetection = this._getRestClient(
'/attack-protection/breached-password-detection'
);
this.breachedPasswordDetection = new RetryRestClient(breachedPasswordDetection, options.retry);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/management/BaseManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../Auth0RestClient');
const RetryRestClient = require('../RetryRestClient');

class BaseManager {
constructor(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide manager options');
}

if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}

if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}

this._options = options;
}

_getRestClient(path) {
const options = this._options;
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false },
};

const usersAuth0RestClient = new Auth0RestClient(
`${options.baseUrl}${path}`,
clientOptions,
options.tokenProvider
);
return new RetryRestClient(usersAuth0RestClient, options.retry);
}
}

module.exports = BaseManager;
36 changes: 4 additions & 32 deletions src/management/BlacklistedTokensManager.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,26 @@
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../Auth0RestClient');
const RetryRestClient = require('../RetryRestClient');
const BaseManager = require('./BaseManager');

/**
* The BlacklistedTokensManager class provides methods to retrieve the list of
* blacklisted tokens and blacklist new ones..
*/
class BlacklistedTokensManager {
class BlacklistedTokensManager extends BaseManager {
/**
* @param {object} options The client options.
* @param {string} options.baseUrl The URL of the API.
* @param {object} [options.headers] Headers to be included in all requests.
* @param {object} [options.retry] Retry Policy Config
*/
constructor(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide client options');
}

if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}

if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}

/**
* Options object for the Rest Client instance.
*
* @type {object}
*/
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false },
};
super(options);

/**
* Provides an abstraction layer for consuming the
* [Auth0 Blacklisted Tokens endpoint]{@link https://auth0.com/docs/api/v2#!/Clients}.
*
* @type {external:RestClient}
*/
const auth0RestClient = new Auth0RestClient(
`${options.baseUrl}/blacklists/tokens`,
clientOptions,
options.tokenProvider
);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
this.resource = this._getRestClient('/blacklists/tokens');
}

/**
Expand Down
45 changes: 6 additions & 39 deletions src/management/BrandingManager.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,41 @@
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../Auth0RestClient');
const RetryRestClient = require('../RetryRestClient');
const BaseManager = require('./BaseManager');

/**
* Manages settings related to branding.
*/
class BrandingManager {
class BrandingManager extends BaseManager {
/**
* @param {object} options The client options.
* @param {string} options.baseUrl The URL of the API.
* @param {object} [options.headers] Headers to be included in all requests.
* @param {object} [options.retry] Retry Policy Config
*/
constructor(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide manager options');
}

if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}

if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}

const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false },
};
super(options);

/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/management/v2#!/Branding Branding endpoint}.
*
* @type {external:RestClient}
*/
const auth0RestClient = new Auth0RestClient(
`${options.baseUrl}/branding`,
clientOptions,
options.tokenProvider
);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
this.resource = this._getRestClient('/branding');

/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/management/v2#!/Branding/get_universal_login Branding new universal login template endpoint}.
*
* @type {external:RestClient}
*/
const brandingTemplateAuth0RestClient = new Auth0RestClient(
`${options.baseUrl}/branding/templates/universal-login`,
clientOptions,
options.tokenProvider
);
this.brandingTemplates = new RetryRestClient(brandingTemplateAuth0RestClient, options.retry);
this.brandingTemplates = this._getRestClient('/branding/templates/universal-login');

/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/management/v2#!/Branding/get_branding_theme Branding theme endpoint}.
*
* @type {external:RestClient}
*/
const brandingThemesAuth0RestClient = new Auth0RestClient(
`${options.baseUrl}/branding/themes/:id`,
clientOptions,
options.tokenProvider
);
this.brandingThemes = new RetryRestClient(brandingThemesAuth0RestClient, options.retry);
this.brandingThemes = this._getRestClient('/branding/themes/:id');
}

/**
Expand Down
Loading