From aab77943eeb7b1f64fc7ab6c70ed124f05e3a56c Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Tue, 7 Aug 2018 15:47:14 +0200 Subject: [PATCH 1/3] Report authentication errors more accurately Fixes #462 Report ResinNotLoggedIn only when error 401 (unauthorised) is received or when user token is null or undefined (that either means user is already logged out or causes logging out with the next request). Every other case rethrow the original error to report the problem better. Change-type: patch Signed-off-by: amdomanska --- lib/auth.coffee | 17 ++++++++++++----- tests/integration/auth.spec.coffee | 8 ++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/auth.coffee b/lib/auth.coffee index a907d015c..8efdb9240 100644 --- a/lib/auth.coffee +++ b/lib/auth.coffee @@ -23,6 +23,14 @@ getAuth = (deps, opts) -> twoFactor = require('./2fa')(deps, opts) exports = {} + normalizeError = (err) -> + if err.statusCode == 401 + return new errors.ResinNotLoggedIn() + else if err.code == 'ResinMalformedToken' and not savedToken? + return new errors.ResinNotLoggedIn() + else + return err + ###* # @namespace resin.auth.twoFactor # @memberof resin.auth @@ -41,8 +49,8 @@ getAuth = (deps, opts) -> .get('body') .tap (body) -> userDetailsCache = body - .catch -> - throw new errors.ResinNotLoggedIn() + .catch (err) -> + throw normalizeError(err) ###* # @summary Return current logged in username @@ -240,10 +248,9 @@ getAuth = (deps, opts) -> exports.getToken = (callback) -> auth.getKey() .then (savedToken) -> - throw new errors.ResinNotLoggedIn() if not savedToken? return savedToken - .catch -> - throw new errors.ResinNotLoggedIn() + .catch (err) -> + throw normalizeError(err) .asCallback(callback) ###* diff --git a/tests/integration/auth.spec.coffee b/tests/integration/auth.spec.coffee index 46712d390..0cffe786a 100644 --- a/tests/integration/auth.spec.coffee +++ b/tests/integration/auth.spec.coffee @@ -42,6 +42,13 @@ describe 'SDK authentication', -> m.chai.expect(promise).to.be.rejectedWith('Unauthorized') + describe 'resin.auth.getToken()', -> + + it 'should be rejected', -> + promise = resin.auth.getToken() + m.chai.expect(promise).to.be.rejected + .and.eventually.have.property('code', 'ResinNotLoggedIn') + describe 'resin.auth.loginWithToken()', -> it 'should be able to login with a session token', -> @@ -122,6 +129,7 @@ describe 'SDK authentication', -> m.chai.expect(promise).to.be.rejectedWith('This email is already taken') + describe 'when logged in with credentials', -> givenLoggedInUser() From 25c1c218430e33151fd8fc9fe323a5d003dcdae4 Mon Sep 17 00:00:00 2001 From: amdomanska Date: Wed, 15 Aug 2018 09:42:40 +0200 Subject: [PATCH 2/3] Change name and logic of normalizeAuthError method Signed-off-by: amdomanska Change-type: patch --- lib/auth.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/auth.coffee b/lib/auth.coffee index 8efdb9240..cbfea05ab 100644 --- a/lib/auth.coffee +++ b/lib/auth.coffee @@ -23,10 +23,10 @@ getAuth = (deps, opts) -> twoFactor = require('./2fa')(deps, opts) exports = {} - normalizeError = (err) -> + normalizeAuthError = (err) -> if err.statusCode == 401 return new errors.ResinNotLoggedIn() - else if err.code == 'ResinMalformedToken' and not savedToken? + else if err.code == 'ResinMalformedToken' return new errors.ResinNotLoggedIn() else return err @@ -50,7 +50,7 @@ getAuth = (deps, opts) -> .tap (body) -> userDetailsCache = body .catch (err) -> - throw normalizeError(err) + throw normalizeAuthError(err) ###* # @summary Return current logged in username @@ -250,7 +250,7 @@ getAuth = (deps, opts) -> .then (savedToken) -> return savedToken .catch (err) -> - throw normalizeError(err) + throw normalizeAuthError(err) .asCallback(callback) ###* From da699d5bca9ad7f18e30699e5c8278b25223ff7a Mon Sep 17 00:00:00 2001 From: "resin-io-versionbot[bot]" Date: Mon, 20 Aug 2018 12:14:37 +0000 Subject: [PATCH 3/3] v10.0.7 --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85e2f21c2..606d25059 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to [Semantic Versioning](http://semver.org/). +## v10.0.7 - 2018-08-20 + +* Change name and logic of normalizeAuthError method #566 [amdomanska] +* Report authentication errors more accurately #566 [Tim Perry] + ## v10.0.6 - 2018-08-17 * Add support for 'all' in logs requests #556 [Tim Perry] diff --git a/package.json b/package.json index 04ba5852f..0711a6d52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resin-sdk", - "version": "10.0.6", + "version": "10.0.7", "description": "The Resin.io JavaScript SDK", "main": "build/resin.js", "types": "typings/resin-sdk.d.ts",