From 477705df37879d5d4f940df1241e7d32e96705d5 Mon Sep 17 00:00:00 2001 From: Steve Adams Date: Mon, 5 Nov 2018 23:22:40 +1100 Subject: [PATCH 01/11] add extra guardian settings --- src/management/GuardianManager.js | 197 ++++++++++++++++++++++++++++-- src/management/index.js | 150 +++++++++++++++++++++++ 2 files changed, 339 insertions(+), 8 deletions(-) diff --git a/src/management/GuardianManager.js b/src/management/GuardianManager.js index eede511d2..cf3452cd6 100644 --- a/src/management/GuardianManager.js +++ b/src/management/GuardianManager.js @@ -1,4 +1,5 @@ var ArgumentError = require('rest-facade').ArgumentError; +var utils = require('../utils'); var Auth0RestClient = require('../Auth0RestClient'); var RetryRestClient = require('../RetryRestClient'); @@ -49,6 +50,60 @@ var GuardianManager = function(options) { options.tokenProvider ); this.enrollments = new RetryRestClient(guardianEnrollmentsAuth0RestClient, options.retry); + + /** + * Provides an abstraction layer for retrieving Guardian tickets. + * + * @type {external:RestClient} + */ + var guardianTicketsAuth0RestClient = new Auth0RestClient( + options.baseUrl + '/guardian/enrollments/ticket', + clientOptions, + options.tokenProvider + ); + this.tickets = new RetryRestClient(guardianTicketsAuth0RestClient, options.retry); + + /** + * Provides an abstraction layer for retrieving Guardian factors. + * + * @type {external:RestClient} + */ + var guardianFactorsAuth0RestClient = new Auth0RestClient( + options.baseUrl + '/guardian/factors/:factor', + clientOptions, + options.tokenProvider + ); + this.factors = new RetryRestClient(guardianFactorsAuth0RestClient, options.retry); + + /** + * Provides an abstraction layer for retrieving Guardian factors. + * + * @type {external:RestClient} + */ + var guardianFactorsTemplatesAuth0RestClient = new Auth0RestClient( + options.baseUrl + '/guardian/factors/:factor/templates', + clientOptions, + options.tokenProvider + ); + this.factorsTemplates = new RetryRestClient( + guardianFactorsTemplatesAuth0RestClient, + options.retry + ); + + /** + * Provides an abstraction layer for retrieving Guardian factor providers. + * + * @type {external:RestClient} + */ + var guardianFactorsProvidersAuth0RestClient = new Auth0RestClient( + options.baseUrl + '/guardian/factors/:factor/providers/:provider', + clientOptions, + options.tokenProvider + ); + this.factorsProviders = new RetryRestClient( + guardianFactorsProvidersAuth0RestClient, + options.retry + ); }; /** @@ -58,7 +113,7 @@ var GuardianManager = function(options) { * @memberOf module:management.GuardianManager.prototype * * @example - * management.users.getGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollment) { + * management.guardian.getGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollment) { * console.log(enrollment); * }); * @@ -68,9 +123,7 @@ var GuardianManager = function(options) { * * @return {Promise|undefined} */ -GuardianManager.prototype.getGuardianEnrollment = function(params, cb) { - return this.enrollments.get(params, cb); -}; +utils.wrapPropertyMethod(GuardianManager, 'getGuardianEnrollment', 'enrollments.get'); /** * Delete a Guardian enrollment. @@ -79,7 +132,7 @@ GuardianManager.prototype.getGuardianEnrollment = function(params, cb) { * @memberOf module:management.GuardianManager.prototype * * @example - * management.users.deleteGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollments) { + * management.guardian.deleteGuardianEnrollment({ id: ENROLLMENT_ID }, function (err, enrollments) { * console.log(enrollments); * }); * @@ -89,8 +142,136 @@ GuardianManager.prototype.getGuardianEnrollment = function(params, cb) { * * @return {Promise|undefined} */ -GuardianManager.prototype.deleteGuardianEnrollment = function(params, cb) { - return this.enrollments.delete(params, cb); -}; +utils.wrapPropertyMethod(GuardianManager, 'deleteGuardianEnrollment', 'enrollments.delete'); + +/** + * Create a Guardian enrollment ticket. + * + * @method createEnrollmentTicket + * @memberOf module:management.GuardianManager.prototype + * + * @example + * management.guardian.createEnrollmentTicket(function (err, ticket) { + * console.log(ticket); + * }); + * + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'createEnrollmentTicket', 'tickets.create'); + +/** + * Get a list of factors and statuses. + * + * @method getFactors + * @memberOf module:management.GuardianManager.prototype + * + * management.guardian.getAll(function (err, factors) { + * console.log(factors.length); + * }); + * + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'getFactors', 'factors.getAll'); + +/** + * Get Guardian factor provider configuration + * + * @method getFactorProvider + * @memberOf module:management.GuardianManager.prototype + * + * management.guardian.getFactorProvider({ factor: 'sms', provider: 'twilio'}, function (err, provider) { + * console.log(provider); + * }); + * + * @param {Object} params Factor provider parameters. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'getFactorProvider', 'factorsProviders.get'); + +/** + * Update Guardian's factor provider + * + * @method updateFactorProvider + * @memberOf module:management.GuardianManager.prototype + * + * management.guardian.updateFactorProvider({ factor: 'sms', provider: 'twilio' }, { + * messaging_service_sid: 'XXXXXXXXXXXXXX', + * auth_token: 'XXXXXXXXXXXXXX', + * sid: 'XXXXXXXXXXXXXX' + * }, function(err, provider) { + * console.log(provider); + * }); + * + * @param {Object} params Factor provider parameters. + * @param {Object} data Updated Factor provider data. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'updateFactorProvider', 'factorsProviders.update'); + +/** + * Get Guardian enrollment and verification factor templates + * + * @method getFactorTemplates + * @memberOf module:management.GuardianManager.prototype + * + * management.guardian.getFactorTemplates({ factor: 'sms' }, function (err, templates) { + * console.log(templates); + * }); + * + * @param {Object} params Factor parameters. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'getFactorTemplates', 'factorsTemplates.get'); + +/** + * Update Guardian enrollment and verification factor templates + * + * @method updateFactorTemplates + * @memberOf module:management.GuardianManager.prototype + * + * management.guardian.updateFactorProvider({ factor: 'sms' }, { + * enrollment_message: "{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.", + * verification_message: "{{code}} is your verification code for {{tenant.friendly_name}}" + * }, function(err, templates) { + * console.log(templates); + * }); + * + * @param {Object} params Factor parameters. + * @param {Object} data Updated factor templates data. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'updateFactorTemplates', 'factorsTemplates.update'); + +/** + * Update Guardian Factor + * + * @method updateFactor + * @memberOf module:management.GuardianManager.prototype + * + * management.guardian.updateFactor({ factor: 'sms' }, { + * enabled: true + * }, function(err, factor) { + * console.log(factor); + * }); + * + * @param {Object} params Factor parameters. + * @param {Object} data Updated factor data. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(GuardianManager, 'updateFactor', 'factors.update'); module.exports = GuardianManager; diff --git a/src/management/index.js b/src/management/index.js index ba35e4734..95711e72f 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -1929,4 +1929,154 @@ utils.wrapPropertyMethod(ManagementClient, 'getRulesConfigs', 'rulesConfigs.getA */ utils.wrapPropertyMethod(ManagementClient, 'deleteRulesConfig', 'rulesConfigs.delete'); +/** + * Create a Guardian enrollment ticket. + * + * @method createGuardianEnrollmentTicket + * @memberOf module:management.GuardianManager.prototype + * + * @example + * management.createGuardianEnrollmentTicket(function (err, ticket) { + * console.log(ticket); + * }); + * + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod( + ManagementClient, + 'createGuardianEnrollmentTicket', + 'guardian.tickets.create' +); + +/** + * Get a list of Guardian factors and statuses. + * + * @method getGuardianFactors + * @memberOf module:management.GuardianManager.prototype + * + * management.getGuardianFactors(function (err, factors) { + * console.log(factors.length); + * }); + * + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(ManagementClient, 'getGuardianFactors', 'guardian.factors.getAll'); + +/** + * Get Guardian factor provider configuration + * + * @method getGuardianFactorProvider + * @memberOf module:management.GuardianManager.prototype + * + * management.getFactorProvider({ factor: 'sms', provider: 'twilio'}, function (err, provider) { + * console.log(provider); + * }); + * + * @param {Object} params Factor provider parameters. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod( + ManagementClient, + 'getGuardianFactorProvider', + 'guardian.factorsProviders.get' +); + +/** + * Update Guardian's factor provider + * + * @method updateFactorProvider + * @memberOf module:management.GuardianManager.prototype + * + * management.updateGuardianFactorProvider({ factor: 'sms', provider: 'twilio' }, { + * messaging_service_sid: 'XXXXXXXXXXXXXX', + * auth_token: 'XXXXXXXXXXXXXX', + * sid: 'XXXXXXXXXXXXXX' + * }, function(err, provider) { + * console.log(provider); + * }); + * + * @param {Object} params Factor provider parameters. + * @param {Object} data Updated Factor provider data. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod( + ManagementClient, + 'updateGuardianFactorProvider', + 'guardian.factorsProviders.update' +); + +/** + * Get Guardian enrollment and verification factor templates + * + * @method getGuardianFactorTemplates + * @memberOf module:management.GuardianManager.prototype + * + * management.getGuardianFactorTemplates({ factor: 'sms' }, function (err, templates) { + * console.log(templates); + * }); + * + * @param {Object} params Factor parameters. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod( + ManagementClient, + 'getGuardianFactorTemplates', + 'guardian.factorsTemplates.get' +); + +/** + * Update Guardian enrollment and verification factor templates + * + * @method updateGuardianFactorTemplates + * @memberOf module:management.GuardianManager.prototype + * + * management.updateGuardianFactorTemplates({ factor: 'sms' }, { + * enrollment_message: "{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.", + * verification_message: "{{code}} is your verification code for {{tenant.friendly_name}}" + * }, function(err, templates) { + * console.log(templates); + * }); + * + * @param {Object} params Factor parameters. + * @param {Object} data Updated factor templates data. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod( + ManagementClient, + 'updateGuardianFactorTemplates', + 'guardian.factorsTemplates.update' +); + +/** + * Update Guardian Factor + * + * @method updateGuardianFactor + * @memberOf module.GuardianManager.prototype + * + * management.updateGuardianFactor({ factor: 'sms' }, { + * enabled: true + * }, function(err, factor) { + * console.log(factor); + * }); + * + * @param {Object} params Factor parameters. + * @param {Object} data Updated factor data. + * @param {Function} [cb] Callback function. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(ManagementClient, 'updateGuardianFactor', 'guardian.factors.update'); + module.exports = ManagementClient; From e895d914ca2d53b1d19f64f95027cc23a505ab56 Mon Sep 17 00:00:00 2001 From: Prashant Trivedy Date: Tue, 6 Nov 2018 09:25:00 +1300 Subject: [PATCH 02/11] Added client_id parameter to docs for verifyEmail --- docs/management_JobsManager.js.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/management_JobsManager.js.html b/docs/management_JobsManager.js.html index f7e52792c..7bb900673 100644 --- a/docs/management_JobsManager.js.html +++ b/docs/management_JobsManager.js.html @@ -235,7 +235,8 @@

management/JobsManager.js

* * @example * var params = { - * user_id: '{USER_ID}' + * user_id: '{USER_ID}', + * client_id: '{CLIENT_ID}' * }; * * management.jobs.verifyEmail(function (err) { @@ -246,6 +247,7 @@

management/JobsManager.js

* * @param {Object} data User data object. * @param {String} data.user_id ID of the user to be verified. + * @param {String} data.client_id ID of the client for which the verification email will be sent. * @param {Function} [cb] Callback function. * * @return {Promise|undefined} From 84a339aad5036d6d85a21e4421e2b07aae2eb34a Mon Sep 17 00:00:00 2001 From: Steve Adams Date: Wed, 7 Nov 2018 08:55:08 +1100 Subject: [PATCH 03/11] add guardian tests --- test/management/guardian.tests.js | 519 +++++++++++++++++++++++++++++- 1 file changed, 518 insertions(+), 1 deletion(-) diff --git a/test/management/guardian.tests.js b/test/management/guardian.tests.js index 75f7e2bac..89c98f778 100644 --- a/test/management/guardian.tests.js +++ b/test/management/guardian.tests.js @@ -17,7 +17,16 @@ describe('GuardianManager', function() { }); describe('instance', function() { - var methods = ['getGuardianEnrollment', 'deleteGuardianEnrollment']; + var methods = [ + 'getGuardianEnrollment', + 'deleteGuardianEnrollment', + 'getFactors', + 'getFactorProvider', + 'updateFactorProvider', + 'getFactorTemplates', + 'updateFactorTemplates', + 'updateFactor' + ]; methods.forEach(function(method) { it('should have a ' + method + ' method', function() { @@ -186,4 +195,512 @@ describe('GuardianManager', function() { }); }); }); + + describe('#createEnrollmentTicket', function() { + var data = { + user_id: '', + email: '', + send_mail: false + }; + + beforeEach(function() { + this.request = nock(API_URL) + .post('/guardian/enrollments/ticket') + .reply(200); + }); + + it('should accept a callback', function(done) { + this.guardian.createEnrollmentTicket(data, function() { + done(); + }); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .createEnrollmentTicket(data) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should pass any errors to the promise catch handler', function(done) { + nock.cleanAll(); + + var request = nock(API_URL) + .post('/guardian/enrollments/ticket') + .reply(500); + + this.guardian.createEnrollmentTicket(data).catch(function(err) { + expect(err).to.exist; + + done(); + }); + }); + + it('should perform a POST request to /api/v2/guardian/enrollments/ticket', function(done) { + var request = this.request; + + this.guardian.createEnrollmentTicket(data).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + + it('should pass the data in the body of the request', function(done) { + nock.cleanAll(); + + var request = nock(API_URL) + .post('/guardian/enrollments/ticket', data) + .reply(200); + + this.guardian.createEnrollmentTicket(data).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + + it('should include the token in the Authorization header', function(done) { + nock.cleanAll(); + + var request = nock(API_URL) + .post('/guardian/enrollments/ticket') + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.createEnrollmentTicket(data).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); + + describe('#getFactors', function() { + beforeEach(function() { + this.data = [ + { name: 'sms', enabled: true, trial_expired: false }, + { name: 'push-notification', enabled: false, trial_expired: false }, + { name: 'otp', enabled: false, trial_expired: false }, + { name: 'email', enabled: false, trial_expired: false }, + { name: 'duo', enabled: false, trial_expired: false } + ]; + + this.request = nock(API_URL) + .get('/guardian/factors') + .reply(200, this.data); + }); + + it('should accept a callback', function(done) { + this.guardian.getFactors(done.bind(null, null)); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .getFactors() + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should perform a POST request to /api/v2/guardian/factors', function(done) { + var request = this.request; + + this.guardian.getFactors().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') + .reply(500); + + this.guardian.getFactors().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') + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.getFactors().then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); + + describe('#getFactorProvider', function() { + beforeEach(function() { + this.params = { factor: 'sms', provider: 'twilio' }; + this.data = { + from: '+1223323', + messaging_service_sid: '5dEkAiHLPCuQ1uJj4qNXcAnERFAL6cpq', + auth_token: 'zw5Ku6z2sxhd0ZVXto5SDHX6KPDByJPU', + sid: 'wywA2BH4VqTpfywiDuyDAYZL3xQjoO40' + }; + + this.request = nock(API_URL) + .get('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .reply(200, this.data); + }); + + it('should accept a callback', function(done) { + this.guardian.getFactorProvider(this.params, done.bind(null, null)); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .getFactorProvider(this.params) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should perform a POST request to /api/v2/guardian/factors/sms/twilio', function(done) { + var request = this.request; + + this.guardian.getFactorProvider(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.factor + '/providers/' + this.params.provider) + .reply(500); + + this.guardian.getFactorProvider(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.factor + '/providers/' + this.params.provider) + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.getFactorProvider(this.params).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); + + describe('#updateFactorProvider', function() { + beforeEach(function() { + this.params = { factor: 'sms', provider: 'twilio' }; + this.data = { + from: '+1223323', + messaging_service_sid: '5dEkAiHLPCuQ1uJj4qNXcAnERFAL6cpq', + auth_token: 'zw5Ku6z2sxhd0ZVXto5SDHX6KPDByJPU', + sid: 'wywA2BH4VqTpfywiDuyDAYZL3xQjoO40' + }; + }); + + it('should accept a callback', function(done) { + this.guardian.updateFactorProvider({ id: 5 }, {}, done.bind(null, null)); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .updateFactorProvider(this.params, {}) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should perform a PUT request to /api/v2/guardian/factors/sms/providers/twilio', function(done) { + var request = nock(API_URL) + .put('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .reply(200, this.data); + + this.guardian.updateFactorProvider(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.factor + '/providers/' + this.params.provider) + .reply(200); + + this.guardian.updateFactorProvider(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.factor + '/providers/' + this.params.provider) + .reply(500); + + this.guardian.updateFactorProvider(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.factor + '/providers/' + this.params.provider) + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.updateFactorProvider(this.params, this.data).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); + + describe('#getFactorTemplates', function() { + beforeEach(function() { + this.params = { factor: 'sms' }; + + this.data = { + enrollment_message: + '{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.', + verification_message: '{{code}} is your verification code for {{tenant.friendly_name}}' + }; + + this.request = nock(API_URL) + .get('/guardian/factors/sms/templates') + .reply(200, this.data); + }); + + it('should accept a callback', function(done) { + this.guardian.getFactorTemplates(this.params, done.bind(null, null)); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .getFactorTemplates(this.params) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should perform a POST request to /api/v2/guardian/factors/sms/templates', function(done) { + var request = this.request; + + this.guardian.getFactorTemplates(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/sms/templates') + .reply(500); + + this.guardian.getFactorTemplates(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/sms/templates') + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.getFactorTemplates(this.params).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); + + describe('#updateFactorTemplates', function() { + beforeEach(function() { + this.params = { factor: 'sms' }; + this.data = { + enrollment_message: + '{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.', + verification_message: '{{code}} is your verification code for {{tenant.friendly_name}}' + }; + }); + + it('should accept a callback', function(done) { + this.guardian.updateFactorTemplates(this.params, {}, done.bind(null, null)); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .updateFactorTemplates(this.params, this.data) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should perform a PUT request to /api/v2/guardian/factors/sms/templates', function(done) { + var request = nock(API_URL) + .put('/guardian/factors/' + this.params.factor + '/templates') + .reply(200, this.data); + + this.guardian.updateFactorTemplates(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.factor + '/templates') + .reply(200); + + this.guardian.updateFactorTemplates(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.factor + '/templates') + .reply(500); + + this.guardian.updateFactorTemplates(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.factor + '/templates') + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.updateFactorTemplates(this.params, this.data).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); + + describe('#updateFactor', function() { + beforeEach(function() { + this.params = { factor: 'sms' }; + this.data = { + enabled: true + }; + }); + + it('should accept a callback', function(done) { + this.guardian.updateFactor(this.params, {}, done.bind(null, null)); + }); + + it('should return a promise if no callback is given', function(done) { + this.guardian + .updateFactor(this.params, this.data) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + + it('should perform a PUT request to /api/v2/guardian/factors/sms', function(done) { + var request = nock(API_URL) + .put('/guardian/factors/' + this.params.factor) + .reply(200, this.data); + + this.guardian.updateFactor(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.factor) + .reply(200); + + this.guardian.updateFactor(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.factor) + .reply(500); + + this.guardian.updateFactor(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.factor) + .matchHeader('Authorization', 'Bearer ' + this.token) + .reply(200); + + this.guardian.updateFactor(this.params, this.data).then(function() { + expect(request.isDone()).to.be.true; + + done(); + }); + }); + }); }); From b8a1984c1c5e10f289cff5d7bfbcf5790899aeb5 Mon Sep 17 00:00:00 2001 From: Alan K Date: Thu, 8 Nov 2018 15:12:21 -0500 Subject: [PATCH 04/11] Adds support for fetching an auth token using a refresh token --- src/auth/OAuthAuthenticator.js | 55 +++++++++++++++++ src/auth/index.js | 35 +++++++++++ test/auth/oauth.tests.js | 105 ++++++++++++++++++++++++++++++++- 3 files changed, 194 insertions(+), 1 deletion(-) diff --git a/src/auth/OAuthAuthenticator.js b/src/auth/OAuthAuthenticator.js index 0d2b33d81..923304a8c 100644 --- a/src/auth/OAuthAuthenticator.js +++ b/src/auth/OAuthAuthenticator.js @@ -179,6 +179,61 @@ OAuthAuthenticator.prototype.passwordGrant = function(userData, cb) { return this.oauthWithIDTokenValidation.create(params, data); }; +/** + * Sign in using a refresh token + * + * @method refresh + * @memberOf module:auth.OAuthAuthenticator.prototype + * + * @example + * Given a refresh token from a previous authentication request + * it will return a JSON with the access_token and id_token. + * More information in the + * + * API Docs + * . + * + * + * var data = { + * client_id: '{CLIENT_ID}', // Optional field. + * refresh_token: '{REFRESH_TOKEN}', + * }; + * + * auth0.oauth.refresh(data, function (err, userData) { + * if (err) { + * // Handle error. + * } + * + * console.log(userData); + * }); + * + * @param {Object} userData User credentials object. + * @param {String} userData.refresh_token Refresh token. + * + * @return {Promise|undefined} + */ +OAuthAuthenticator.prototype.refresh = function (userData, cb) { + var params = { + type: 'token' + }; + var defaultFields = { + client_id: this.clientId, + grant_type: 'refresh_token' + }; + var data = extend(defaultFields, userData); + if (!userData || typeof userData !== 'object') { + throw new ArgumentError('Missing user data object'); + } + if (typeof data.refresh_token !== 'string' + || data.refresh_token.split().length === 0) { + throw new ArgumentError('refresh_token field is required'); + } + if (cb && cb instanceof Function) { + return this.oauthWithIDTokenValidation.create(params, data, cb); + } + return this.oauthWithIDTokenValidation.create(params, data); +}; + /** * Sign in using a social provider access token. * diff --git a/src/auth/index.js b/src/auth/index.js index fa410aa9c..5c9ee63c9 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -543,4 +543,39 @@ utils.wrapPropertyMethod( */ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordGrant'); +/** + * Sign in using a refresh token + * + * @method refresh + * @memberOf module:auth.OAuthAuthenticator.prototype + * + * @example + * Given a refresh token from a previous authentication request + * it will return a JSON with the access_token and id_token. + * More information in the + * + * API Docs + * . + * + * + * var data = { + * client_id: '{CLIENT_ID}', // Optional field. + * refresh_token: '{REFRESH_TOKEN}', + * }; + * + * auth0.oauth.refresh(data, function (err, userData) { + * if (err) { + * // Handle error. + * } + * + * console.log(userData); + * }); + * + * @param {Object} userData User credentials object. + * @param {String} userData.refresh_token Refresh token. + * + * @return {Promise|undefined} + */ +utils.wrapPropertyMethod(AuthenticationClient, 'refresh', 'oauth.refresh'); + module.exports = AuthenticationClient; diff --git a/test/auth/oauth.tests.js b/test/auth/oauth.tests.js index 3dcb436a2..fecfa5018 100644 --- a/test/auth/oauth.tests.js +++ b/test/auth/oauth.tests.js @@ -44,7 +44,7 @@ describe('OAuthAuthenticator', function() { }); describe('instance', function() { - var methods = ['signIn', 'socialSignIn', 'passwordGrant', 'authorizationCodeGrant']; + var methods = ['signIn', 'socialSignIn', 'passwordGrant', 'authorizationCodeGrant', 'refresh']; var authenticator = new Authenticator(validOptions); methods.forEach(function(method) { @@ -405,6 +405,109 @@ describe('OAuthAuthenticator', function() { }); }); + describe('#refresh', function () { + var path = '/oauth/token'; + var userData = { + refresh_token: 'refresh_token' + }; + beforeEach(function () { + this.authenticator = new Authenticator(validOptions); + this.request = nock(API_URL) + .post(path) + .reply(200); + }); + it('should require an object as first argument', function () { + expect(this.authenticator.refresh) + .to.throw(ArgumentError, 'Missing user data object'); + }); + it('should require a refresh_token', function () { + var auth = this.authenticator; + var refresh = auth.refresh.bind(auth, {}); + expect(refresh) + .to.throw(ArgumentError, 'refresh_token field is required'); + }); + it('should accept a callback', function (done) { + this + .authenticator + .refresh(userData, done.bind(null, null)); + }); + it('should return a promise when no callback is provided', function (done) { + this + .authenticator + .refresh(userData) + .then(done.bind(null, null)) + .catch(done.bind(null, null)); + }); + it('should perform a POST request to ' + path, function (done) { + var request = this.request; + this + .authenticator + .refresh(userData) + .then(function () { + expect(request.isDone()) + .to.be.true; + done(); + }) + .catch(done); + }); + it('should include the user data in the request', function (done) { + nock.cleanAll(); + var request = nock(API_URL) + .post(path, function (body) { + for (var property in userData) { + if (userData[property] !== body[property]) { + return false; + } + } + return true; + }) + .reply(200); + this + .authenticator + .refresh(userData) + .then(function () { + expect(request.isDone()) + .to.be.true; + done(); + }) + .catch(done); + }); + it('should include the Auth0 client ID in the request', function (done) { + nock.cleanAll(); + var request = nock(API_URL) + .post(path, function (body) { + return body.client_id === CLIENT_ID; + }) + .reply(200); + this + .authenticator + .refresh(userData) + .then(function () { + expect(request.isDone()) + .to.be.true; + done(); + }) + .catch(done); + }); + it('should use refresh_token as default grant type', function (done) { + nock.cleanAll(); + var request = nock(API_URL) + .post(path, function (body) { + return body.grant_type === 'refresh_token'; + }) + .reply(200); + this + .authenticator + .refresh(userData) + .then(function () { + expect(request.isDone()) + .to.be.true; + done(); + }) + .catch(done); + }); + }); + describe('#socialSignIn', function() { var path = '/oauth/access_token'; var userData = { From 886da19ae97001a86f9992972f466bd7e73f8282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Rudge?= Date: Thu, 8 Nov 2018 16:50:01 -0500 Subject: [PATCH 05/11] Update src/auth/index.js Co-Authored-By: modeswitch --- src/auth/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auth/index.js b/src/auth/index.js index 5c9ee63c9..d00ae41db 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -547,7 +547,7 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG * Sign in using a refresh token * * @method refresh - * @memberOf module:auth.OAuthAuthenticator.prototype + * @memberOf module:auth.AuthenticationClient.prototype * * @example * Given a refresh token from a previous authentication request From 455afb2a94b30a0f34f45836602385d2f7a00f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Rudge?= Date: Thu, 8 Nov 2018 16:50:13 -0500 Subject: [PATCH 06/11] Update src/auth/index.js Co-Authored-By: modeswitch --- src/auth/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auth/index.js b/src/auth/index.js index d00ae41db..63027c784 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -563,7 +563,7 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG * refresh_token: '{REFRESH_TOKEN}', * }; * - * auth0.oauth.refresh(data, function (err, userData) { + * auth0.refresh(data, function (err, userData) { * if (err) { * // Handle error. * } From ddde0d30cb985c011eb3596cb4ea1f87c2f84ae0 Mon Sep 17 00:00:00 2001 From: Alan K Date: Thu, 8 Nov 2018 18:54:40 -0500 Subject: [PATCH 07/11] Rename userData parameter refresh_token to refreshToken --- src/auth/OAuthAuthenticator.js | 10 +++++----- src/auth/index.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/auth/OAuthAuthenticator.js b/src/auth/OAuthAuthenticator.js index 923304a8c..442c9b4e8 100644 --- a/src/auth/OAuthAuthenticator.js +++ b/src/auth/OAuthAuthenticator.js @@ -196,7 +196,7 @@ OAuthAuthenticator.prototype.passwordGrant = function(userData, cb) { * * var data = { * client_id: '{CLIENT_ID}', // Optional field. - * refresh_token: '{REFRESH_TOKEN}', + * refreshToken: '{REFRESH_TOKEN}', * }; * * auth0.oauth.refresh(data, function (err, userData) { @@ -208,7 +208,7 @@ OAuthAuthenticator.prototype.passwordGrant = function(userData, cb) { * }); * * @param {Object} userData User credentials object. - * @param {String} userData.refresh_token Refresh token. + * @param {String} userData.refreshToken Refresh token. * * @return {Promise|undefined} */ @@ -224,9 +224,9 @@ OAuthAuthenticator.prototype.refresh = function (userData, cb) { if (!userData || typeof userData !== 'object') { throw new ArgumentError('Missing user data object'); } - if (typeof data.refresh_token !== 'string' - || data.refresh_token.split().length === 0) { - throw new ArgumentError('refresh_token field is required'); + if (typeof data.refreshToken !== 'string' + || data.refreshToken.split().length === 0) { + throw new ArgumentError('refreshToken field is required'); } if (cb && cb instanceof Function) { return this.oauthWithIDTokenValidation.create(params, data, cb); diff --git a/src/auth/index.js b/src/auth/index.js index 5c9ee63c9..cf9e88f80 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -560,7 +560,7 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG * * var data = { * client_id: '{CLIENT_ID}', // Optional field. - * refresh_token: '{REFRESH_TOKEN}', + * refreshToken: '{REFRESH_TOKEN}', * }; * * auth0.oauth.refresh(data, function (err, userData) { @@ -572,7 +572,7 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG * }); * * @param {Object} userData User credentials object. - * @param {String} userData.refresh_token Refresh token. + * @param {String} userData.refreshToken Refresh token. * * @return {Promise|undefined} */ From 7b86d3af2380c4d7f47510b0630c7e488f325e62 Mon Sep 17 00:00:00 2001 From: Alan K Date: Thu, 8 Nov 2018 18:58:13 -0500 Subject: [PATCH 08/11] Update refresh token tests to use refreshToken instead of refresh_token --- test/auth/oauth.tests.js | 73 +++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/test/auth/oauth.tests.js b/test/auth/oauth.tests.js index fecfa5018..b705f3f7e 100644 --- a/test/auth/oauth.tests.js +++ b/test/auth/oauth.tests.js @@ -405,55 +405,48 @@ describe('OAuthAuthenticator', function() { }); }); - describe('#refresh', function () { + describe('#refresh', function() { var path = '/oauth/token'; var userData = { - refresh_token: 'refresh_token' + refreshToken: 'refresh_token' }; - beforeEach(function () { + beforeEach(function() { this.authenticator = new Authenticator(validOptions); this.request = nock(API_URL) .post(path) .reply(200); }); - it('should require an object as first argument', function () { - expect(this.authenticator.refresh) - .to.throw(ArgumentError, 'Missing user data object'); + it('should require an object as first argument', function() { + expect(this.authenticator.refresh).to.throw(ArgumentError, 'Missing user data object'); }); - it('should require a refresh_token', function () { + it('should require a refreshToken', function() { var auth = this.authenticator; var refresh = auth.refresh.bind(auth, {}); - expect(refresh) - .to.throw(ArgumentError, 'refresh_token field is required'); + expect(refresh).to.throw(ArgumentError, 'refreshToken field is required'); }); - it('should accept a callback', function (done) { - this - .authenticator - .refresh(userData, done.bind(null, null)); + it('should accept a callback', function(done) { + this.authenticator.refresh(userData, done.bind(null, null)); }); - it('should return a promise when no callback is provided', function (done) { - this - .authenticator + it('should return a promise when no callback is provided', function(done) { + this.authenticator .refresh(userData) .then(done.bind(null, null)) .catch(done.bind(null, null)); }); - it('should perform a POST request to ' + path, function (done) { + it('should perform a POST request to ' + path, function(done) { var request = this.request; - this - .authenticator + this.authenticator .refresh(userData) - .then(function () { - expect(request.isDone()) - .to.be.true; + .then(function() { + expect(request.isDone()).to.be.true; done(); }) .catch(done); }); - it('should include the user data in the request', function (done) { + it('should include the user data in the request', function(done) { nock.cleanAll(); var request = nock(API_URL) - .post(path, function (body) { + .post(path, function(body) { for (var property in userData) { if (userData[property] !== body[property]) { return false; @@ -462,46 +455,40 @@ describe('OAuthAuthenticator', function() { return true; }) .reply(200); - this - .authenticator + this.authenticator .refresh(userData) - .then(function () { - expect(request.isDone()) - .to.be.true; + .then(function() { + expect(request.isDone()).to.be.true; done(); }) .catch(done); }); - it('should include the Auth0 client ID in the request', function (done) { + it('should include the Auth0 client ID in the request', function(done) { nock.cleanAll(); var request = nock(API_URL) - .post(path, function (body) { + .post(path, function(body) { return body.client_id === CLIENT_ID; }) .reply(200); - this - .authenticator + this.authenticator .refresh(userData) - .then(function () { - expect(request.isDone()) - .to.be.true; + .then(function() { + expect(request.isDone()).to.be.true; done(); }) .catch(done); }); - it('should use refresh_token as default grant type', function (done) { + it('should use refresh_token as default grant type', function(done) { nock.cleanAll(); var request = nock(API_URL) - .post(path, function (body) { + .post(path, function(body) { return body.grant_type === 'refresh_token'; }) .reply(200); - this - .authenticator + this.authenticator .refresh(userData) - .then(function () { - expect(request.isDone()) - .to.be.true; + .then(function() { + expect(request.isDone()).to.be.true; done(); }) .catch(done); From 0219968915695bb541cbce7c64441413137f772f Mon Sep 17 00:00:00 2001 From: Luis Deschamps Rudge Date: Thu, 8 Nov 2018 22:19:02 -0200 Subject: [PATCH 09/11] rename refresh_token --- src/auth/OAuthAuthenticator.js | 17 ++++++++--------- src/auth/index.js | 14 +++++++------- test/auth/oauth.tests.js | 30 ++++++++++++++++++------------ 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/auth/OAuthAuthenticator.js b/src/auth/OAuthAuthenticator.js index 442c9b4e8..d8df93582 100644 --- a/src/auth/OAuthAuthenticator.js +++ b/src/auth/OAuthAuthenticator.js @@ -182,7 +182,7 @@ OAuthAuthenticator.prototype.passwordGrant = function(userData, cb) { /** * Sign in using a refresh token * - * @method refresh + * @method refreshToken * @memberOf module:auth.OAuthAuthenticator.prototype * * @example @@ -195,11 +195,11 @@ OAuthAuthenticator.prototype.passwordGrant = function(userData, cb) { * * * var data = { - * client_id: '{CLIENT_ID}', // Optional field. - * refreshToken: '{REFRESH_TOKEN}', + * client_id: '{CLIENT_ID}', // Optional field. + * refresh_token: '{REFRESH_TOKEN}', * }; * - * auth0.oauth.refresh(data, function (err, userData) { + * auth0.oauth.refreshToken(data, function (err, userData) { * if (err) { * // Handle error. * } @@ -208,11 +208,11 @@ OAuthAuthenticator.prototype.passwordGrant = function(userData, cb) { * }); * * @param {Object} userData User credentials object. - * @param {String} userData.refreshToken Refresh token. + * @param {String} userData.refresh_token Refresh token. * * @return {Promise|undefined} */ -OAuthAuthenticator.prototype.refresh = function (userData, cb) { +OAuthAuthenticator.prototype.refreshToken = function(userData, cb) { var params = { type: 'token' }; @@ -224,9 +224,8 @@ OAuthAuthenticator.prototype.refresh = function (userData, cb) { if (!userData || typeof userData !== 'object') { throw new ArgumentError('Missing user data object'); } - if (typeof data.refreshToken !== 'string' - || data.refreshToken.split().length === 0) { - throw new ArgumentError('refreshToken field is required'); + if (typeof data.refresh_token !== 'string' || data.refresh_token.split().length === 0) { + throw new ArgumentError('refresh_token is required'); } if (cb && cb instanceof Function) { return this.oauthWithIDTokenValidation.create(params, data, cb); diff --git a/src/auth/index.js b/src/auth/index.js index df2be1aad..acb70f253 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -546,11 +546,11 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG /** * Sign in using a refresh token * - * @method refresh + * @method refreshToken * @memberOf module:auth.AuthenticationClient.prototype * * @example - * Given a refresh token from a previous authentication request + * Given a refresh token from a previous authentication request, * it will return a JSON with the access_token and id_token. * More information in the * @@ -559,11 +559,11 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG * * * var data = { - * client_id: '{CLIENT_ID}', // Optional field. - * refreshToken: '{REFRESH_TOKEN}', + * client_id: '{CLIENT_ID}', // Optional field. + * refresh_token: '{REFRESH_TOKEN}', * }; * - * auth0.refresh(data, function (err, userData) { + * auth0.refreshToken(data, function (err, userData) { * if (err) { * // Handle error. * } @@ -572,10 +572,10 @@ utils.wrapPropertyMethod(AuthenticationClient, 'passwordGrant', 'oauth.passwordG * }); * * @param {Object} userData User credentials object. - * @param {String} userData.refreshToken Refresh token. + * @param {String} userData.refresh_token Refresh token. * * @return {Promise|undefined} */ -utils.wrapPropertyMethod(AuthenticationClient, 'refresh', 'oauth.refresh'); +utils.wrapPropertyMethod(AuthenticationClient, 'refreshToken', 'oauth.refreshToken'); module.exports = AuthenticationClient; diff --git a/test/auth/oauth.tests.js b/test/auth/oauth.tests.js index b705f3f7e..f91d4104e 100644 --- a/test/auth/oauth.tests.js +++ b/test/auth/oauth.tests.js @@ -44,7 +44,13 @@ describe('OAuthAuthenticator', function() { }); describe('instance', function() { - var methods = ['signIn', 'socialSignIn', 'passwordGrant', 'authorizationCodeGrant', 'refresh']; + var methods = [ + 'signIn', + 'socialSignIn', + 'passwordGrant', + 'authorizationCodeGrant', + 'refreshToken' + ]; var authenticator = new Authenticator(validOptions); methods.forEach(function(method) { @@ -405,10 +411,10 @@ describe('OAuthAuthenticator', function() { }); }); - describe('#refresh', function() { + describe('#refreshToken', function() { var path = '/oauth/token'; var userData = { - refreshToken: 'refresh_token' + refresh_token: 'refresh_token' }; beforeEach(function() { this.authenticator = new Authenticator(validOptions); @@ -417,26 +423,26 @@ describe('OAuthAuthenticator', function() { .reply(200); }); it('should require an object as first argument', function() { - expect(this.authenticator.refresh).to.throw(ArgumentError, 'Missing user data object'); + expect(this.authenticator.refreshToken).to.throw(ArgumentError, 'Missing user data object'); }); it('should require a refreshToken', function() { var auth = this.authenticator; - var refresh = auth.refresh.bind(auth, {}); - expect(refresh).to.throw(ArgumentError, 'refreshToken field is required'); + var refresh = auth.refreshToken.bind(auth, {}); + expect(refresh).to.throw(ArgumentError, 'refresh_token is required'); }); it('should accept a callback', function(done) { - this.authenticator.refresh(userData, done.bind(null, null)); + this.authenticator.refreshToken(userData, done.bind(null, null)); }); it('should return a promise when no callback is provided', function(done) { this.authenticator - .refresh(userData) + .refreshToken(userData) .then(done.bind(null, null)) .catch(done.bind(null, null)); }); it('should perform a POST request to ' + path, function(done) { var request = this.request; this.authenticator - .refresh(userData) + .refreshToken(userData) .then(function() { expect(request.isDone()).to.be.true; done(); @@ -456,7 +462,7 @@ describe('OAuthAuthenticator', function() { }) .reply(200); this.authenticator - .refresh(userData) + .refreshToken(userData) .then(function() { expect(request.isDone()).to.be.true; done(); @@ -471,7 +477,7 @@ describe('OAuthAuthenticator', function() { }) .reply(200); this.authenticator - .refresh(userData) + .refreshToken(userData) .then(function() { expect(request.isDone()).to.be.true; done(); @@ -486,7 +492,7 @@ describe('OAuthAuthenticator', function() { }) .reply(200); this.authenticator - .refresh(userData) + .refreshToken(userData) .then(function() { expect(request.isDone()).to.be.true; done(); From fc165bafe8c3fd8389d4c4b83d70574471588438 Mon Sep 17 00:00:00 2001 From: Steve Adams Date: Sun, 11 Nov 2018 13:02:27 +1100 Subject: [PATCH 10/11] Typo in getFactors doc --- src/management/GuardianManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/management/GuardianManager.js b/src/management/GuardianManager.js index cf3452cd6..72c87d723 100644 --- a/src/management/GuardianManager.js +++ b/src/management/GuardianManager.js @@ -167,7 +167,7 @@ utils.wrapPropertyMethod(GuardianManager, 'createEnrollmentTicket', 'tickets.cre * @method getFactors * @memberOf module:management.GuardianManager.prototype * - * management.guardian.getAll(function (err, factors) { + * management.guardian.getFactors(function (err, factors) { * console.log(factors.length); * }); * From 349e77d39566e6751b83edf448474fe8a992882b Mon Sep 17 00:00:00 2001 From: Steve Adams Date: Sun, 11 Nov 2018 13:18:58 +1100 Subject: [PATCH 11/11] Change from factor to name in guardian params to match the mgmt api. --- src/management/GuardianManager.js | 16 ++++++------- src/management/index.js | 10 ++++---- test/management/guardian.tests.js | 40 +++++++++++++++---------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/management/GuardianManager.js b/src/management/GuardianManager.js index cf3452cd6..549be3ce1 100644 --- a/src/management/GuardianManager.js +++ b/src/management/GuardianManager.js @@ -69,7 +69,7 @@ var GuardianManager = function(options) { * @type {external:RestClient} */ var guardianFactorsAuth0RestClient = new Auth0RestClient( - options.baseUrl + '/guardian/factors/:factor', + options.baseUrl + '/guardian/factors/:name', clientOptions, options.tokenProvider ); @@ -81,7 +81,7 @@ var GuardianManager = function(options) { * @type {external:RestClient} */ var guardianFactorsTemplatesAuth0RestClient = new Auth0RestClient( - options.baseUrl + '/guardian/factors/:factor/templates', + options.baseUrl + '/guardian/factors/:name/templates', clientOptions, options.tokenProvider ); @@ -96,7 +96,7 @@ var GuardianManager = function(options) { * @type {external:RestClient} */ var guardianFactorsProvidersAuth0RestClient = new Auth0RestClient( - options.baseUrl + '/guardian/factors/:factor/providers/:provider', + options.baseUrl + '/guardian/factors/:name/providers/:provider', clientOptions, options.tokenProvider ); @@ -183,7 +183,7 @@ utils.wrapPropertyMethod(GuardianManager, 'getFactors', 'factors.getAll'); * @method getFactorProvider * @memberOf module:management.GuardianManager.prototype * - * management.guardian.getFactorProvider({ factor: 'sms', provider: 'twilio'}, function (err, provider) { + * management.guardian.getFactorProvider({ name: 'sms', provider: 'twilio'}, function (err, provider) { * console.log(provider); * }); * @@ -200,7 +200,7 @@ utils.wrapPropertyMethod(GuardianManager, 'getFactorProvider', 'factorsProviders * @method updateFactorProvider * @memberOf module:management.GuardianManager.prototype * - * management.guardian.updateFactorProvider({ factor: 'sms', provider: 'twilio' }, { + * management.guardian.updateFactorProvider({ name: 'sms', provider: 'twilio' }, { * messaging_service_sid: 'XXXXXXXXXXXXXX', * auth_token: 'XXXXXXXXXXXXXX', * sid: 'XXXXXXXXXXXXXX' @@ -222,7 +222,7 @@ utils.wrapPropertyMethod(GuardianManager, 'updateFactorProvider', 'factorsProvid * @method getFactorTemplates * @memberOf module:management.GuardianManager.prototype * - * management.guardian.getFactorTemplates({ factor: 'sms' }, function (err, templates) { + * management.guardian.getFactorTemplates({ name: 'sms' }, function (err, templates) { * console.log(templates); * }); * @@ -239,7 +239,7 @@ utils.wrapPropertyMethod(GuardianManager, 'getFactorTemplates', 'factorsTemplate * @method updateFactorTemplates * @memberOf module:management.GuardianManager.prototype * - * management.guardian.updateFactorProvider({ factor: 'sms' }, { + * management.guardian.updateFactorProvider({ name: 'sms' }, { * enrollment_message: "{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.", * verification_message: "{{code}} is your verification code for {{tenant.friendly_name}}" * }, function(err, templates) { @@ -260,7 +260,7 @@ utils.wrapPropertyMethod(GuardianManager, 'updateFactorTemplates', 'factorsTempl * @method updateFactor * @memberOf module:management.GuardianManager.prototype * - * management.guardian.updateFactor({ factor: 'sms' }, { + * management.guardian.updateFactor({ name: 'sms' }, { * enabled: true * }, function(err, factor) { * console.log(factor); diff --git a/src/management/index.js b/src/management/index.js index 95711e72f..598c66a85 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -1972,7 +1972,7 @@ utils.wrapPropertyMethod(ManagementClient, 'getGuardianFactors', 'guardian.facto * @method getGuardianFactorProvider * @memberOf module:management.GuardianManager.prototype * - * management.getFactorProvider({ factor: 'sms', provider: 'twilio'}, function (err, provider) { + * management.getFactorProvider({ name: 'sms', provider: 'twilio'}, function (err, provider) { * console.log(provider); * }); * @@ -1993,7 +1993,7 @@ utils.wrapPropertyMethod( * @method updateFactorProvider * @memberOf module:management.GuardianManager.prototype * - * management.updateGuardianFactorProvider({ factor: 'sms', provider: 'twilio' }, { + * management.updateGuardianFactorProvider({ name: 'sms', provider: 'twilio' }, { * messaging_service_sid: 'XXXXXXXXXXXXXX', * auth_token: 'XXXXXXXXXXXXXX', * sid: 'XXXXXXXXXXXXXX' @@ -2019,7 +2019,7 @@ utils.wrapPropertyMethod( * @method getGuardianFactorTemplates * @memberOf module:management.GuardianManager.prototype * - * management.getGuardianFactorTemplates({ factor: 'sms' }, function (err, templates) { + * management.getGuardianFactorTemplates({ name: 'sms' }, function (err, templates) { * console.log(templates); * }); * @@ -2040,7 +2040,7 @@ utils.wrapPropertyMethod( * @method updateGuardianFactorTemplates * @memberOf module:management.GuardianManager.prototype * - * management.updateGuardianFactorTemplates({ factor: 'sms' }, { + * management.updateGuardianFactorTemplates({ name: 'sms' }, { * enrollment_message: "{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.", * verification_message: "{{code}} is your verification code for {{tenant.friendly_name}}" * }, function(err, templates) { @@ -2065,7 +2065,7 @@ utils.wrapPropertyMethod( * @method updateGuardianFactor * @memberOf module.GuardianManager.prototype * - * management.updateGuardianFactor({ factor: 'sms' }, { + * management.updateGuardianFactor({ name: 'sms' }, { * enabled: true * }, function(err, factor) { * console.log(factor); diff --git a/test/management/guardian.tests.js b/test/management/guardian.tests.js index 89c98f778..b3fd95731 100644 --- a/test/management/guardian.tests.js +++ b/test/management/guardian.tests.js @@ -344,7 +344,7 @@ describe('GuardianManager', function() { describe('#getFactorProvider', function() { beforeEach(function() { - this.params = { factor: 'sms', provider: 'twilio' }; + this.params = { name: 'sms', provider: 'twilio' }; this.data = { from: '+1223323', messaging_service_sid: '5dEkAiHLPCuQ1uJj4qNXcAnERFAL6cpq', @@ -353,7 +353,7 @@ describe('GuardianManager', function() { }; this.request = nock(API_URL) - .get('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .get('/guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .reply(200, this.data); }); @@ -382,7 +382,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .get('guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .get('guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .reply(500); this.guardian.getFactorProvider(this.params).catch(function(err) { @@ -396,7 +396,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .get('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .get('/guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .matchHeader('Authorization', 'Bearer ' + this.token) .reply(200); @@ -410,7 +410,7 @@ describe('GuardianManager', function() { describe('#updateFactorProvider', function() { beforeEach(function() { - this.params = { factor: 'sms', provider: 'twilio' }; + this.params = { name: 'sms', provider: 'twilio' }; this.data = { from: '+1223323', messaging_service_sid: '5dEkAiHLPCuQ1uJj4qNXcAnERFAL6cpq', @@ -432,7 +432,7 @@ describe('GuardianManager', function() { it('should perform a PUT request to /api/v2/guardian/factors/sms/providers/twilio', function(done) { var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .put('/guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .reply(200, this.data); this.guardian.updateFactorProvider(this.params, this.data).then(function() { @@ -446,7 +446,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .put('/guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .reply(200); this.guardian.updateFactorProvider(this.params, this.data).then(function() { @@ -460,7 +460,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .put('/guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .reply(500); this.guardian.updateFactorProvider(this.params, this.data).catch(function(err) { @@ -474,7 +474,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/providers/' + this.params.provider) + .put('/guardian/factors/' + this.params.name + '/providers/' + this.params.provider) .matchHeader('Authorization', 'Bearer ' + this.token) .reply(200); @@ -488,7 +488,7 @@ describe('GuardianManager', function() { describe('#getFactorTemplates', function() { beforeEach(function() { - this.params = { factor: 'sms' }; + this.params = { name: 'sms' }; this.data = { enrollment_message: @@ -554,7 +554,7 @@ describe('GuardianManager', function() { describe('#updateFactorTemplates', function() { beforeEach(function() { - this.params = { factor: 'sms' }; + this.params = { name: 'sms' }; this.data = { enrollment_message: '{{code}} is your verification code for {{tenant.friendly_name}}. Please enter this code to verify your enrollment.', @@ -575,7 +575,7 @@ describe('GuardianManager', function() { it('should perform a PUT request to /api/v2/guardian/factors/sms/templates', function(done) { var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/templates') + .put('/guardian/factors/' + this.params.name + '/templates') .reply(200, this.data); this.guardian.updateFactorTemplates(this.params, this.data).then(function() { @@ -589,7 +589,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/templates') + .put('/guardian/factors/' + this.params.name + '/templates') .reply(200); this.guardian.updateFactorTemplates(this.params, this.data).then(function() { @@ -603,7 +603,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/templates') + .put('/guardian/factors/' + this.params.name + '/templates') .reply(500); this.guardian.updateFactorTemplates(this.params, this.data).catch(function(err) { @@ -617,7 +617,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor + '/templates') + .put('/guardian/factors/' + this.params.name + '/templates') .matchHeader('Authorization', 'Bearer ' + this.token) .reply(200); @@ -631,7 +631,7 @@ describe('GuardianManager', function() { describe('#updateFactor', function() { beforeEach(function() { - this.params = { factor: 'sms' }; + this.params = { name: 'sms' }; this.data = { enabled: true }; @@ -650,7 +650,7 @@ describe('GuardianManager', function() { it('should perform a PUT request to /api/v2/guardian/factors/sms', function(done) { var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor) + .put('/guardian/factors/' + this.params.name) .reply(200, this.data); this.guardian.updateFactor(this.params, this.data).then(function() { @@ -664,7 +664,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor) + .put('/guardian/factors/' + this.params.name) .reply(200); this.guardian.updateFactor(this.params, this.data).then(function() { @@ -678,7 +678,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor) + .put('/guardian/factors/' + this.params.name) .reply(500); this.guardian.updateFactor(this.params, this.data).catch(function(err) { @@ -692,7 +692,7 @@ describe('GuardianManager', function() { nock.cleanAll(); var request = nock(API_URL) - .put('/guardian/factors/' + this.params.factor) + .put('/guardian/factors/' + this.params.name) .matchHeader('Authorization', 'Bearer ' + this.token) .reply(200);