refactor: 💡 deauth should delete the auth token (#1256)
This commit is contained in:
1 parent
e3324ad64a
commit
7386b2c9ed
4 files changed
+15
-10
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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'));
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user