Skip to content

Commit

Permalink
Adding Support for Guardian factor settings endpoints (#569)
Browse files Browse the repository at this point in the history
* adding endpoint methods

* adding tests

* fixing typo

* removing .only

* adding management wrapper
  • Loading branch information
JayHelton authored Jan 11, 2021
1 parent 0144fb3 commit 09dec3e
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/management/GuardianManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ var GuardianManager = function(options) {
);
this.factors = new RetryRestClient(guardianFactorsAuth0RestClient, options.retry);

/**
* Provides an abstraction layer for configuring Factor settings
*
* @type {external:RestClient}
*/
var guardianFactorSettingsAuth0RestClient = new Auth0RestClient(
options.baseUrl + '/guardian/factors/:name/settings',
clientOptions,
options.tokenProvider
);
this.factorSettings = new RetryRestClient(guardianFactorSettingsAuth0RestClient, options.retry);

/**
* Provides an abstraction layer for retrieving Guardian factor templates.
*
Expand Down Expand Up @@ -220,6 +232,45 @@ utils.wrapPropertyMethod(GuardianManager, 'createEnrollmentTicket', 'tickets.cre
*/
utils.wrapPropertyMethod(GuardianManager, 'getFactors', 'factors.getAll');

/**
* Get Guardian factor configuration
*
* @method getFactorSettings
* @memberOf module:management.GuardianManager.prototype
*
* @example
* management.guardian.getFactorSettings({ name: 'webauthn-roaming' }, function (err, settings) {
* console.log(settings);
* });
*
* @param {Object} params Factor parameters.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(GuardianManager, 'getFactorSettings', 'factorSettings.get');

/**
* Update Guardian factor configuration
*
* @method updateFactorSettings
* @memberOf module:management.GuardianManager.prototype
*
* @example
* management.guardian.updateFactorSettings(
* { name: 'webauthn-roaming' },
* { userVerification: 'discouraged', overrideRelyingParty: false },
* function (err, settings) {
* console.log(settings);
* });
*
* @param {Object} params Factor parameters.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(GuardianManager, 'updateFactorSettings', 'factorSettings.update');

/**
* Get Guardian factor provider configuration
*
Expand Down
48 changes: 48 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,28 @@ utils.wrapPropertyMethod(
*/
utils.wrapPropertyMethod(ManagementClient, 'getGuardianFactors', 'guardian.getFactors');

/**
* Get the settings of a Guardian factor.
*
* @method getGuardianFactorSettings
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.getGuardianFactorSettings({ name: 'duo' }, function (err, settings) {
* console.log(settings);
* });
*
* @param {Object} params Factor parameters.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(
ManagementClient,
'getGuardianFactorSettings',
'guardian.getFactorSettings'
);

/**
* Get Guardian factor provider configuration
*
Expand Down Expand Up @@ -2883,6 +2905,32 @@ utils.wrapPropertyMethod(
'guardian.updateFactorProvider'
);

/**
* Update a Guardian's factor settings
*
* @method updateGuardianFactorSettings
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.updateGuardianFactorSettings(
* { name: 'webauthn-roaming' },
* { userVerification: 'discouraged', overrideRelyingParty: false },
* function (err, settings) {
* console.log(settings);
* })
*
* @param {Object} params Factor parameters.
* @param {Object} data Updated Factor settings data.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(
ManagementClient,
'updateGuardianFactorSettings',
'guardian.updateFactorSettings'
);

/**
* Get Guardian enrollment and verification factor templates
*
Expand Down
141 changes: 141 additions & 0 deletions test/management/guardian.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ describe('GuardianManager', function() {
'getGuardianEnrollment',
'deleteGuardianEnrollment',
'getFactors',
'getFactorSettings',
'getFactorProvider',
'updateFactorProvider',
'getFactorTemplates',
'updateFactorTemplates',
'updateFactor',
'updateFactorSettings',
'getPhoneFactorSelectedProvider',
'updatePhoneFactorSelectedProvider',
'getPhoneFactorMessageTypes',
Expand Down Expand Up @@ -346,6 +348,69 @@ describe('GuardianManager', function() {
});
});

describe('#getFactorSettings', function() {
beforeEach(function() {
this.params = { name: 'webauthn-roaming' };
this.data = {
userVerification: 'discouraged',
overrideRelyingParty: false
};
this.request = nock(API_URL)
.get('/guardian/factors/' + this.params.name + '/settings')
.reply(200, this.data);
});

it('should accept a callback', function(done) {
this.guardian.getFactorSettings(this.params, done.bind(null, null));
});

it('should return a promise if no callback is given', function(done) {
this.guardian
.getFactorSettings(this.params)
.then(done.bind(null, null))
.catch(done.bind(null, null));
});

it('should perform a GET request to /api/v2/guardian/factors/webauthn-roaming/settings', function(done) {
var request = this.request;

this.guardian.getFactorSettings(this.params).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should pass any errors to the promise catch handler', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.get('/guardian/factors/' + this.params.name + '/settings')
.reply(500);

this.guardian.getFactorSettings(this.params).catch(function(err) {
expect(err).to.exist;

done();
});
});

it('should include the token in the Authorization header', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.get('/guardian/factors/' + this.params.name + '/settings')
.matchHeader('Authorization', 'Bearer ' + this.token)
.reply(200);

this.guardian.getFactorSettings(this.params).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});
});

describe('#getFactorProvider', function() {
beforeEach(function() {
this.params = { name: 'sms', provider: 'twilio' };
Expand Down Expand Up @@ -711,6 +776,82 @@ describe('GuardianManager', function() {
});
});

describe('#updateFactorSettings', function() {
beforeEach(function() {
this.params = { name: 'webauthn-roaming' };
this.data = {
userVerification: 'discouraged',
overrideRelyingParty: false
};
});

it('should accept a callback', function(done) {
this.guardian.updateFactorSettings({ id: 5 }, {}, done.bind(null, null));
});

it('should return a promise if no callback is given', function(done) {
this.guardian
.updateFactorSettings(this.params, {})
.then(done.bind(null, null))
.catch(done.bind(null, null));
});

it('should perform a PUT request to /api/v2/guardian/factors/webauthn-roaming/settings', function(done) {
var request = nock(API_URL)
.put('/guardian/factors/' + this.params.name + '/settings')
.reply(200, this.data);

this.guardian.updateFactorSettings(this.params, this.data).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should include the new data in the body of the request', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.put('/guardian/factors/' + this.params.name + '/settings', this.data)
.reply(200);

this.guardian.updateFactorSettings(this.params, this.data).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should pass any errors to the promise catch handler', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.put('/guardian/factors/' + this.params.name + '/settings')
.reply(500);

this.guardian.updateFactorSettings(this.params, this.data).catch(function(err) {
expect(err).to.exist;

done();
});
});

it('should include the token in the Authorization header', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.put('/guardian/factors/' + this.params.name + '/settings')
.matchHeader('Authorization', 'Bearer ' + this.token)
.reply(200);

this.guardian.updateFactorSettings(this.params, this.data).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});
});

describe('#getPolicies', function() {
beforeEach(function() {
this.data = ['all-applications'];
Expand Down
2 changes: 2 additions & 0 deletions test/management/management-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,10 @@ describe('ManagementClient', function() {
'deleteRulesConfig',
'createGuardianEnrollmentTicket',
'getGuardianFactors',
'getGuardianFactorSettings',
'getGuardianFactorProvider',
'updateGuardianFactorProvider',
'updateGuardianFactorSettings',
'getGuardianFactorTemplates',
'updateGuardianFactorTemplates',
'updateGuardianFactor',
Expand Down

0 comments on commit 09dec3e

Please sign in to comment.