From 453ca63455fccb25d54a0c47799c31c1c57c3448 Mon Sep 17 00:00:00 2001 From: TemuulenBM Date: Thu, 4 Jan 2024 18:00:53 +0800 Subject: [PATCH 1/7] added function that checks 2fa --- addon/authenticators/fleetbase.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/addon/authenticators/fleetbase.js b/addon/authenticators/fleetbase.js index 4b8a50b..98bd5b4 100644 --- a/addon/authenticators/fleetbase.js +++ b/addon/authenticators/fleetbase.js @@ -59,6 +59,12 @@ export default class FleetbaseAuthenticator extends Base { }); } + checkForTwoFactor(identity) { + return this.fetch.get('two-fa/check', { identity }).catch((error) => { + throw new Error(error.message); + }); + } + /** * Invalidates the current session * From 51ffe77b8c0072e6128103f64449abc2bb503cfc Mon Sep 17 00:00:00 2001 From: TemuulenBM Date: Fri, 5 Jan 2024 18:03:47 +0800 Subject: [PATCH 2/7] managed check2fa session --- addon/authenticators/fleetbase.js | 6 ------ addon/services/session.js | 13 +++++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/addon/authenticators/fleetbase.js b/addon/authenticators/fleetbase.js index 98bd5b4..4b8a50b 100644 --- a/addon/authenticators/fleetbase.js +++ b/addon/authenticators/fleetbase.js @@ -59,12 +59,6 @@ export default class FleetbaseAuthenticator extends Base { }); } - checkForTwoFactor(identity) { - return this.fetch.get('two-fa/check', { identity }).catch((error) => { - throw new Error(error.message); - }); - } - /** * Invalidates the current session * diff --git a/addon/services/session.js b/addon/services/session.js index 4cda6dd..745c0d1 100644 --- a/addon/services/session.js +++ b/addon/services/session.js @@ -218,4 +218,17 @@ export default class SessionService extends SimpleAuthSessionService { return Math.round((now - date) / 1000); } + + /** + * Checks for the presence of two-factor authentication for a given user identity. + * + * @param {String} identity + * @return {Promise} + * @throws {Error} + */ + checkForTwoFactor(identity) { + return this.fetch.get('two-fa/check', { identity }).catch((error) => { + throw new Error(error.message); + }); + } } From 4282fa8828d77afacf7ee1f606b65bc74ce7d104 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Tue, 16 Jan 2024 18:19:35 +0800 Subject: [PATCH 3/7] minor patch on application adapter --- addon/adapters/application.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/addon/adapters/application.js b/addon/adapters/application.js index 80cc93f..352ddf1 100644 --- a/addon/adapters/application.js +++ b/addon/adapters/application.js @@ -95,7 +95,9 @@ export default class ApplicationAdapter extends RESTAdapter { const localStorageSession = JSON.parse(window.localStorage.getItem('ember_simple_auth-session')); if (localStorageSession) { const { authenticated } = localStorageSession; - token = authenticated.token; + if (authenticated) { + token = authenticated.token; + } // Check isAuthenticated again isAuthenticated = !!token; From a72e2de01c2935dcaf3f0ba78ca7e471e1b54569 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 18 Jan 2024 12:32:14 +0800 Subject: [PATCH 4/7] remove usage of `extractCoordinates` --- addon/utils/extract-latitude.js | 7 ++----- addon/utils/extract-longitude.js | 5 +---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/addon/utils/extract-latitude.js b/addon/utils/extract-latitude.js index a71f121..4227e43 100644 --- a/addon/utils/extract-latitude.js +++ b/addon/utils/extract-latitude.js @@ -1,17 +1,16 @@ /* eslint-disable no-unused-vars */ -import extractCoordinates from './extract-coordinates'; import Terraformer from 'terraformer'; import { isArray } from '@ember/array'; export default function extractLatitude(position) { - let latitude, longitude; + let [longitude, latitude] = [0, 0]; if (!position) { return 0; } if (position instanceof Terraformer.Point || isArray(position.coordinates)) { - [latitude, longitude] = extractCoordinates(position.coordinates); + [longitude, latitude] = position.coordinates; return latitude; } @@ -22,7 +21,5 @@ export default function extractLatitude(position) { return latitude; } - [latitude, longitude] = extractCoordinates(position); - return latitude; } diff --git a/addon/utils/extract-longitude.js b/addon/utils/extract-longitude.js index bc2f4be..e240c68 100644 --- a/addon/utils/extract-longitude.js +++ b/addon/utils/extract-longitude.js @@ -1,5 +1,4 @@ /* eslint-disable no-unused-vars */ -import extractCoordinates from './extract-coordinates'; import Terraformer from 'terraformer'; import { isArray } from '@ember/array'; @@ -11,7 +10,7 @@ export default function extractLongitude(position) { } if (position instanceof Terraformer.Point || isArray(position.coordinates)) { - [latitude, longitude] = extractCoordinates(position.coordinates); + [longitude, latitude] = position.coordinates; return longitude; } @@ -22,7 +21,5 @@ export default function extractLongitude(position) { return longitude; } - [latitude, longitude] = extractCoordinates(position); - return longitude; } From 9a6155973bf7e96da15d84faeee33a978ed70c1d Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 18 Jan 2024 12:36:04 +0800 Subject: [PATCH 5/7] cleaned up linter, removed `extractLatitude()` and `extractLongitude()` utility --- addon/services/session.js | 2 +- addon/utils/extract-latitude.js | 25 ---------------------- addon/utils/extract-longitude.js | 25 ---------------------- app/utils/extract-latitude.js | 1 - app/utils/extract-longitude.js | 1 - tests/unit/utils/extract-latitude-test.js | 10 --------- tests/unit/utils/extract-longitude-test.js | 10 --------- 7 files changed, 1 insertion(+), 73 deletions(-) delete mode 100644 addon/utils/extract-latitude.js delete mode 100644 addon/utils/extract-longitude.js delete mode 100644 app/utils/extract-latitude.js delete mode 100644 app/utils/extract-longitude.js delete mode 100644 tests/unit/utils/extract-latitude-test.js delete mode 100644 tests/unit/utils/extract-longitude-test.js diff --git a/addon/services/session.js b/addon/services/session.js index 745c0d1..3758124 100644 --- a/addon/services/session.js +++ b/addon/services/session.js @@ -224,7 +224,7 @@ export default class SessionService extends SimpleAuthSessionService { * * @param {String} identity * @return {Promise} - * @throws {Error} + * @throws {Error} */ checkForTwoFactor(identity) { return this.fetch.get('two-fa/check', { identity }).catch((error) => { diff --git a/addon/utils/extract-latitude.js b/addon/utils/extract-latitude.js deleted file mode 100644 index 4227e43..0000000 --- a/addon/utils/extract-latitude.js +++ /dev/null @@ -1,25 +0,0 @@ -/* eslint-disable no-unused-vars */ -import Terraformer from 'terraformer'; -import { isArray } from '@ember/array'; - -export default function extractLatitude(position) { - let [longitude, latitude] = [0, 0]; - - if (!position) { - return 0; - } - - if (position instanceof Terraformer.Point || isArray(position.coordinates)) { - [longitude, latitude] = position.coordinates; - - return latitude; - } - - if (typeof position === 'object') { - let latitude = position['lat'] || position['latitude'] || position['x']; - - return latitude; - } - - return latitude; -} diff --git a/addon/utils/extract-longitude.js b/addon/utils/extract-longitude.js deleted file mode 100644 index e240c68..0000000 --- a/addon/utils/extract-longitude.js +++ /dev/null @@ -1,25 +0,0 @@ -/* eslint-disable no-unused-vars */ -import Terraformer from 'terraformer'; -import { isArray } from '@ember/array'; - -export default function extractLongitude(position) { - let latitude, longitude; - - if (!position) { - return 0; - } - - if (position instanceof Terraformer.Point || isArray(position.coordinates)) { - [longitude, latitude] = position.coordinates; - - return longitude; - } - - if (typeof position === 'object') { - let longitude = position['lng'] || position['longitude'] || position['lon'] || position['y']; - - return longitude; - } - - return longitude; -} diff --git a/app/utils/extract-latitude.js b/app/utils/extract-latitude.js deleted file mode 100644 index 9f0fda8..0000000 --- a/app/utils/extract-latitude.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from '@fleetbase/ember-core/utils/extract-latitude'; diff --git a/app/utils/extract-longitude.js b/app/utils/extract-longitude.js deleted file mode 100644 index 397b5a0..0000000 --- a/app/utils/extract-longitude.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from '@fleetbase/ember-core/utils/extract-longitude'; diff --git a/tests/unit/utils/extract-latitude-test.js b/tests/unit/utils/extract-latitude-test.js deleted file mode 100644 index 1529d50..0000000 --- a/tests/unit/utils/extract-latitude-test.js +++ /dev/null @@ -1,10 +0,0 @@ -import extractLatitude from 'dummy/utils/extract-latitude'; -import { module, test } from 'qunit'; - -module('Unit | Utility | extract-latitude', function () { - // TODO: Replace this with your real tests. - test('it works', function (assert) { - let result = extractLatitude(); - assert.ok(result); - }); -}); diff --git a/tests/unit/utils/extract-longitude-test.js b/tests/unit/utils/extract-longitude-test.js deleted file mode 100644 index 60670ed..0000000 --- a/tests/unit/utils/extract-longitude-test.js +++ /dev/null @@ -1,10 +0,0 @@ -import extractLongitude from 'dummy/utils/extract-longitude'; -import { module, test } from 'qunit'; - -module('Unit | Utility | extract-longitude', function () { - // TODO: Replace this with your real tests. - test('it works', function (assert) { - let result = extractLongitude(); - assert.ok(result); - }); -}); From d7ae46116000d9d7d7a3b24bba263b2746c48ed9 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 18 Jan 2024 13:06:16 +0800 Subject: [PATCH 6/7] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6d08c24..4992e1d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/ember-core", - "version": "0.1.9", + "version": "0.2.0", "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.", "keywords": [ "fleetbase-core", From fc3397151c4fb036ec8f604dc7fa7beefee826fa Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 18 Jan 2024 16:48:04 +0800 Subject: [PATCH 7/7] minor patches to authentication and session --- addon/authenticators/fleetbase.js | 19 +++++++++++++++++-- addon/services/session.js | 7 ++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/addon/authenticators/fleetbase.js b/addon/authenticators/fleetbase.js index 4b8a50b..d29afc9 100644 --- a/addon/authenticators/fleetbase.js +++ b/addon/authenticators/fleetbase.js @@ -1,5 +1,17 @@ import Base from 'ember-simple-auth/authenticators/base'; import { inject as service } from '@ember/service'; +import getWithDefault from '../utils/get-with-default'; + +export class AuthenticationError extends Error { + constructor(message, code) { + super(message); + this.code = code; + } + + getCode() { + return this.code; + } +} export default class FleetbaseAuthenticator extends Base { /** @@ -35,7 +47,7 @@ export default class FleetbaseAuthenticator extends Base { ) .then((response) => { if (response.restore === false) { - return Promise.reject(new Error(response.error)); + return Promise.reject(new AuthenticationError(response.error)); } return response; @@ -52,7 +64,10 @@ export default class FleetbaseAuthenticator extends Base { authenticate(credentials = {}, remember = false, path = 'auth/login') { return this.fetch.post(path, { ...credentials, remember }).then((response) => { if (response.errors) { - return Promise.reject(new Error(response.errors.firstObject ?? 'Authentication failed!')); + const errorMessage = getWithDefault(response.errors, '0', 'Authentication failed!'); + const errorCode = getWithDefault(response, 'code'); + + return Promise.reject(new AuthenticationError(errorMessage, errorCode)); } return response; diff --git a/addon/services/session.js b/addon/services/session.js index 3758124..ea8f061 100644 --- a/addon/services/session.js +++ b/addon/services/session.js @@ -2,6 +2,7 @@ import SimpleAuthSessionService from 'ember-simple-auth/services/session'; import { tracked } from '@glimmer/tracking'; import { inject as service } from '@ember/service'; import { later } from '@ember/runloop'; +import getWithDefault from '../utils/get-with-default'; export default class SessionService extends SimpleAuthSessionService { /** @@ -97,12 +98,12 @@ export default class SessionService extends SimpleAuthSessionService { const user = await this.currentUser.load(); if (!user) { - return this.invalidateWithLoader(`Session authentication failed...`); + return this.invalidateWithLoader('Session authentication failed...'); } return user; } catch (error) { - await this.invalidateWithLoader(error.message ?? `Session authentication failed...`); + await this.invalidateWithLoader(getWithDefault(error, 'message', 'Session authentication failed...')); } } @@ -124,7 +125,7 @@ export default class SessionService extends SimpleAuthSessionService { transition.abort(); } - reject(invalidateWithLoader(`Session authentication failed...`)); + reject(invalidateWithLoader('Session authentication failed...')); } resolve(user);