refactor: auth/deauth URLs should be built via a method call rather than statically

This commit is contained in:
Randall Morey committed 2020-07-24 14:37:50 -04:00
1 parent c5280ca27c
commit 69b08ffcb0
3 files changed
+28 -18

No files matched your search

+13 -10
View File
@@ -7,16 +7,16 @@ import fetch from 'fetch';
* This authenticator is intended for use with Ember Simple Auth.
*
* Simple id/password base authenticator sends credentials to an endpoint
* specified in the `authEndpoint` URL. If the HTTP response code is in the
* specified by the `buildAuthEndpointURL`. If the HTTP response code is in the
* success range, authentication resolves. Otherwise it rejects.
*
* Upon session invalidation, deauthentication is attempted at the
* `deauthEndpoint`, but is not guaranteed.
* Upon session invalidation, deauthentication is attempted at the URL generated
* by `buildDeauthEndpointURL`, but is not guaranteed.
*
* This authenticator should not be used directly because it does not specify
* an `authEndpoint` or `deauthEndpoint` of its own. To use, generate an
* application authenticator in your app `authenticators/application.js` and
* extend this class.
* an `buildAuthEndpointURL` or `buildDeauthEndpointURL` of its own.
* To use, generate an application authenticator in your app
* `authenticators/application.js` and extend this class.
*
* @example
*
@@ -56,13 +56,15 @@ export default class PasswordAuthenticator extends BaseAuthenticator {
*/
async authenticate(
{ identification: name, password },
requestCookies = true
requestCookies = true,
options
) {
const body = JSON.stringify({
token_type: requestCookies ? 'cookie' : null,
credentials: { name, password },
});
const response = await fetch(this.authEndpoint, { method: 'post', body });
const authEndpointURL = this.buildAuthEndpointURL(options);
const response = await fetch(authEndpointURL, { method: 'post', body });
const json = await response.json();
return response.status < 400 ? resolve(json) : reject();
}
@@ -75,8 +77,9 @@ export default class PasswordAuthenticator extends BaseAuthenticator {
* @override
* @return {Promise}
*/
invalidate() {
fetch(this.deauthEndpoint, { method: 'post' }).catch(() => {
invalidate(options) {
const deauthEndpointURL = this.buildDeauthEndpointURL(options);
fetch(deauthEndpointURL, { method: 'post' }).catch(() => {
/* no op */
});
return super.invalidate(...arguments);
@@ -1,6 +1,13 @@
import PasswordAuthenticator from 'auth/authenticators/password';
export default class ApplicationPasswordAuthenticator extends PasswordAuthenticator {
authEndpoint = '/authenticate';
deauthEndpoint = '/deauthenticate';
buildAuthEndpointURL() {
return '/authenticate';
}
buildDeauthEndpointURL() {
return '/deauthenticate';
}
}
@@ -10,7 +10,7 @@ module('Unit | Authenticator | password', function (hooks) {
test('it authenticates to the specified authEndpoint', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.authEndpoint, (schema, request) => {
this.server.post(authenticator.buildAuthEndpointURL(), (schema, request) => {
const json = JSON.parse(request.requestBody);
assert.ok(json.token_type, 'Requested token cookies by default');
return new Response(200);
@@ -23,7 +23,7 @@ module('Unit | Authenticator | password', function (hooks) {
test('it can authenticate without requesting cookies', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.authEndpoint, (schema, request) => {
this.server.post(authenticator.buildAuthEndpointURL(), (schema, request) => {
const json = JSON.parse(request.requestBody);
assert.notOk(json.token_type, 'Did not request tokens cookies');
return new Response(200);
@@ -36,7 +36,7 @@ module('Unit | Authenticator | password', function (hooks) {
test('it authenticates with the expected payload', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.authEndpoint, (schema, request) => {
this.server.post(authenticator.buildAuthEndpointURL(), (schema, request) => {
const json = JSON.parse(request.requestBody);
assert.deepEqual(json, {
token_type: 'cookie',
@@ -57,7 +57,7 @@ module('Unit | Authenticator | password', function (hooks) {
test('it rejects if the endpoint sends an error status code', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.authEndpoint, () => {
this.server.post(authenticator.buildAuthEndpointURL(), () => {
return new Response(400);
});
await authenticator.authenticate({}).catch(() => {
@@ -68,7 +68,7 @@ module('Unit | Authenticator | password', function (hooks) {
test('it deauthenticates on invalidation', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.deauthEndpoint, () => {
this.server.post(authenticator.buildDeauthEndpointURL(), () => {
assert.ok(true, 'deauthentication occurred');
return new Response(200);
});
@@ -78,7 +78,7 @@ module('Unit | Authenticator | password', function (hooks) {
test('it invalidation succeeds even if the deauthentication request fails', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.deauthEndpoint, () => {
this.server.post(authenticator.buildDeauthEndpointURL(), () => {
assert.ok(true, 'deauthentication was requested');
return new Response(500);
});