diff --git a/ui/core/app/authenticators/password.js b/ui/core/app/authenticators/password.js index 2d64dfc47..a0ceccdd9 100644 --- a/ui/core/app/authenticators/password.js +++ b/ui/core/app/authenticators/password.js @@ -43,6 +43,17 @@ export default class PasswordAuthenticator extends BasePasswordAuthenticator { return adapter.buildURL('scope', scopeID, options, 'findRecord'); } + /** + * Generates an auth token validation URL used to check tokens on restoration. + * @override + * @param {string} tokenID + * @return {string} + */ + buildTokenValidationEndpointURL(tokenID) { + const adapter = this.store.adapterFor('auth-token'); + return adapter.buildURL('auth-token', tokenID, {}, 'findRecord'); + } + /** * Intercepts the authenticate response, if any, and assigns the returned * token to all future requests via `addTokenToAuthorization`. diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 59cbf0180..3d332f121 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -1,7 +1,7 @@ import config from '../config/environment'; import { authHandler, deauthHandler } from './route-handlers/auth'; import { pickRandomStatusString } from './factories/session'; -//import { Response } from 'miragejs'; +import { Response } from 'miragejs'; export default function() { @@ -326,6 +326,14 @@ export default function() { return session.update(updatedAttrs); }); + // auth-tokens + + this.get('/auth-tokens/:id', function () { + // Set to 401 or 404 to simulate token invalidation, which will + // cause the session to fail restoration and logout the user. + return new Response(200); + }); + /* Uncomment the following line and the Response import above * Then change the response code to simulate error responses. * this.get('/scopes', () => new Response(505)); diff --git a/ui/core/tests/unit/authenticators/password-test.js b/ui/core/tests/unit/authenticators/password-test.js index 79996c8c0..0314aed17 100644 --- a/ui/core/tests/unit/authenticators/password-test.js +++ b/ui/core/tests/unit/authenticators/password-test.js @@ -1,6 +1,7 @@ import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; +import { Response } from 'miragejs'; module('Unit | Authenticator | password', function (hooks) { setupTest(hooks); @@ -25,7 +26,11 @@ module('Unit | Authenticator | password', function (hooks) { test('it adds an authorization header to application adapter on restore', async function (assert) { assert.expect(1); - authenticator.restore({ token: 'token1234' }); + const mockData = { id: 'token1234', token: 'token1234' }; + this.server.get(authenticator.buildTokenValidationEndpointURL(mockData.id), () => { + return new Response(200); + }); + authenticator.restore(mockData); assert.equal(applicationAdapter.headers.Authorization, 'Bearer token1234'); });