Skip to content

Commit

Permalink
Merge branch 'master' into feature/authorizationCodeGrant
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge authored Aug 8, 2018
2 parents cb44906 + d608e7a commit 980bc79
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/auth/OAuthAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var RestClient = require('rest-facade').Client;
/**
* @class
* Abstracts the sign-in, sign-up and change-password processes for Database &
* Active Directory auhtentication services.
* Active Directory authentication services.
* @constructor
* @memberOf module:auth
*
Expand Down Expand Up @@ -56,7 +56,7 @@ var OAuthAuthenticator = function(options) {
* var data = {
* client_id: '{CLIENT_ID}', // Optional field.
* username: '{USERNAME}',
* password: '{PASSWORD}
* password: '{PASSWORD}',
* connection: '{CONNECTION_NAME}',
* scope: 'openid' // Optional field.
* };
Expand Down Expand Up @@ -121,7 +121,7 @@ OAuthAuthenticator.prototype.signIn = function(userData, cb) {
* var data = {
* client_id: '{CLIENT_ID}', // Optional field.
* username: '{USERNAME}',
* password: '{PASSWORD}'
* password: '{PASSWORD}',
* realm: '{CONNECTION_NAME}', // Optional field.
* scope: 'openid' // Optional field.
* };
Expand Down
96 changes: 96 additions & 0 deletions src/management/GuardianManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var ArgumentError = require('rest-facade').ArgumentError;
var Auth0RestClient = require('../Auth0RestClient');
var RetryRestClient = require('../RetryRestClient');

/**
* Simple facade for consuming a REST API endpoint.
* @external RestClient
* @see https://github.com/ngonzalvez/rest-facade
*/

/**
* @class
* Abstracts interaction with the Guardian endpoint.
* @constructor
* @memberOf module:management
*
* @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
*/
var GuardianManager = function(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');
}

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

/**
* Provides an abstraction layer for retrieving Guardian enrollments.
*
* @type {external:RestClient}
*/
var guardianEnrollmentsAuth0RestClient = new Auth0RestClient(
options.baseUrl + '/guardian/enrollments/:id',
clientOptions,
options.tokenProvider
);
this.enrollments = new RetryRestClient(guardianEnrollmentsAuth0RestClient, options.retry);
};

/**
* Get a single Guardian enrollment.
*
* @method getGuardianEnrollment
* @memberOf module:management.GuardianManager.prototype
*
* @example
* management.users.getGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollment) {
* console.log(enrollment);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
GuardianManager.prototype.getGuardianEnrollment = function(params, cb) {
return this.enrollments.get(params, cb);
};

/**
* Delete a Guardian enrollment.
*
* @method deleteGuardianEnrollment
* @memberOf module:management.GuardianManager.prototype
*
* @example
* management.users.deleteGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollments) {
* console.log(enrollments);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
GuardianManager.prototype.deleteGuardianEnrollment = function(params, cb) {
return this.enrollments.delete(params, cb);
};

module.exports = GuardianManager;
44 changes: 44 additions & 0 deletions src/management/UsersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ var UsersManager = function(options) {
options.tokenProvider
);
this.usersByEmail = new RetryRestClient(usersByEmailClient, options.retry);

/**
* Provides an abstraction layer for regenerating Guardian recovery codes.
*
* @type {external:RestClient}
*/
var recoveryCodeRegenerationAuth0RestClients = new Auth0RestClient(
options.baseUrl + '/users/:id/recovery-code-regeneration',
clientOptions,
options.tokenProvider
);
this.recoveryCodeRegenerations = new RetryRestClient(
recoveryCodeRegenerationAuth0RestClients,
options.retry
);
};

/**
Expand Down Expand Up @@ -577,4 +592,33 @@ UsersManager.prototype.getGuardianEnrollments = function() {
return this.enrollments.get.apply(this.enrollments, arguments);
};

/**
* Generate new Guardian recovery code.
*
* @method regenerateRecoveryCode
* @memberOf module:management.UsersManager.prototype
*
* @example
* management.users.regenerateRecoveryCode("USER_ID", function (err, result) {
* console.log(result.recovery_code);
* });
*
* @param {Object} params Get logs data.
* @param {String} params.id User id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
UsersManager.prototype.regenerateRecoveryCode = function(params, cb) {
if (!params || !params.id) {
throw new ArgumentError('The userId cannot be null or undefined');
}

if (cb && cb instanceof Function) {
return this.recoveryCodeRegenerations.create(params, {}, cb);
}

return this.recoveryCodeRegenerations.create(params, {});
};

module.exports = UsersManager;
82 changes: 82 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var ResourceServersManager = require('./ResourceServersManager');
var ManagementTokenProvider = require('./ManagementTokenProvider');
var RulesConfigsManager = require('./RulesConfigsManager');
var EmailTemplatesManager = require('./EmailTemplatesManager');
var GuardianManager = require('./GuardianManager');

var BASE_URL_FORMAT = 'https://%s/api/v2';
var MANAGEMENT_API_AUD_FORMAT = 'https://%s/api/v2/';
Expand Down Expand Up @@ -155,6 +156,14 @@ var ManagementClient = function(options) {
*/
this.users = new UsersManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on the
* guardian endpoint.
*
* @type {GuardianManager}
*/
this.guardian = new GuardianManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on the
* connections endpoint.
Expand Down Expand Up @@ -1236,6 +1245,79 @@ utils.wrapPropertyMethod(
'users.getGuardianEnrollments'
);

/**
* Generate new Guardian recovery code.
*
* @method regenerateRecoveryCode
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.regenerateRecoveryCode({ id: USER_ID }, function (err, newRecoveryCode) {
* console.log(newRecoveryCode);
* });
*
* @param {Object} data The user data object.
* @param {String} data.id The user id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(
ManagementClient,
'regenerateRecoveryCode',
'users.regenerateRecoveryCode'
);

/**
* Get a single Guardian enrollment.
*
* @method getGuardianEnrollment
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.getGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollment) {
* console.log(enrollment);
* });
*
* @param {Object} data The Guardian enrollment data object.
* @param {String} data.id The Guardian enrollment id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(
ManagementClient,
'getGuardianEnrollment',
'guardian.getGuardianEnrollment'
);

/**
* Delete a user's Guardian enrollment.
*
* @method deleteGuardianEnrollment
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.deleteGuardianEnrollment({ id: ENROLLMENT_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Email provider deleted.
* });
*
* @param {Object} data The Guardian enrollment data object.
* @param {String} data.id The Guardian enrollment id.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(
ManagementClient,
'deleteGuardianEnrollment',
'guardian.deleteGuardianEnrollment'
);

/**
* Get all blacklisted tokens.
*
Expand Down
Loading

0 comments on commit 980bc79

Please sign in to comment.