Merge branch 'develop' into feature-staged-edit

This commit is contained in:
Randall Morey authored and GitHub committed 2020-09-24 09:16:52 -04:00
commit d5f69d83f0
10 files changed
+118 -6

No files matched your search

@@ -0,0 +1,8 @@
import BaseModel from '../../models/base';
/**
* NOTE: we don't yet use this model for display in the UI and thus no
* attributes are included at this time. This model is used only for
* validating that an auth token exists.
*/
export default class GeneratedAuthTokenModel extends BaseModel {}
+3
View File
@@ -0,0 +1,3 @@
import GeneratedAuthTokenModel from '../generated/models/auth-token';
export default class AuthTokenModel extends GeneratedAuthTokenModel {}
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/models/auth-token';
@@ -0,0 +1,13 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Model | auth token', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('auth-token', {});
assert.ok(model);
});
});
+22 -4
View File
@@ -13,8 +13,14 @@ import fetch from 'fetch';
* Upon session invalidation, deauthentication is attempted at the URL generated
* by `buildDeauthEndpointURL`, but is not guaranteed.
*
* When a session is restored, a call is made to the endpoint specified by
* `buildTokenValidationEndpointURL`. If this endpoint responds with 401 or
* 404, then the session is invalid and the restoration is rejected. All other
* responses will resolve the session restoration successfully.
*
* This authenticator should not be used directly because it does not specify
* an `buildAuthEndpointURL` or `buildDeauthEndpointURL` of its own.
* an `buildAuthEndpointURL`, `buildDeauthEndpointURL`, or
* `buildTokenValidationEndpointURL` of its own.
* To use, generate an application authenticator in your app
* `authenticators/application.js` and extend this class.
*
@@ -31,13 +37,25 @@ export default class PasswordAuthenticator extends BaseAuthenticator {
// =methods
/**
* Restores the session if data is present. Otherwise rejects.
* Restores the session if data is present and token validation succeeds
* (any response other than 401 or 404 === success). Otherwise rejects.
* @override
* @param {object} data
* @return {Promise}
*/
restore(data) {
return data ? resolve(data) : reject();
async restore(data) {
if (!data) return reject();
const tokenID = data.id;
const tokenValidationURL = this.buildTokenValidationEndpointURL(tokenID);
const response = await fetch(tokenValidationURL, { method: 'get' });
// 401 and 404 responses mean the token is invalid, whereas other types of
// error responses do not tell us about the validity of the token.
switch (response.status) {
case 401:
case 404:
return reject();
}
return resolve(data);
}
/**
@@ -1,6 +1,10 @@
import PasswordAuthenticator from 'auth/authenticators/password';
export default class ApplicationPasswordAuthenticator extends PasswordAuthenticator {
buildTokenValidationEndpointURL(id) {
return `/validate/${id}`;
}
buildAuthEndpointURL() {
return '/authenticate';
}
@@ -96,4 +96,45 @@ module('Unit | Authenticator | password', function (hooks) {
.then(() => assert.ok(true))
.catch(() => assert.notOk(true, 'uh oh, this should not happen'));
});
test('it passes token validation on restoration if receiving 200', async function (assert) {
assert.expect(1);
const mockData = { id: 'token_123' };
const authenticator = this.owner.lookup('authenticator:password');
this.server.get(authenticator.buildTokenValidationEndpointURL(mockData.id), () => {
assert.ok(true, 'token validation was requested');
return new Response(200);
});
await authenticator.restore(mockData);
});
test('it fails token validation on restoration if receiving 401', async function (assert) {
assert.expect(2);
const mockData = { id: 'token_123' };
const authenticator = this.owner.lookup('authenticator:password');
this.server.get(authenticator.buildTokenValidationEndpointURL(mockData.id), () => {
assert.ok(true, 'token validation was requested');
return new Response(401);
});
try {
await authenticator.restore(mockData);
} catch (e) {
assert.ok(true, 'restoration failed');
}
});
test('it fails token validation on restoration if receiving 404', async function (assert) {
assert.expect(2);
const mockData = { id: 'token_123' };
const authenticator = this.owner.lookup('authenticator:password');
this.server.get(authenticator.buildTokenValidationEndpointURL(mockData.id), () => {
assert.ok(true, 'token validation was requested');
return new Response(404);
});
try {
await authenticator.restore(mockData);
} catch (e) {
assert.ok(true, 'restoration failed');
}
});
});
+11
View File
@@ -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`.
+9 -1
View File
@@ -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));
@@ -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');
});