From 7386b2c9ed08f89120c8d8d77195fb47ccea2c19 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 15 Aug 2022 11:22:27 -0700 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20deauth=20should=20de?= =?UTF-8?q?lete=20the=20auth=20token=20(#1256)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/mirage/config.js | 2 +- addons/auth/addon/authenticators/base.js | 8 ++++++-- addons/auth/tests/unit/authenticators/base-test.js | 10 ++++++---- addons/core/addon/authenticators/password.js | 5 ++--- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index 6c3b7d5aa..e7a504b25 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -138,7 +138,7 @@ export default function () { // Authenticate + auth methods custom routes this.post('/auth-methods/:id_method', authHandler); // Deauthenticate route - this.post('/scopes/:id_method', deauthHandler); + this.delete('/auth-tokens/:id', deauthHandler); // IAM : Users this.get( diff --git a/addons/auth/addon/authenticators/base.js b/addons/auth/addon/authenticators/base.js index deeea5a0d..74e9a4daa 100644 --- a/addons/auth/addon/authenticators/base.js +++ b/addons/auth/addon/authenticators/base.js @@ -105,9 +105,13 @@ export default class BaseAuthenticator extends SimpleAuthBaseAuthenticator { * @override * @return {Promise} */ - invalidate(options) { + async invalidate(options) { + const { token } = options; const deauthEndpointURL = this.buildDeauthEndpointURL(options); - fetch(deauthEndpointURL, { method: 'post' }).catch(() => { + await fetch(deauthEndpointURL, { + method: 'delete', + headers: { Authorization: `Bearer ${token}` }, + }).catch(() => { /* no op */ }); return super.invalidate(...arguments); diff --git a/addons/auth/tests/unit/authenticators/base-test.js b/addons/auth/tests/unit/authenticators/base-test.js index 31d2b3603..c79f21be9 100644 --- a/addons/auth/tests/unit/authenticators/base-test.js +++ b/addons/auth/tests/unit/authenticators/base-test.js @@ -10,22 +10,24 @@ module('Unit | Authenticator | base', function (hooks) { test('it deauthenticates on invalidation', async function (assert) { assert.expect(1); const authenticator = this.owner.lookup('authenticator:base'); - this.server.post(authenticator.buildDeauthEndpointURL(), () => { + const endpoint = authenticator.buildDeauthEndpointURL({ id: 'token123' }); + this.server.delete(endpoint, () => { assert.ok(true, 'deauthentication occurred'); return new Response(200); }); - await authenticator.invalidate(); + await authenticator.invalidate({ token: 'token123' }); }); test('invalidation succeeds even if the deauthentication request fails', async function (assert) { assert.expect(2); const authenticator = this.owner.lookup('authenticator:base'); - this.server.post(authenticator.buildDeauthEndpointURL(), () => { + const endpoint = authenticator.buildDeauthEndpointURL({ id: 'token123' }); + this.server.delete(endpoint, () => { assert.ok(true, 'deauthentication was requested'); return new Response(500); }); await authenticator - .invalidate() + .invalidate({ token: 'token123' }) .then(() => assert.ok(true)) .catch(() => assert.notOk(true, 'uh oh, this should not happen')); }); diff --git a/addons/core/addon/authenticators/password.js b/addons/core/addon/authenticators/password.js index a2dfc8131..87eb38fee 100644 --- a/addons/core/addon/authenticators/password.js +++ b/addons/core/addon/authenticators/password.js @@ -37,10 +37,9 @@ export default class PasswordAuthenticator extends BasePasswordAuthenticator { * @param {string} scopeID * @return {string} */ - buildDeauthEndpointURL({ scope: { id: scopeID } }) { + buildDeauthEndpointURL({ id }) { const adapter = this.store.adapterFor('application'); - const options = { adapterOptions: { method: 'deauthenticate' } }; - return adapter.buildURL('scope', scopeID, options, 'findRecord'); + return adapter.buildURL('auth-token', id, {}, 'findRecord'); } /**