Skip to content

Commit

Permalink
Adopt ember-simple-auth auth recommendations
Browse files Browse the repository at this point in the history
Authentication header are now passed as query-params instead of xhr
as suggested in the following documentations
-> mainmatter/ember-simple-auth#1994

It also affects the load of the current user as suggested here:
-> https://github.com/simplabs/ember-simple-auth/blob/master/guides/managing-current-user.md#loading-the-current-user
  • Loading branch information
jonasgrilleres authored and sbedeau committed Feb 4, 2020
1 parent ce7e342 commit db1a357
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
13 changes: 7 additions & 6 deletions mon-pix/app/adapters/application.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import { isPresent } from '@ember/utils';
import { computed } from '@ember/object';
import ENV from 'mon-pix/config/environment';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
host: ENV.APP.API_HOST,
namespace: 'api',

authorize(xhr) {
const { access_token } = this.get('session.data.authenticated');
if (isPresent(access_token)) {
xhr.setRequestHeader('Authorization', `Bearer ${access_token}`);
headers: computed('session.data.authenticated.access_token', function() {
const headers = {};
if (this.session.isAuthenticated) {
headers['Authorization'] = `Bearer ${this.session.data.authenticated.access_token}`;
}
}
return headers;
})
});
17 changes: 10 additions & 7 deletions mon-pix/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { inject as service } from '@ember/service';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

import Route from '@ember/routing/route';

import { inject as service } from '@ember/service';

export default Route.extend(ApplicationRouteMixin, {

splash: service(),
Expand All @@ -26,17 +28,18 @@ export default Route.extend(ApplicationRouteMixin, {
},

async sessionAuthenticated() {
const _super = this._super;
await this._loadCurrentUser();

// Because ember-simple-auth does not support calling this._super() asynchronously,
// we have to do this hack to call the original "sessionAuthenticated"
ApplicationRouteMixin.mixins[0].properties.sessionAuthenticated.call(this);
_super.call(this, ...arguments);
},

// XXX: For override the sessionInvalidated from ApplicationRouteMixin to avoid the automatic redirection
sessionInvalidated() {},
sessionInvalidated() {
this.transitionTo('login');
},

_loadCurrentUser() {
return this.currentUser.load();
return this.get('currentUser').load().catch(() => this.get('session').invalidate());
}

});
3 changes: 1 addition & 2 deletions mon-pix/app/routes/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export default Route.extend({
}

if (this.hasUnauthorizedError(error)) {
return this.session.invalidate()
.then(() => this.transitionTo('login'));
return this.session.invalidate();
}
}

Expand Down
2 changes: 1 addition & 1 deletion mon-pix/app/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default Route.extend(UnauthenticatedRouteMixin, {
session: service(),

actions: {
authenticate(login, password) {
async authenticate(login, password) {
const scope = 'mon-pix';
const trimedLogin = login ? login.trim() : '';
return this.session.authenticate('authenticator:oauth2', { login: trimedLogin, password, scope });
Expand Down
23 changes: 6 additions & 17 deletions mon-pix/tests/unit/adapters/application-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'chai';
import { it, describe } from 'mocha';
import { setupTest } from 'ember-mocha';
import sinon from 'sinon';

describe('Unit | Route | subscribers', function() {
setupTest();
Expand All @@ -14,36 +13,26 @@ describe('Unit | Route | subscribers', function() {
expect(applicationAdapter.namespace).to.equal('api');
});

it('should add header with authentication token ', function() {
it('should add header with authentication token when the session is authenticated', function() {
// Given
const xhr = {
setRequestHeader: sinon.stub()
};
const access_token = '23456789';
const applicationAdapter = this.owner.lookup('adapter:application');

// When
applicationAdapter.set('session', { data: { authenticated: { access_token } } });
applicationAdapter.authorize(xhr);
applicationAdapter.set('session', { isAuthenticated: true, data: { authenticated: { access_token } } });

// Then
sinon.assert.calledWith(xhr.setRequestHeader, 'Authorization', `Bearer ${access_token}`);
expect(applicationAdapter.headers['Authorization']).to.equal(`Bearer ${access_token}`);
});

it('should not set Authorization header without token ', function() {
it('should not add header authentication token when the session is not authenticated', function() {
// Given
const xhr = {
setRequestHeader: sinon.stub()
};
const access_token = '';
const applicationAdapter = this.owner.lookup('adapter:application');

// When
applicationAdapter.set('session', { data: { authenticated: { access_token } } });
applicationAdapter.authorize(xhr);
applicationAdapter.set('session', {});

// Then
sinon.assert.notCalled (xhr.setRequestHeader);
expect(applicationAdapter.headers['Authorization']).to.be.undefined;
});

});

0 comments on commit db1a357

Please sign in to comment.