From f056db814f20e307ab71a4a391fb32ba44b457f7 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 27 Jul 2015 10:18:10 -0700 Subject: [PATCH 1/2] Use a custom authenticator for shibboleth The ember-cli-simple-auth-token author declined to include this functionality in the upstream library so I have added a custom authenticator for ilios which handles our case. --- app/authenticators/ilios-jwt.js | 53 +++++++++++++++++++++++++++++++++ app/controllers/login.js | 2 +- app/routes/login.js | 2 +- package.json | 2 +- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 app/authenticators/ilios-jwt.js diff --git a/app/authenticators/ilios-jwt.js b/app/authenticators/ilios-jwt.js new file mode 100644 index 0000000000..71da098296 --- /dev/null +++ b/app/authenticators/ilios-jwt.js @@ -0,0 +1,53 @@ +import Ember from 'ember'; +import JwtTokenAuthenticator from 'simple-auth-token/authenticators/jwt'; + +export default JwtTokenAuthenticator.extend({ + /** + Extend the JwtTokenAuthenticator to accept a token in liu of credentials + This allows authentication of an already existing session. + @method authenticate + @param {Object} options The credentials to authenticate the session with + @return {Ember.RSVP.Promise} A promise that resolves when an auth token is + successfully acquired from the server and rejects + otherwise + */ + authenticate: function(credentials) { + return new Ember.RSVP.Promise((resolve, reject) => { + if(this.tokenPropertyName in credentials){ + let token = credentials[this.tokenPropertyName]; + let tokenData = this.getTokenData(token); + let expiresAt = tokenData[this.tokenExpireName]; + let response = {}; + response[this.tokenPropertyName] = token; + response[this.tokenExpireName] = expiresAt; + this.scheduleAccessTokenRefresh(expiresAt, token); + + resolve(this.getResponseData(response)); + } else { + var data = this.getAuthenticateData(credentials); + + this.makeRequest(this.serverTokenEndpoint, data).then(response => { + var self = this; + Ember.run(function() { + var token = response[self.tokenPropertyName], + tokenData = self.getTokenData(token), + expiresAt = tokenData[self.tokenExpireName], + tokenExpireData = {}; + + self.scheduleAccessTokenRefresh(expiresAt, token); + + tokenExpireData[self.tokenExpireName] = expiresAt; + + response = Ember.merge(response, tokenExpireData); + + resolve(self.getResponseData(response)); + }); + }, function(xhr) { + Ember.run(function() { + reject(xhr.responseJSON || xhr.responseText); + }); + }); + } + }); + } +}); diff --git a/app/controllers/login.js b/app/controllers/login.js index 90a48ed458..42e9de49bb 100644 --- a/app/controllers/login.js +++ b/app/controllers/login.js @@ -6,7 +6,7 @@ export default Ember.Controller.extend({ actions: { authenticate: function() { let credentials = this.getProperties('identification', 'password'); - let authenticator = 'simple-auth-authenticator:jwt'; + let authenticator = 'authenticator:ilios-jwt'; this.set('errors', []); this.get('session').authenticate(authenticator, credentials).then(() => { let jwt = this.get('session').get('secure.jwt'); diff --git a/app/routes/login.js b/app/routes/login.js index 79e2ebd9cd..07626a90e0 100644 --- a/app/routes/login.js +++ b/app/routes/login.js @@ -22,7 +22,7 @@ export default Ember.Route.extend(UnauthenticatedRouteMixin, { } window.location.replace(shibbolethLoginUrl); } else { - let authenticator = 'simple-auth-authenticator:jwt'; + let authenticator = 'authenticator:ilios-jwt'; this.get('session').authenticate(authenticator, {jwt: token.jwt}).then(() => { let jwt = this.get('session').get('secure.jwt'); diff --git a/package.json b/package.json index 55ef6f50e7..6f44ed6915 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "ember-cli-sauce": "^1.0.0", "ember-cli-simple-auth": "^0.8.0", "ember-cli-simple-auth-testing": "0.8.0", - "ember-cli-simple-auth-token": "jrjohnson/ember-cli-simple-auth-token#authbyjwt", + "ember-cli-simple-auth-token": "^0.7.3", "ember-cli-tooltipster": "0.0.9", "ember-cli-uglify": "^1.0.1", "ember-data": "1.13.3", From 17e2fde6b525c567fa5e08651e1cb43cde302041 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Tue, 28 Jul 2015 22:18:17 -0700 Subject: [PATCH 2/2] Fix click month date test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This test would break at the end of the month. It now focuses on the 13th of each month so it shouldn’t be as fragile. --- tests/acceptance/dashboard/calendar-test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/acceptance/dashboard/calendar-test.js b/tests/acceptance/dashboard/calendar-test.js index 01bd3253d4..76d9e87a4d 100644 --- a/tests/acceptance/dashboard/calendar-test.js +++ b/tests/acceptance/dashboard/calendar-test.js @@ -107,20 +107,20 @@ test('load day calendar', function(assert) { }); test('click month day number and go to day', function(assert) { - let today = moment().hour(8); + let aDayInTheMonth = moment().startOf('month').add(12, 'days').hour(8); server.create('userevent', { - name: 'today', - startDate: today.format(), - endDate: today.clone().add(1, 'hour').format() + name: 'start of month', + startDate: aDayInTheMonth.format(), + endDate: aDayInTheMonth.clone().add(1, 'hour').format() }); visit('/dashboard?view=month'); andThen(function() { - let dayOfMonth = today.date(); + let dayOfMonth = aDayInTheMonth.date(); let link = find('.day a').filter(function(){ return parseInt($(this).text()) === dayOfMonth; }).eq(0); click(link).then(()=>{ - assert.equal(currentURL(), '/dashboard?date=' + today.format('YYYY-MM-DD') + '&view=day'); + assert.equal(currentURL(), '/dashboard?date=' + aDayInTheMonth.format('YYYY-MM-DD') + '&view=day'); }); }); });