From bc696a765e3731e5fe5e4a58a0917b8a3eb8832b Mon Sep 17 00:00:00 2001 From: German Lena Date: Wed, 16 Nov 2016 15:50:14 -0300 Subject: [PATCH 1/7] Wrapped all the requests to allow to abort --- src/authentication/db-connection.js | 4 +- src/authentication/index.js | 12 +++-- .../passwordless-authentication.js | 4 +- src/helper/request-builder.js | 53 +++++++++++++++++++ src/management/index.js | 10 ++-- src/web-auth/index.js | 21 ++++++-- src/web-auth/redirect.js | 10 ++-- src/web-auth/username-password.js | 2 +- 8 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/authentication/db-connection.js b/src/authentication/db-connection.js index dc74e3f7..9131cf4e 100644 --- a/src/authentication/db-connection.js +++ b/src/authentication/db-connection.js @@ -29,7 +29,7 @@ DBConnection.prototype.signup = function (options, cb) { body = objectHelper.toSnakeCase(body, ['auth0Client']); - this.request + return this.request .post(url) .send(body) .end(responseHandler(cb)); @@ -52,7 +52,7 @@ DBConnection.prototype.changePassword = function (options, cb) { body = objectHelper.toSnakeCase(body, ['auth0Client']); - this.request + return this.request .post(url) .send(body) .end(responseHandler(cb)); diff --git a/src/authentication/index.js b/src/authentication/index.js index f1a4be61..a64ded17 100644 --- a/src/authentication/index.js +++ b/src/authentication/index.js @@ -91,7 +91,11 @@ Authentication.prototype.buildLogoutUrl = function (options) { return urljoin(this.baseOptions.rootUrl, 'v2', 'logout', '?' + qString); }; -Authentication.prototype.ro = function (options, cb) { +Authentication.prototype.login = function (options, cb) { + // oauth/token +}; + +Authentication.prototype.loginWithResourceOwner = function (options, cb) { var url; var body; @@ -113,7 +117,7 @@ Authentication.prototype.ro = function (options, cb) { body.grant_type = body.grant_type || 'password'; - this.request + return this.request .post(url) .send(body) .end(responseHandler(cb)); @@ -127,7 +131,7 @@ Authentication.prototype.userInfo = function (accessToken, cb) { url = urljoin(this.baseOptions.rootUrl, 'userinfo'); - this.request + return this.request .get(url) .set('Authorization', 'Bearer ' + accessToken) .end(responseHandler(cb)); @@ -149,7 +153,7 @@ Authentication.prototype.delegation = function (options, cb) { body = objectHelper.toSnakeCase(body, ['auth0Client']); - this.request + return this.request .post(url) .send(body) .end(responseHandler(cb)); diff --git a/src/authentication/passwordless-authentication.js b/src/authentication/passwordless-authentication.js index 2bff61c1..3bce4219 100644 --- a/src/authentication/passwordless-authentication.js +++ b/src/authentication/passwordless-authentication.js @@ -83,7 +83,7 @@ PasswordlessAuthentication.prototype.start = function (options, cb) { body = objectHelper.toSnakeCase(body, ['auth0Client']); - this.request + return this.request .post(url) .send(body) .end(responseHandler(cb)); @@ -114,7 +114,7 @@ PasswordlessAuthentication.prototype.verify = function (options, cb) { url = urljoin(this.baseOptions.rootUrl, 'passwordless', 'verify'); - this.request + return this.request .post(url) .send(cleanOption) .end(responseHandler(cb)); diff --git a/src/helper/request-builder.js b/src/helper/request-builder.js index 67560b84..ac1671ae 100644 --- a/src/helper/request-builder.js +++ b/src/helper/request-builder.js @@ -3,6 +3,59 @@ var request = require('superagent'); var base64Url = require('./base64_url'); var version = require('../version'); +// ------------------------------------------------ RequestWrapper + +function RequestWrapper(req) { + this.request = req; + this.method = req.method; + this.url = req.url; + this.body = req._data; + this.headers = req._header; +} + +RequestWrapper.prototype.abort = function () { + this.request.abort(); +}; + +RequestWrapper.prototype.getMethod = function () { + return this.method; +}; + +RequestWrapper.prototype.getBody = function () { + return this.body; +}; + +RequestWrapper.prototype.getUrl = function () { + return this.url; +}; + +RequestWrapper.prototype.getHeaders = function () { + return this.headers; +}; + +// ------------------------------------------------ RequestObj + +function RequestObj(req) { + this.request = req; +} + +RequestObj.prototype.set = function (key, value) { + this.request = this.request.set(key, value); + return this; +}; + +RequestObj.prototype.send = function (body) { + this.request = this.request.send(body); + return this; +}; + +RequestObj.prototype.end = function (cb) { + this.request = this.request.end(cb); + return new RequestWrapper(this.request); +}; + +// ------------------------------------------------ RequestBuilder + function RequestBuilder(options) { this._sendTelemetry = options._sendTelemetry === false ? options._sendTelemetry : true; this._telemetryInfo = options._telemetryInfo || null; diff --git a/src/management/index.js b/src/management/index.js index dce1de24..445538cb 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -30,7 +30,7 @@ Management.prototype.getUser = function (userId, cb) { url = urljoin(this.baseOptions.rootUrl, 'users', userId); - this.request + return this.request .get(url) .end(responseHandler(cb)); }; @@ -44,24 +44,24 @@ Management.prototype.patchUserMetadata = function (userId, userMetadata, cb) { url = urljoin(this.baseOptions.rootUrl, 'users', userId); - this.request + return this.request .patch(url) .send({ user_metadata: userMetadata }) .end(responseHandler(cb)); }; -Management.prototype.linkUsers = function (userId, secondaryUserToken, cb) { +Management.prototype.linkUser = function (userId, secondaryUserToken, cb) { var url; /* eslint-disable */ assert.check(userId, { type: 'string', message: 'userId parameter is not valid' }); - assert.check(secondaryUserToken, { type: 'string', + assert.check(secondaryUserToken, { type: 'string', message: 'secondaryUserToken parameter is not valid' }); assert.check(cb, { type: 'function', message: 'cb parameter is not valid' }); /* eslint-enable */ url = urljoin(this.baseOptions.rootUrl, 'users', userId, 'identities'); - this.request + return this.request .post(url) .send({ link_with: secondaryUserToken }) .end(responseHandler(cb)); diff --git a/src/web-auth/index.js b/src/web-auth/index.js index e6e21fb5..10c5f53d 100644 --- a/src/web-auth/index.js +++ b/src/web-auth/index.js @@ -90,9 +90,13 @@ WebAuth.prototype.parseHash = function (hash) { }; }; +WebAuth.prototype.login = function (options, cb) { + // return this.authentication.login(options, cb); +}; + WebAuth.prototype.renewAuth = function (options, cb) { var handler; - var usePostMessage = options.usePostMessage || false; + var usePostMessage = !!options.usePostMessage; var params = objectHelper.merge(this.baseOptions, [ 'clientID', @@ -119,15 +123,24 @@ WebAuth.prototype.renewAuth = function (options, cb) { }; WebAuth.prototype.changePassword = function (options, cb) { - this.authentication.dbConnection.changePassword(options, cb); + return this.authentication.dbConnection.changePassword(options, cb); }; WebAuth.prototype.passwordlessStart = function (options, cb) { - this.authentication.passwordless.start(options, cb); + return this.authentication.passwordless.start(options, cb); +}; + +WebAuth.prototype.passwordlessVerify = function (options, cb) { + // return this.authentication.passwordless.loginWithResourceOwner(...); +}; + +WebAuth.prototype.signup = function (options, cb) { + return this.authentication.dbConnection.signup(options, cb); }; // popup.login +// popup.authorize // popup.passwordlessVerify -// popup.signup +// popup.signupAndLogin module.exports = WebAuth; diff --git a/src/web-auth/redirect.js b/src/web-auth/redirect.js index 3785f97b..a0f67221 100644 --- a/src/web-auth/redirect.js +++ b/src/web-auth/redirect.js @@ -16,7 +16,7 @@ Redirect.prototype.logout = function (options) { Redirect.prototype.login = function (options, cb) { var usernamePassword = new UsernamePassword(this.baseOptions); - usernamePassword.login(options, function (err, data) { + return usernamePassword.login(options, function (err, data) { if (err) { return cb(err); } @@ -24,13 +24,9 @@ Redirect.prototype.login = function (options, cb) { }); }; -Redirect.prototype.signup = function (options, cb) { - this.authentication.dbConnection.signup(options, cb); -}; - Redirect.prototype.signupAndLogin = function (options, cb) { var _this = this; - this.authentication.dbConnection.signup(options, function (err) { + return this.authentication.dbConnection.signup(options, function (err) { if (err) { return cb(err); } @@ -40,7 +36,7 @@ Redirect.prototype.signupAndLogin = function (options, cb) { Redirect.prototype.passwordlessVerify = function (options, cb) { var _this = this; - this.authentication.passwordless.verify(options, function (err) { + return this.authentication.passwordless.verify(options, function (err) { if (err) { return cb(err); } diff --git a/src/web-auth/username-password.js b/src/web-auth/username-password.js index 71fe5770..946f6695 100644 --- a/src/web-auth/username-password.js +++ b/src/web-auth/username-password.js @@ -31,7 +31,7 @@ UsernamePassword.prototype.login = function (options, cb) { body = objectHelper.toSnakeCase(body, ['auth0Client']); - this.request + return this.request .post(url) .send(body) .end(responseHandler(cb)); From 79684c55d5972b75b7519a9e54eee9747c1ffa41 Mon Sep 17 00:00:00 2001 From: German Lena Date: Wed, 16 Nov 2016 15:55:29 -0300 Subject: [PATCH 2/7] updated tests --- test/authentication/ro.test.js | 6 +++--- test/management/management.test.js | 8 ++++---- test/web-auth/redirect.test.js | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/authentication/ro.test.js b/test/authentication/ro.test.js index 78b015eb..a32caae8 100644 --- a/test/authentication/ro.test.js +++ b/test/authentication/ro.test.js @@ -53,7 +53,7 @@ describe('auth0.authentication', function () { }); }); - this.auth0.ro({ + this.auth0.loginWithResourceOwner({ username: 'the username', password: 'the password', connection: 'the_connection', @@ -91,7 +91,7 @@ describe('auth0.authentication', function () { }); }); - this.auth0.ro({ + this.auth0.loginWithResourceOwner({ username: 'the username', password: 'the password', connection: 'the_connection', @@ -131,7 +131,7 @@ describe('auth0.authentication', function () { }); }); - this.auth0.ro({ + this.auth0.loginWithResourceOwner({ clientID: '123', username: 'the username', password: 'the password', diff --git a/test/management/management.test.js b/test/management/management.test.js index b15c5567..1700d341 100644 --- a/test/management/management.test.js +++ b/test/management/management.test.js @@ -203,7 +203,7 @@ describe('auth0.Management', function () { it('should check that userId is valid', function() { expect(() => { - this.auth0.linkUsers(); + this.auth0.linkUser(); }).to.throwException(function (e) { expect(e.message).to.be('userId parameter is not valid'); }); @@ -211,7 +211,7 @@ describe('auth0.Management', function () { it('should check that secondaryUserToken is valid', function() { expect(() => { - this.auth0.linkUsers('...'); + this.auth0.linkUser('...'); }).to.throwException(function (e) { expect(e.message).to.be('secondaryUserToken parameter is not valid'); }); @@ -219,7 +219,7 @@ describe('auth0.Management', function () { it('should check that cb is valid', function() { expect(() => { - this.auth0.linkUsers('...', '...'); + this.auth0.linkUser('...', '...'); }).to.throwException(function (e) { expect(e.message).to.be('cb parameter is not valid'); }); @@ -277,7 +277,7 @@ describe('auth0.Management', function () { }); }); - this.auth0.linkUsers('twitter|191919191919191', 'the_second_token', function(err, user) { + this.auth0.linkUser('twitter|191919191919191', 'the_second_token', function(err, user) { expect(err).to.be(null); expect(user).to.eql([{ 'connection': 'twitter', diff --git a/test/web-auth/redirect.test.js b/test/web-auth/redirect.test.js index c1cab8b1..49bae242 100644 --- a/test/web-auth/redirect.test.js +++ b/test/web-auth/redirect.test.js @@ -52,7 +52,7 @@ describe('auth0.WebAuth.redirect', function () { }); }); - this.auth0.redirect.signup({ + this.auth0.signup({ connection: 'the_connection', email: 'me@example.com', password: '123456' From 51d6909f4b51dc497351560a1ab5c7698612963c Mon Sep 17 00:00:00 2001 From: German Lena Date: Wed, 16 Nov 2016 16:08:35 -0300 Subject: [PATCH 3/7] restructure webauth --- example/index.html | 12 +++++++----- src/web-auth/index.js | 38 +++++++++++++++++++++++++------------- src/web-auth/redirect.js | 24 +++--------------------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/example/index.html b/example/index.html index fff7faa5..d932cb6d 100644 --- a/example/index.html +++ b/example/index.html @@ -130,11 +130,13 @@

Console:

$('#clear-console').click(function () { $('#clear-console').removeClass('icon-budicon-498'); $('#clear-console').addClass('icon-budicon-495'); + htmlConsole.clear(); + setTimeout(function () { $('#clear-console').removeClass('icon-budicon-495'); $('#clear-console').addClass('icon-budicon-498'); - }, 250) + }, 250); }); $('.login-db').click(function (e) { @@ -151,22 +153,22 @@

Console:

$('.login-facebook').click(function (e) { e.preventDefault(); - webAuth.redirect.authorize({ connection: 'facebook' }); + webAuth.login({ connection: 'facebook' }); }); $('.login-twitter').click(function (e) { e.preventDefault(); - webAuth.redirect.authorize({ connection: 'twitter' }); + webAuth.login({ connection: 'twitter' }); }); $('.login-github').click(function (e) { e.preventDefault(); - webAuth.redirect.authorize({ connection: 'github' }); + webAuth.login({ connection: 'github' }); }); $('.logout').click(function (e) { e.preventDefault(); - webAuth.redirect.logout({ returnTo: 'http://localhost:3000/example' }); + webAuth.logout({ returnTo: 'http://localhost:3000/example' }); }); $('.renew-auth').click(function (e) { diff --git a/src/web-auth/index.js b/src/web-auth/index.js index 10c5f53d..c58579e8 100644 --- a/src/web-auth/index.js +++ b/src/web-auth/index.js @@ -7,6 +7,7 @@ var objectHelper = require('../helper/object'); var Authentication = require('../authentication'); var Redirect = require('./redirect'); var SilentAuthenticationHandler = require('./silent-authentication-handler'); +var windowHelper = require('../helper/window'); function WebAuth(options) { /* eslint-disable */ @@ -30,8 +31,8 @@ function WebAuth(options) { this.baseOptions.tenant = this.baseOptions.domain.split('.')[0]; - this.authentication = new Authentication(this.baseOptions); - this.redirect = new Redirect(this.authentication, this.baseOptions); + this.client = new Authentication(this.baseOptions); + this.redirect = new Redirect(this.client, this.baseOptions); } WebAuth.prototype.parseHash = function (hash) { @@ -90,10 +91,6 @@ WebAuth.prototype.parseHash = function (hash) { }; }; -WebAuth.prototype.login = function (options, cb) { - // return this.authentication.login(options, cb); -}; - WebAuth.prototype.renewAuth = function (options, cb) { var handler; var usePostMessage = !!options.usePostMessage; @@ -118,26 +115,41 @@ WebAuth.prototype.renewAuth = function (options, cb) { params = objectHelper.toSnakeCase(params, ['auth0Client']); - handler = new SilentAuthenticationHandler(this, this.authentication.buildAuthorizeUrl(params)); + handler = new SilentAuthenticationHandler(this, this.client.buildAuthorizeUrl(params)); handler.login(usePostMessage, cb); }; WebAuth.prototype.changePassword = function (options, cb) { - return this.authentication.dbConnection.changePassword(options, cb); + return this.client.dbConnection.changePassword(options, cb); }; WebAuth.prototype.passwordlessStart = function (options, cb) { - return this.authentication.passwordless.start(options, cb); + return this.client.passwordless.start(options, cb); }; -WebAuth.prototype.passwordlessVerify = function (options, cb) { - // return this.authentication.passwordless.loginWithResourceOwner(...); +WebAuth.prototype.signup = function (options, cb) { + return this.client.dbConnection.signup(options, cb); }; -WebAuth.prototype.signup = function (options, cb) { - return this.authentication.dbConnection.signup(options, cb); +WebAuth.prototype.login = function (options) { + windowHelper.redirect(this.client.buildAuthorizeUrl(options)); +}; + +WebAuth.prototype.logout = function (options) { + windowHelper.redirect(this.client.buildLogoutUrl(options)); }; +WebAuth.prototype.passwordlessVerify = function (options, cb) { + var _this = this; + return this.client.passwordless.verify(options, function (err) { + if (err) { + return cb(err); + } + windowHelper.redirect(_this.client.passwordless.buildVerifyUrl(options)); + }); +}; + + // popup.login // popup.authorize // popup.passwordlessVerify diff --git a/src/web-auth/redirect.js b/src/web-auth/redirect.js index a0f67221..bbdae436 100644 --- a/src/web-auth/redirect.js +++ b/src/web-auth/redirect.js @@ -1,19 +1,11 @@ var windowHelper = require('../helper/window'); var UsernamePassword = require('./username-password'); -function Redirect(authentication, options) { +function Redirect(client, options) { this.baseOptions = options; - this.authentication = authentication; + this.client = client; } -Redirect.prototype.authorize = function (options) { - windowHelper.redirect(this.authentication.buildAuthorizeUrl(options)); -}; - -Redirect.prototype.logout = function (options) { - windowHelper.redirect(this.authentication.buildLogoutUrl(options)); -}; - Redirect.prototype.login = function (options, cb) { var usernamePassword = new UsernamePassword(this.baseOptions); return usernamePassword.login(options, function (err, data) { @@ -26,7 +18,7 @@ Redirect.prototype.login = function (options, cb) { Redirect.prototype.signupAndLogin = function (options, cb) { var _this = this; - return this.authentication.dbConnection.signup(options, function (err) { + return this.client.dbConnection.signup(options, function (err) { if (err) { return cb(err); } @@ -34,14 +26,4 @@ Redirect.prototype.signupAndLogin = function (options, cb) { }); }; -Redirect.prototype.passwordlessVerify = function (options, cb) { - var _this = this; - return this.authentication.passwordless.verify(options, function (err) { - if (err) { - return cb(err); - } - windowHelper.redirect(_this.authentication.passwordless.buildVerifyUrl(options)); - }); -}; - module.exports = Redirect; From a1fd29ac4799c16383f8e668fd64867df6b2c9e9 Mon Sep 17 00:00:00 2001 From: German Lena Date: Wed, 16 Nov 2016 16:10:12 -0300 Subject: [PATCH 4/7] fixed tests --- test/web-auth/redirect.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/web-auth/redirect.test.js b/test/web-auth/redirect.test.js index 49bae242..29e170ad 100644 --- a/test/web-auth/redirect.test.js +++ b/test/web-auth/redirect.test.js @@ -368,7 +368,7 @@ describe('auth0.WebAuth.redirect', function () { }); }); - this.auth0.redirect.passwordlessVerify({ + this.auth0.passwordlessVerify({ connection: 'the_connection', phoneNumber: '123456', type: 'sms', @@ -418,7 +418,7 @@ describe('auth0.WebAuth.redirect', function () { }); }); - this.auth0.redirect.passwordlessVerify({ + this.auth0.passwordlessVerify({ connection: 'the_connection', phoneNumber: '123456', type: 'sms', @@ -462,7 +462,7 @@ describe('auth0.WebAuth.redirect', function () { }); }); - this.auth0.redirect.passwordlessVerify({ + this.auth0.passwordlessVerify({ connection: 'the_connection', phoneNumber: '123456', type: 'sms', @@ -490,12 +490,12 @@ describe('auth0.WebAuth.redirect', function () { }); it('should redirect to authorize', function () { - this.auth0.redirect.authorize({connection: 'facebook'}) + this.auth0.login({connection: 'facebook'}) expect(global.window.location).to.be('https://me.auth0.com/authorize?client_id=...&response_type=code&redirect_uri=http%3A%2F%2Fpage.com%2Fcallback&connection=facebook'); }); it('should redirect to logout', function () { - this.auth0.redirect.logout({redirect_to: 'http://example.com/logout'}) + this.auth0.logout({redirect_to: 'http://example.com/logout'}) expect(global.window.location).to.be('https://me.auth0.com/v2/logout?client_id=...&redirect_to=http%3A%2F%2Fexample.com%2Flogout'); }); }); From e7181f8b0b3ecf915118579a7a3f80a2176eb250 Mon Sep 17 00:00:00 2001 From: German Lena Date: Wed, 16 Nov 2016 16:43:53 -0300 Subject: [PATCH 5/7] wrap the responses --- src/helper/request-builder.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/helper/request-builder.js b/src/helper/request-builder.js index ac1671ae..61662354 100644 --- a/src/helper/request-builder.js +++ b/src/helper/request-builder.js @@ -81,15 +81,15 @@ RequestBuilder.prototype.getTelemetryData = function () { }; RequestBuilder.prototype.get = function (url) { - return this.setCommonConfiguration(request.get(url)); + return new RequestObj(this.setCommonConfiguration(request.get(url))); }; RequestBuilder.prototype.post = function (url) { - return this.setCommonConfiguration(request.post(url)); + return new RequestObj(this.setCommonConfiguration(request.post(url))); }; RequestBuilder.prototype.patch = function (url) { - return this.setCommonConfiguration(request.patch(url)); + return new RequestObj(this.setCommonConfiguration(request.patch(url))); }; module.exports = RequestBuilder; From 0e67428f8bdb3d9d15db5d34a76e1993030b510d Mon Sep 17 00:00:00 2001 From: German Lena Date: Thu, 17 Nov 2016 11:48:59 -0300 Subject: [PATCH 6/7] added oauth token + improved playground --- example/index.html | 56 +++++++++++++++++++++++++++------- src/authentication/index.js | 47 ++++++++++++++++++++++++++-- src/helper/response-handler.js | 22 ++++++++++++- 3 files changed, 110 insertions(+), 15 deletions(-) diff --git a/example/index.html b/example/index.html index d932cb6d..5e71424a 100644 --- a/example/index.html +++ b/example/index.html @@ -12,6 +12,7 @@ } pre { margin: 0 0 10px 0; + min-height: 300px; } code { min-height: 300px; @@ -39,15 +40,38 @@ diff --git a/src/authentication/index.js b/src/authentication/index.js index a64ded17..a924d249 100644 --- a/src/authentication/index.js +++ b/src/authentication/index.js @@ -92,7 +92,45 @@ Authentication.prototype.buildLogoutUrl = function (options) { }; Authentication.prototype.login = function (options, cb) { - // oauth/token + assert.check(options, { type: 'object', message: 'options parameter is not valid' }, { + clientID: { optional: true, type: 'string', message: 'clientID option is required' }, + username: { optional: true, type: 'string', message: 'username option is required' }, + password: { optional: true, type: 'string', message: 'password option is required' }, + scope: { optional: true, type: 'string', message: 'scope option is required' }, + audience: { optional: true, type: 'string', message: 'audience option is required' } + }); + assert.check(cb, { type: 'function', message: 'cb parameter is not valid' }); + + options.grantType = 'password'; + + return this.oauthToken(options, cb); +}; + +Authentication.prototype.oauthToken = function (options, cb) { + var url; + var body; + + assert.check(options, { type: 'object', message: 'options parameter is not valid' }, { + grantType: { optional: true, type: 'string', message: 'grantType option is required' } + }); + assert.check(cb, { type: 'function', message: 'cb parameter is not valid' }); + + url = urljoin(this.baseOptions.rootUrl, 'oauth', 'token'); + + body = objectHelper.merge(this.baseOptions, [ + 'clientID', + 'scope', + 'audience' + ]).with(options); + + body = objectHelper.toSnakeCase(body, ['auth0Client']); + + body.grant_type = body.grant_type || 'password'; + + return this.request + .post(url) + .send(body) + .end(responseHandler(cb)); }; Authentication.prototype.loginWithResourceOwner = function (options, cb) { @@ -110,8 +148,11 @@ Authentication.prototype.loginWithResourceOwner = function (options, cb) { url = urljoin(this.baseOptions.rootUrl, 'oauth', 'ro'); - body = objectHelper.merge(this.baseOptions, ['clientID']) - .with(options); + body = objectHelper.merge(this.baseOptions, [ + 'clientID', + 'scope', + 'audience' + ]).with(options); body = objectHelper.toSnakeCase(body, ['auth0Client']); diff --git a/src/helper/response-handler.js b/src/helper/response-handler.js index 02587b19..803c0e9a 100644 --- a/src/helper/response-handler.js +++ b/src/helper/response-handler.js @@ -1,7 +1,27 @@ function wrapCallback(cb) { return function (err, data) { + if (err) { - return cb(err); + var data = { + original: err + } + + if (err.response && err.response.statusCode) { + data.status_code = err.response.statusCode; + } + + if (err.response && err.response.statusText) { + data.status_text = err.response.statusText; + } + + if (err.response && err.response.body) { + err = err.response.body; + } + + data.error = err.error || err.code || err.error_code; + data.error_description = err.error_description || err.description || err.error; + + return cb(data); } return cb(null, data.body || data.text); From a31a58c4441d6f8c3973fca74142aa58f9e2bb14 Mon Sep 17 00:00:00 2001 From: German Lena Date: Thu, 17 Nov 2016 13:36:53 -0300 Subject: [PATCH 7/7] fix error managemenet + tests + playground --- example/index.html | 8 ++-- src/helper/response-handler.js | 7 ++-- test/authentication/ro.test.js | 10 ++++- test/web-auth/redirect.test.js | 68 +++++++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 24 deletions(-) diff --git a/example/index.html b/example/index.html index 5e71424a..564a7f75 100644 --- a/example/index.html +++ b/example/index.html @@ -41,11 +41,13 @@ function HTMLConsole(options) { this.ele = $(options.selector); this.data = []; - + var _this = this; var data; if (data = localStorage.getItem("consoleData")) { data = JSON.parse(data); - data.forEach(this.dumpCallback.bind(this)); + data.forEach(function(d){ + _this.dumpCallback(d.error ? d : null, d.error ? null : d); + }); } } @@ -154,7 +156,7 @@

Console:

var hash = webAuth.parseHash(); if (hash) { - htmlConsole.dumpCallback(hash); + htmlConsole.dumpCallback(hash.error ? hash : null, hash.error ? null : hash); window.location.hash = ''; } diff --git a/src/helper/response-handler.js b/src/helper/response-handler.js index 803c0e9a..5352c696 100644 --- a/src/helper/response-handler.js +++ b/src/helper/response-handler.js @@ -4,7 +4,7 @@ function wrapCallback(cb) { if (err) { var data = { original: err - } + }; if (err.response && err.response.statusCode) { data.status_code = err.response.statusCode; @@ -18,8 +18,9 @@ function wrapCallback(cb) { err = err.response.body; } - data.error = err.error || err.code || err.error_code; - data.error_description = err.error_description || err.description || err.error; + data.code = err.error || err.code || err.error_code || null; + data.description = err.error_description || err.description || err.error || null; + data.name = err.name || null; return cb(data); } diff --git a/test/authentication/ro.test.js b/test/authentication/ro.test.js index a32caae8..829e5cfa 100644 --- a/test/authentication/ro.test.js +++ b/test/authentication/ro.test.js @@ -98,7 +98,15 @@ describe('auth0.authentication', function () { scope: 'openid' }, function (err, data) { expect(data).to.be(undefined); - expect(err).to.eql({ error: 'unauthorized', error_description: 'invalid username' }); + expect(err).to.eql({ + original: { + error: 'unauthorized', + error_description: 'invalid username' + }, + code: 'unauthorized', + description: 'invalid username', + name: null + }); done(); }); }); diff --git a/test/web-auth/redirect.test.js b/test/web-auth/redirect.test.js index 29e170ad..c06f82d4 100644 --- a/test/web-auth/redirect.test.js +++ b/test/web-auth/redirect.test.js @@ -165,8 +165,7 @@ describe('auth0.WebAuth.redirect', function () { cb({ 'name': 'ValidationError', 'code': 'invalid_user_password', - 'description': 'Wrong email or password.', - 'statusCode': 400 + 'description': 'Wrong email or password.' }); } }); @@ -188,10 +187,14 @@ describe('auth0.WebAuth.redirect', function () { scope: 'openid' }, function (err) { expect(err).to.eql({ + 'original': { + 'name': 'ValidationError', + 'code': 'invalid_user_password', + 'description': 'Wrong email or password.' + }, 'name': 'ValidationError', 'code': 'invalid_user_password', - 'description': 'Wrong email or password.', - 'statusCode': 400 + 'description': 'Wrong email or password.' }); done(); }); @@ -233,10 +236,14 @@ describe('auth0.WebAuth.redirect', function () { }, cb: function (cb) { cb({ - 'name': 'ValidationError', - 'code': 'invalid_user_password', - 'description': 'Wrong email or password.', - 'statusCode': 400 + response: { + body: { + 'name': 'ValidationError', + 'code': 'invalid_user_password', + 'description': 'Wrong email or password.' + }, + 'statusCode': 400 + } }); } }); @@ -274,10 +281,20 @@ describe('auth0.WebAuth.redirect', function () { }, function (err, data) { expect(data).to.be(undefined); expect(err).to.eql({ + 'original': { + 'response': { + 'body': { + 'name': 'ValidationError', + 'code': 'invalid_user_password', + 'description': 'Wrong email or password.' + }, + 'statusCode': 400 + } + }, 'name': 'ValidationError', 'code': 'invalid_user_password', 'description': 'Wrong email or password.', - 'statusCode': 400 + 'status_code': 400 }); done(); }); @@ -300,10 +317,13 @@ describe('auth0.WebAuth.redirect', function () { }, cb: function (cb) { cb({ - "name":"BadRequestError", - "code":"user_exists", - "description":"The user already exists.", - "statusCode":400 + response: { + "statusCode":400, + body: { + "code":"user_exists", + "description":"The user already exists." + } + } }); } }); @@ -317,10 +337,19 @@ describe('auth0.WebAuth.redirect', function () { }, function (err, data) { expect(data).to.be(undefined); expect(err).to.eql({ - "name":"BadRequestError", + original: { + response: { + "statusCode":400, + body: { + "code":"user_exists", + "description":"The user already exists." + } + } + }, + "name":null, "code":"user_exists", "description":"The user already exists.", - "statusCode":400 + "status_code":400 }); done(); }); @@ -469,8 +498,13 @@ describe('auth0.WebAuth.redirect', function () { verificationCode: 'abc' }, function (err) { expect(err).to.eql({ - error: 'some_error_code', - error_description: 'Some error description' + original: { + error: 'some_error_code', + error_description: 'Some error description' + }, + name: null, + code: 'some_error_code', + description: 'Some error description' }); done(); });