From f9011d4120bc33c52812fa04797746865bd4d62f Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 14 Jul 2014 14:41:55 +0200 Subject: [PATCH 01/41] added Authenticators.Test --- .../lib/simple-auth/authenticators/test.js | 51 +++++++++++++++++++ .../simple-auth/authenticators/test-test.js | 34 +++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 packages/ember-simple-auth/lib/simple-auth/authenticators/test.js create mode 100644 packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js diff --git a/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js b/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js new file mode 100644 index 000000000..50bb29bf5 --- /dev/null +++ b/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js @@ -0,0 +1,51 @@ +import Base from './base'; + +/** + Authenticator that is used by the test helpers. This authenticator simply + always returns a resolving promise. + + _The factory for this authenticator is registered as + `'simple-auth-authenticator:test'` in Ember's container when `Ember.testing` + is `true`._ + + @class Test + @namespace SimpleAuth.Authenticators + @module simple-auth/authenticators/base + @extends Base + @uses Ember.Evented +*/ +export default Base.extend({ + /** + Restores the session from a set of session properties; __will always return + a resolving promise__. + + @method restore + @param {Object} data The data to restore the session from + @return {Ember.RSVP.Promise} A resolving promise + */ + restore: function(data) { + return new Ember.RSVP.resolve(); + }, + + /** + Authenticates the session; __will always return a resolving promise__. + + @method authenticate + @param {Object} options The options to authenticate the session with + @return {Ember.RSVP.Promise} A resolving promise + */ + authenticate: function(options) { + return new Ember.RSVP.resolve(); + }, + + /** + Invalidates the session; __will always return a resolving promise__. + + @method invalidate + @param {Object} data The data that the session currently holds + @return {Ember.RSVP.Promise} A resolving promise + */ + invalidate: function(data) { + return new Ember.RSVP.resolve(); + } +}); diff --git a/packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js b/packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js new file mode 100644 index 000000000..a17f011c4 --- /dev/null +++ b/packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js @@ -0,0 +1,34 @@ +import Test from 'simple-auth/authenticators/test'; + +describe('Authenticators.Test', function() { + beforeEach(function() { + this.authenticator = Test.create(); + }); + + describe('#restore', function() { + it('returns a resolving promise', function(done) { + this.authenticator.restore().then(function() { + expect(true).to.be.true; + done(); + }); + }); + }); + + describe('#authenticate', function() { + it('returns a resolving promise', function(done) { + this.authenticator.authenticate().then(function() { + expect(true).to.be.true; + done(); + }); + }); + }); + + describe('#invalidate', function() { + it('returns a resolving promise', function(done) { + this.authenticator.invalidate().then(function() { + expect(true).to.be.true; + done(); + }); + }); + }); +}); From b08a80c030d643b0462bea3de4c10207a9e5c60a Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 14 Jul 2014 14:44:59 +0200 Subject: [PATCH 02/41] register test authenticator when Ember.testing is true --- .../lib/simple-auth/authenticators/test.js | 36 ------------------- .../lib/simple-auth/initializer.js | 4 +++ 2 files changed, 4 insertions(+), 36 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js b/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js index 50bb29bf5..884683290 100644 --- a/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js +++ b/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js @@ -1,50 +1,14 @@ import Base from './base'; -/** - Authenticator that is used by the test helpers. This authenticator simply - always returns a resolving promise. - - _The factory for this authenticator is registered as - `'simple-auth-authenticator:test'` in Ember's container when `Ember.testing` - is `true`._ - - @class Test - @namespace SimpleAuth.Authenticators - @module simple-auth/authenticators/base - @extends Base - @uses Ember.Evented -*/ export default Base.extend({ - /** - Restores the session from a set of session properties; __will always return - a resolving promise__. - - @method restore - @param {Object} data The data to restore the session from - @return {Ember.RSVP.Promise} A resolving promise - */ restore: function(data) { return new Ember.RSVP.resolve(); }, - /** - Authenticates the session; __will always return a resolving promise__. - - @method authenticate - @param {Object} options The options to authenticate the session with - @return {Ember.RSVP.Promise} A resolving promise - */ authenticate: function(options) { return new Ember.RSVP.resolve(); }, - /** - Invalidates the session; __will always return a resolving promise__. - - @method invalidate - @param {Object} data The data that the session currently holds - @return {Ember.RSVP.Promise} A resolving promise - */ invalidate: function(data) { return new Ember.RSVP.resolve(); } diff --git a/packages/ember-simple-auth/lib/simple-auth/initializer.js b/packages/ember-simple-auth/lib/simple-auth/initializer.js index cbdf2d2d7..74f7eec94 100644 --- a/packages/ember-simple-auth/lib/simple-auth/initializer.js +++ b/packages/ember-simple-auth/lib/simple-auth/initializer.js @@ -1,11 +1,15 @@ var global = (typeof window !== 'undefined') ? window : {}, Ember = global.Ember; +import TestAuthenticator from 'simple-auth/authenticators/test'; import setup from './setup'; export default { name: 'simple-auth', initialize: function(container, application) { setup(container, application); + if (Ember.testing) { + container.register('simple-auth-authenticator:test', TestAuthenticator); + } } }; From aef51255ff65b02eaf3d29fd35f248a72dcd4f35 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 14 Jul 2014 14:51:33 +0200 Subject: [PATCH 03/41] added test helpers --- .../lib/simple-auth/test-helpers/authenticate-session.js | 5 +++++ .../lib/simple-auth/test-helpers/invalidate-session.js | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js create mode 100644 packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js new file mode 100644 index 000000000..c3c6b6b79 --- /dev/null +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js @@ -0,0 +1,5 @@ +export default Ember.Test.registerAsyncHelper('authenticateSession', function(app) { + var session = app.__container__.lookup('simple-auth-session:main'); + session.authenticate('simple-auth-authenticator:test'); + return wait(); +}); diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js new file mode 100644 index 000000000..c32b3e11a --- /dev/null +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js @@ -0,0 +1,7 @@ +export default Ember.Test.registerAsyncHelper('invalidateSession', function(app) { + var session = app.__container__.lookup('simple-auth-session:main'); + if (session.get('isAuthenticated')) { + session.invalidate(); + } + return wait(); +}); From 83d52ae1de1cd85502cf8e5e3f4e0f9493e7befa Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 14 Jul 2014 15:48:44 +0200 Subject: [PATCH 04/41] require test helper in test mode in browserified build --- packages/ember-simple-auth/wrap/browser.end | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/ember-simple-auth/wrap/browser.end b/packages/ember-simple-auth/wrap/browser.end index 46bbe7259..b6049f445 100644 --- a/packages/ember-simple-auth/wrap/browser.end +++ b/packages/ember-simple-auth/wrap/browser.end @@ -46,4 +46,9 @@ global.SimpleAuth = { }; requireModule('simple-auth/ember'); + +if (Ember.testing) { + requireModule('simple-auth/test-helpers/authenticate-session'); + requireModule('simple-auth/test-helpers/invalidate-session'); +} })((typeof global !== 'undefined') ? global : window); From 91ba48b744546c3c0cfe114a5c4f7c49d2273a9a Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Wed, 16 Jul 2014 12:08:05 +0200 Subject: [PATCH 05/41] added assertion for clearer error, close #250 --- packages/ember-simple-auth/lib/simple-auth/session.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ember-simple-auth/lib/simple-auth/session.js b/packages/ember-simple-auth/lib/simple-auth/session.js index 5a2b9df66..2e6c655bd 100644 --- a/packages/ember-simple-auth/lib/simple-auth/session.js +++ b/packages/ember-simple-auth/lib/simple-auth/session.js @@ -190,6 +190,7 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { @return {Ember.RSVP.Promise} A promise that resolves when the session was invalidated successfully */ invalidate: function() { + Ember.assert('Session#invalidate requires the session to be authenticated', this.get('isAuthenticated')); var _this = this; return new Ember.RSVP.Promise(function(resolve, reject) { var authenticator = _this.container.lookup(_this.authenticator); From d8c338898d93346d85a4e951f62e87e71b0bd693 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Wed, 16 Jul 2014 14:15:49 +0200 Subject: [PATCH 06/41] don't depend on Session#init --- packages/ember-simple-auth/lib/simple-auth/session.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/session.js b/packages/ember-simple-auth/lib/simple-auth/session.js index 2e6c655bd..e500120a5 100644 --- a/packages/ember-simple-auth/lib/simple-auth/session.js +++ b/packages/ember-simple-auth/lib/simple-auth/session.js @@ -131,14 +131,6 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { */ content: {}, - /** - @method init - @private - */ - init: function() { - this.bindToStoreEvents(); - }, - /** Authenticates the session with an `authenticator` and appropriate `options`. __This delegates the actual authentication work to the @@ -306,5 +298,5 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { _this.clear(true); } }); - } + }.observes('store').on('init') }); From f4b73a8eef3791eabb64af0cdfdbd148597a1fde Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Wed, 16 Jul 2014 21:52:34 +0200 Subject: [PATCH 07/41] closes #251 --- examples/4-authenticated-account.html | 26 +++++++----- .../authorizers/devise-test.js | 8 ++-- .../authorizers/oauth2-test.js | 4 +- .../lib/simple-auth/configuration.js | 14 +++++++ .../lib/simple-auth/session.js | 11 ++++- .../lib/simple-auth/setup.js | 19 +++++---- .../tests/simple-auth/configuration-test.js | 6 +++ .../mixins/application-route-mixin-test.js | 5 ++- .../mixins/authenticated-route-mixin-test.js | 3 +- .../authentication-controller-mixin-test.js | 3 +- .../mixins/login-controller-mixin-test.js | 3 +- .../tests/simple-auth/session-test.js | 12 ++++-- .../tests/simple-auth/setup-test.js | 42 +++++++++++++------ 13 files changed, 110 insertions(+), 46 deletions(-) diff --git a/examples/4-authenticated-account.html b/examples/4-authenticated-account.html index 84ebed440..5c1146d0e 100644 --- a/examples/4-authenticated-account.html +++ b/examples/4-authenticated-account.html @@ -96,21 +96,15 @@

Protected Page

// configure an authorizer to be used window.ENV = window.ENV || {}; window.ENV['simple-auth'] = { + session: 'session:custom', authorizer: 'simple-auth-authorizer:oauth2-bearer' }; Ember.Application.initializer({ - name: 'authentication', - before: 'simple-auth', + name: 'authentication', + before: 'simple-auth', initialize: function(container, application) { - // customize the session so that it allows access to the account object - SimpleAuth.Session.reopen({ - account: function() { - var accountId = this.get('account_id'); - if (!Ember.isEmpty(accountId)) { - return container.lookup('store:main').find('account', accountId); - } - }.property('account_id') - }); + // register the custom session so Ember Simple Auth can find it + container.register('session:custom', App.CustomSession); // register the custom authenticator so the session can find it container.register('authenticator:custom', App.CustomAuthenticator); } @@ -134,6 +128,16 @@

Protected Page

this.route('protected'); }); + // the custom session that handles an authenticated account + App.CustomSession = SimpleAuth.Session.extend({ + account: function() { + var accountId = this.get('account_id'); + if (!Ember.isEmpty(accountId)) { + return this.container.lookup('store:main').find('account', accountId); + } + }.property('account_id') + }); + // the custom authenticator that handles the authenticated account App.CustomAuthenticator = SimpleAuth.Authenticators.OAuth2.extend({ authenticate: function(credentials) { diff --git a/packages/ember-simple-auth-devise/tests/simple-auth-devise/authorizers/devise-test.js b/packages/ember-simple-auth-devise/tests/simple-auth-devise/authorizers/devise-test.js index d8869791b..be8528e65 100644 --- a/packages/ember-simple-auth-devise/tests/simple-auth-devise/authorizers/devise-test.js +++ b/packages/ember-simple-auth-devise/tests/simple-auth-devise/authorizers/devise-test.js @@ -4,9 +4,11 @@ import EphemeralStore from 'simple-auth/stores/ephemeral'; describe('Devise', function() { beforeEach(function() { - this.authorizer = Devise.create(); - this.request = { setRequestHeader: function() {} }; - this.authorizer.set('session', Session.create({ store: EphemeralStore.create() })); + this.authorizer = Devise.create(); + this.request = { setRequestHeader: function() {} }; + var session = Session.create(); + session.setProperties({ store: EphemeralStore.create() }); + this.authorizer.set('session', session); sinon.spy(this.request, 'setRequestHeader'); }); diff --git a/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authorizers/oauth2-test.js b/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authorizers/oauth2-test.js index 8df91ed6d..ff6104ae7 100644 --- a/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authorizers/oauth2-test.js +++ b/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authorizers/oauth2-test.js @@ -6,7 +6,9 @@ describe('OAuth2', function() { beforeEach(function() { this.authorizer = OAuth2.create(); this.request = { setRequestHeader: function() {} }; - this.authorizer.set('session', Session.create({ store: EphemeralStore.create() })); + var session = Session.create(); + session.setProperties({ store: EphemeralStore.create() }); + this.authorizer.set('session', session); sinon.spy(this.request, 'setRequestHeader'); }); diff --git a/packages/ember-simple-auth/lib/simple-auth/configuration.js b/packages/ember-simple-auth/lib/simple-auth/configuration.js index 87b969ec5..cbf992ace 100644 --- a/packages/ember-simple-auth/lib/simple-auth/configuration.js +++ b/packages/ember-simple-auth/lib/simple-auth/configuration.js @@ -67,6 +67,19 @@ export default { */ authorizer: null, + /** + The session factory to use as it is registered with Ember's container, + see + [Ember's API docs](http://emberjs.com/api/classes/Ember.Application.html#method_register). + + @property session + @readOnly + @static + @type String + @default 'simple-auth-session:main' + */ + session: 'simple-auth-session:main', + /** The store factory to use as it is registered with Ember's container, see [Ember's API docs](http://emberjs.com/api/classes/Ember.Application.html#method_register). @@ -110,6 +123,7 @@ export default { this.routeAfterAuthentication = globalConfig.routeAfterAuthentication || this.routeAfterAuthentication; this.sessionPropertyName = globalConfig.sessionPropertyName || this.sessionPropertyName; this.authorizer = globalConfig.authorizer || this.authorizer; + this.session = globalConfig.session || this.session; this.store = globalConfig.store || this.store; this.crossOriginWhitelist = globalConfig.crossOriginWhitelist || this.crossOriginWhitelist; this.applicationRootUrl = container.lookup('router:main').get('rootURL') || '/'; diff --git a/packages/ember-simple-auth/lib/simple-auth/session.js b/packages/ember-simple-auth/lib/simple-auth/session.js index e500120a5..656c531e9 100644 --- a/packages/ember-simple-auth/lib/simple-auth/session.js +++ b/packages/ember-simple-auth/lib/simple-auth/session.js @@ -111,6 +111,15 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { @default null */ store: null, + /** + The Ember.js container, + + @property container + @type Container + @readOnly + @default null + */ + container: null, /** Returns whether the session is currently authenticated. @@ -298,5 +307,5 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { _this.clear(true); } }); - }.observes('store').on('init') + }.observes('store') }); diff --git a/packages/ember-simple-auth/lib/simple-auth/setup.js b/packages/ember-simple-auth/lib/simple-auth/setup.js index d7a88ba60..9ce1dba11 100644 --- a/packages/ember-simple-auth/lib/simple-auth/setup.js +++ b/packages/ember-simple-auth/lib/simple-auth/setup.js @@ -31,9 +31,10 @@ function shouldAuthorizeRequest(options) { return crossOriginWhitelist.indexOf(urlOrigin) > -1; } -function registerStores(container) { +function registerFactories(container) { container.register('simple-auth-session-store:local-storage', LocalStorage); container.register('simple-auth-session-store:ephemeral', Ephemeral); + container.register('simple-auth-session:main', Session); } /** @@ -43,17 +44,17 @@ function registerStores(container) { export default function(container, application) { Configuration.load(container); application.deferReadiness(); - registerStores(container); + registerFactories(container); - var store = container.lookup(Configuration.store); - var session = Session.create({ store: store, container: container }); - crossOriginWhitelist = Ember.A(Configuration.crossOriginWhitelist).map(function(origin) { - return extractLocationOrigin(origin); + var store = container.lookup(Configuration.store); + var session = container.lookup(Configuration.session); + session.setProperties({ store: store, container: container }); + Ember.A(['controller', 'route']).forEach(function(component) { + container.injection(component, Configuration.sessionPropertyName, Configuration.session); }); - container.register('simple-auth-session:main', session, { instantiate: false }); - Ember.A(['controller', 'route']).forEach(function(component) { - container.injection(component, Configuration.sessionPropertyName, 'simple-auth-session:main'); + crossOriginWhitelist = Ember.A(Configuration.crossOriginWhitelist).map(function(origin) { + return extractLocationOrigin(origin); }); if (!Ember.isEmpty(Configuration.authorizer)) { diff --git a/packages/ember-simple-auth/tests/simple-auth/configuration-test.js b/packages/ember-simple-auth/tests/simple-auth/configuration-test.js index f2a61e8de..1ca9bb4cb 100644 --- a/packages/ember-simple-auth/tests/simple-auth/configuration-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/configuration-test.js @@ -31,6 +31,12 @@ describe('Configuration', function() { }); }); + describe('session', function() { + it('defaults to "simple-auth-session:main"', function() { + expect(Configuration.session).to.eq('simple-auth-session:main'); + }); + }); + describe('store', function() { it('defaults to "simple-auth-session-store:local-storage"', function() { expect(Configuration.store).to.eq('simple-auth-session-store:local-storage'); diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js index 0d6dfd8b3..dcd09b4c0 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js @@ -7,8 +7,9 @@ var TestRoute = Ember.Route.extend(ApplicationRouteMixin); describe('ApplicationRouteMixin', function() { beforeEach(function() { - this.session = Session.create({ store: EphemeralStore.create() }); - this.route = Ember.Route.extend(ApplicationRouteMixin, { + this.session = Session.create(); + this.session.setProperties({ store: EphemeralStore.create() }); + this.route = Ember.Route.extend(ApplicationRouteMixin, { send: function() {}, transitionTo: function() {} }).create({ session: this.session }); diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/authenticated-route-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/authenticated-route-mixin-test.js index 0d5843f13..8b03f931e 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/authenticated-route-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/authenticated-route-mixin-test.js @@ -5,7 +5,8 @@ import EphemeralStore from 'simple-auth/stores/ephemeral'; describe('AuthenticatedRouteMixin', function() { describe('#beforeModel', function() { beforeEach(function() { - this.session = Session.create({ store: EphemeralStore.create() }); + this.session = Session.create(); + this.session.setProperties({ store: EphemeralStore.create() }); this.transition = { abort: function() {}, send: function() {} }; this.route = Ember.Route.extend(AuthenticatedRouteMixin).create({ session: this.session }); sinon.spy(this.transition, 'abort'); diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js index dcb87f644..28bb60372 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js @@ -5,7 +5,8 @@ import EphemeralStore from 'simple-auth/stores/ephemeral'; describe('AuthenticationControllerMixin', function() { describe('the "authenticate" action', function() { beforeEach(function() { - this.session = Session.create({ store: EphemeralStore.create() }); + this.session = Session.create(); + this.session.setProperties({ store: EphemeralStore.create() }); this.controller = Ember.Controller.extend(AuthenticationControllerMixin, { authenticator: 'authenticator' }).create({ session: this.session }); diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js index 808f77df9..816cbea0c 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js @@ -4,7 +4,8 @@ import EphemeralStore from 'simple-auth/stores/ephemeral'; describe('LoginControllerMixin', function() { beforeEach(function() { - this.session = Session.create({ store: EphemeralStore.create() }); + this.session = Session.create(); + this.session.setProperties({ store: EphemeralStore.create() }); this.controller = Ember.Controller.extend(LoginControllerMixin).create({ authenticator: 'authenticator', session: this.session diff --git a/packages/ember-simple-auth/tests/simple-auth/session-test.js b/packages/ember-simple-auth/tests/simple-auth/session-test.js index 13b9548f4..134211659 100644 --- a/packages/ember-simple-auth/tests/simple-auth/session-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/session-test.js @@ -73,7 +73,8 @@ describe('Session', function() { describe('restore', function() { beforeEach(function() { - this.session = Session.create({ store: this.store, container: this.container }); + this.session = Session.create(); + this.session.setProperties({ store: this.store, container: this.container }); }); function itDoesNotRestore() { @@ -210,7 +211,8 @@ describe('Session', function() { describe('authentication', function() { beforeEach(function() { - this.session = Session.create({ store: this.store, container: this.container }); + this.session = Session.create(); + this.session.setProperties({ store: this.store, container: this.container }); }); describe('when the authenticator resolves authentication', function() { @@ -362,7 +364,8 @@ describe('Session', function() { describe('invalidation', function() { beforeEach(function(done) { - this.session = Session.create({ store: this.store, container: this.container }); + this.session = Session.create(); + this.session.setProperties({ store: this.store, container: this.container }); sinon.stub(this.authenticator, 'authenticate').returns(Ember.RSVP.resolve({ some: 'property' })); this.session.authenticate('authenticator').then(function() { done(); @@ -504,7 +507,8 @@ describe('Session', function() { describe('when the store triggers the "updated" event', function() { beforeEach(function() { - this.session = Session.create({ store: this.store, container: this.container }); + this.session = Session.create(); + this.session.setProperties({ store: this.store, container: this.container }); }); describe('when there is an authenticator factory in the event data', function() { diff --git a/packages/ember-simple-auth/tests/simple-auth/setup-test.js b/packages/ember-simple-auth/tests/simple-auth/setup-test.js index 92eb80ada..c3adc1138 100644 --- a/packages/ember-simple-auth/tests/simple-auth/setup-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/setup-test.js @@ -9,10 +9,13 @@ describe('setup', function() { this.container = { register: function() {}, injection: function() {}, lookup: function() {} }; this.application = { deferReadiness: function() {}, advanceReadiness: function() {} }; this.router = { get: function() { return 'rootURL'; }, send: function() {} }; - this.store = LocalStorageStore.create(); + this.store = EphemeralStore.create(); + this.session = Session.create(); + this.session.setProperties({ store: this.store, container: this.container }); this.containerStub = sinon.stub(this.container, 'lookup'); this.containerStub.withArgs('router:main').returns(this.router); this.containerStub.withArgs('simple-auth-session-store:local-storage').returns(this.store); + this.containerStub.withArgs('simple-auth-session:main').returns(this.session); }); it("defers the application's readiness", function() { @@ -31,14 +34,33 @@ describe('setup', function() { describe('the session instance', function() { beforeEach(function() { Configuration.store = 'simple-auth-session-store:local-storage'; - sinon.spy(this.container, 'register'); + }); + + context('when a custom session class is configured', function() { + beforeEach(function() { + this.originalSessionFactory = Configuration.session; + Configuration.session = 'session:custom'; + this.otherSession = Session.extend().create({ store: this.store, container: this.container }); + this.containerStub.withArgs('session:custom').returns(this.otherSession); + sinon.spy(this.container, 'injection'); + }); + + it('is of that class', function() { + setup(this.container, this.application); + + var spyCall = this.container.injection.getCall(0); + expect(spyCall.args[2]).to.eql('session:custom'); + }); + + afterEach(function() { + Configuration.session = this.originalSessionFactory; + }); }); it('uses the LocalStorage store by default', function() { setup(this.container, this.application); - var spyCall = this.container.register.getCall(2); - expect(spyCall.args[1].store).to.eql(this.store); + expect(this.session.store).to.eql(this.store); }); it('uses a custom store if specified', function() { @@ -46,24 +68,23 @@ describe('setup', function() { var store = EphemeralStore.create(); this.containerStub.withArgs('simple-auth-session-store:ephemeral').returns(store); setup(this.container, this.application); - var spyCall = this.container.register.getCall(2); - expect(spyCall.args[1].store).to.eql(store); + expect(this.session.store).to.eql(store); }); it("uses the app's container", function() { setup(this.container, this.application); - var spyCall = this.container.register.getCall(2); - expect(spyCall.args[1].container).to.eql(this.container); + expect(this.session.container).to.eql(this.container); }); it('is registered with the Ember container', function() { + sinon.spy(this.container, 'register'); setup(this.container, this.application); var spyCall = this.container.register.getCall(2); expect(spyCall.args[0]).to.eql('simple-auth-session:main'); - expect(spyCall.args[1].constructor).to.eql(Session); + expect(spyCall.args[1]).to.eql(Session); }); it('is injected into all controllers and routes', function() { @@ -147,10 +168,7 @@ describe('setup', function() { this.xhr = sinon.useFakeXMLHttpRequest(); this.server = sinon.fakeServer.create(); this.server.autoRespond = true; - sinon.spy(this.container, 'register'); setup(this.container, this.application); - var spyCall = this.container.register.getCall(2); - this.session = spyCall.args[1]; }); describe("when the request's status is 401", function() { From 0df216052d8ac3be1d744ee4df79fb5a991aa19d Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Thu, 17 Jul 2014 18:08:24 +0200 Subject: [PATCH 08/41] fixed tests --- .../lib/simple-auth/initializer.js | 4 -- .../lib/simple-auth/setup.js | 4 ++ .../tests/simple-auth/setup-test.js | 59 +++++++++++++++---- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/initializer.js b/packages/ember-simple-auth/lib/simple-auth/initializer.js index 74f7eec94..cbdf2d2d7 100644 --- a/packages/ember-simple-auth/lib/simple-auth/initializer.js +++ b/packages/ember-simple-auth/lib/simple-auth/initializer.js @@ -1,15 +1,11 @@ var global = (typeof window !== 'undefined') ? window : {}, Ember = global.Ember; -import TestAuthenticator from 'simple-auth/authenticators/test'; import setup from './setup'; export default { name: 'simple-auth', initialize: function(container, application) { setup(container, application); - if (Ember.testing) { - container.register('simple-auth-authenticator:test', TestAuthenticator); - } } }; diff --git a/packages/ember-simple-auth/lib/simple-auth/setup.js b/packages/ember-simple-auth/lib/simple-auth/setup.js index 9ce1dba11..a701cf758 100644 --- a/packages/ember-simple-auth/lib/simple-auth/setup.js +++ b/packages/ember-simple-auth/lib/simple-auth/setup.js @@ -2,6 +2,7 @@ import Configuration from './configuration'; import Session from './session'; import LocalStorage from './stores/local-storage'; import Ephemeral from './stores/ephemeral'; +import TestAuthenticator from 'simple-auth/authenticators/test'; function extractLocationOrigin(location) { if (Ember.typeOf(location) === 'string') { @@ -35,6 +36,9 @@ function registerFactories(container) { container.register('simple-auth-session-store:local-storage', LocalStorage); container.register('simple-auth-session-store:ephemeral', Ephemeral); container.register('simple-auth-session:main', Session); + if (Ember.testing) { + container.register('simple-auth-authenticator:test', TestAuthenticator); + } } /** diff --git a/packages/ember-simple-auth/tests/simple-auth/setup-test.js b/packages/ember-simple-auth/tests/simple-auth/setup-test.js index c3adc1138..51c767e7f 100644 --- a/packages/ember-simple-auth/tests/simple-auth/setup-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/setup-test.js @@ -3,6 +3,7 @@ import Configuration from 'simple-auth/configuration'; import Session from 'simple-auth/session'; import LocalStorageStore from 'simple-auth/stores/local-storage'; import EphemeralStore from 'simple-auth/stores/ephemeral'; +import TestAuthenticator from 'simple-auth/authenticators/test'; describe('setup', function() { beforeEach(function() { @@ -31,6 +32,53 @@ describe('setup', function() { expect(Configuration.applicationRootUrl).to.eql('rootURL'); }); + it('registers the LocalStorage store', function() { + sinon.spy(this.container, 'register'); + setup(this.container, this.application); + + expect(this.container.register).to.have.been.calledWith('simple-auth-session-store:local-storage', LocalStorageStore); + }); + + it('registers the Ephemeral store', function() { + sinon.spy(this.container, 'register'); + setup(this.container, this.application); + + expect(this.container.register).to.have.been.calledWith('simple-auth-session-store:ephemeral', EphemeralStore); + }); + + it('registers the Session', function() { + sinon.spy(this.container, 'register'); + setup(this.container, this.application); + + expect(this.container.register).to.have.been.calledWith('simple-auth-session:main', Session); + }); + + describe('when Ember.testing is true', function() { + beforeEach(function() { + Ember.testing = true; + }); + + it('registers the Test authenticator', function() { + sinon.spy(this.container, 'register'); + setup(this.container, this.application); + + expect(this.container.register).to.have.been.calledWith('simple-auth-authenticator:test', TestAuthenticator); + }); + }); + + describe('when Ember.testing is false', function() { + beforeEach(function() { + Ember.testing = false; + }); + + it('registers the Test authenticator', function() { + sinon.spy(this.container, 'register'); + setup(this.container, this.application); + + expect(this.container.register).to.not.have.been.calledWith('simple-auth-authenticator:test', TestAuthenticator); + }); + }); + describe('the session instance', function() { beforeEach(function() { Configuration.store = 'simple-auth-session-store:local-storage'; @@ -78,22 +126,13 @@ describe('setup', function() { expect(this.session.container).to.eql(this.container); }); - it('is registered with the Ember container', function() { - sinon.spy(this.container, 'register'); - setup(this.container, this.application); - var spyCall = this.container.register.getCall(2); - - expect(spyCall.args[0]).to.eql('simple-auth-session:main'); - expect(spyCall.args[1]).to.eql(Session); - }); - it('is injected into all controllers and routes', function() { var _this = this; sinon.spy(this.container, 'injection'); setup(this.container, this.application); ['controller', 'route'].forEach(function(component) { - expect(_this.container.injection).to.have.been.calledWith(component, Configuration.sessionPropertyName, 'simple-auth-session:main'); + expect(_this.container.injection).to.have.been.calledWith(component, Configuration.sessionPropertyName, Configuration.session); }); }); }); From 428bfb24d563e6a2789b8c15dd58135947ae028d Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Thu, 17 Jul 2014 19:28:43 +0200 Subject: [PATCH 09/41] make sure Ember is defined --- .../lib/simple-auth/test-helpers/authenticate-session.js | 3 +++ .../lib/simple-auth/test-helpers/invalidate-session.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js index c3c6b6b79..78e059df7 100644 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js @@ -1,3 +1,6 @@ +var global = (typeof window !== 'undefined') ? window : {}, + Ember = global.Ember; + export default Ember.Test.registerAsyncHelper('authenticateSession', function(app) { var session = app.__container__.lookup('simple-auth-session:main'); session.authenticate('simple-auth-authenticator:test'); diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js index c32b3e11a..e675e236f 100644 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js @@ -1,3 +1,6 @@ +var global = (typeof window !== 'undefined') ? window : {}, + Ember = global.Ember; + export default Ember.Test.registerAsyncHelper('invalidateSession', function(app) { var session = app.__container__.lookup('simple-auth-session:main'); if (session.get('isAuthenticated')) { From 690a24ebc5457e1c8dfa50f4f7e46b4f3a06a3e5 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Thu, 17 Jul 2014 19:32:17 +0200 Subject: [PATCH 10/41] use global.Ember instead of just Ember --- packages/ember-simple-auth/wrap/browser.end | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ember-simple-auth/wrap/browser.end b/packages/ember-simple-auth/wrap/browser.end index b6049f445..a6cd57207 100644 --- a/packages/ember-simple-auth/wrap/browser.end +++ b/packages/ember-simple-auth/wrap/browser.end @@ -47,7 +47,7 @@ global.SimpleAuth = { requireModule('simple-auth/ember'); -if (Ember.testing) { +if (global.Ember.testing) { requireModule('simple-auth/test-helpers/authenticate-session'); requireModule('simple-auth/test-helpers/invalidate-session'); } From 25bdc38069945ba69c299249ed2e1cce5827883a Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Fri, 18 Jul 2014 12:06:12 +0200 Subject: [PATCH 11/41] use Configuration.session to lookup session --- .../simple-auth/test-helpers/authenticate-session.js | 4 +++- .../lib/simple-auth/test-helpers/invalidate-session.js | 4 +++- .../ember-simple-auth/tests/simple-auth/setup-test.js | 10 +++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js index 78e059df7..6760bb2fa 100644 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js @@ -1,8 +1,10 @@ var global = (typeof window !== 'undefined') ? window : {}, Ember = global.Ember; +import Configuration from '../configuration'; + export default Ember.Test.registerAsyncHelper('authenticateSession', function(app) { - var session = app.__container__.lookup('simple-auth-session:main'); + var session = app.__container__.lookup(Configuration.session); session.authenticate('simple-auth-authenticator:test'); return wait(); }); diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js index e675e236f..df016cf7d 100644 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js @@ -1,8 +1,10 @@ var global = (typeof window !== 'undefined') ? window : {}, Ember = global.Ember; +import Configuration from '../configuration'; + export default Ember.Test.registerAsyncHelper('invalidateSession', function(app) { - var session = app.__container__.lookup('simple-auth-session:main'); + var session = app.__container__.lookup(Configuration.session); if (session.get('isAuthenticated')) { session.invalidate(); } diff --git a/packages/ember-simple-auth/tests/simple-auth/setup-test.js b/packages/ember-simple-auth/tests/simple-auth/setup-test.js index 51c767e7f..c2b0c716a 100644 --- a/packages/ember-simple-auth/tests/simple-auth/setup-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/setup-test.js @@ -7,11 +7,11 @@ import TestAuthenticator from 'simple-auth/authenticators/test'; describe('setup', function() { beforeEach(function() { - this.container = { register: function() {}, injection: function() {}, lookup: function() {} }; - this.application = { deferReadiness: function() {}, advanceReadiness: function() {} }; - this.router = { get: function() { return 'rootURL'; }, send: function() {} }; - this.store = EphemeralStore.create(); - this.session = Session.create(); + this.container = { register: function() {}, injection: function() {}, lookup: function() {} }; + this.application = { deferReadiness: function() {}, advanceReadiness: function() {} }; + this.router = { get: function() { return 'rootURL'; }, send: function() {} }; + this.store = EphemeralStore.create(); + this.session = Session.create(); this.session.setProperties({ store: this.store, container: this.container }); this.containerStub = sinon.stub(this.container, 'lookup'); this.containerStub.withArgs('router:main').returns(this.router); From 212bb2b47fa53940aa6780128f375564225153ae Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Sat, 19 Jul 2014 11:50:32 +0200 Subject: [PATCH 12/41] store only one key to prevent race conditions, see #254 --- .../simple-auth-cookie-store/stores/cookie.js | 76 +++++++------------ .../stores/cookie-test.js | 20 ++--- .../lib/simple-auth/session.js | 2 +- .../lib/simple-auth/stores/base.js | 20 +---- .../lib/simple-auth/stores/ephemeral.js | 6 +- .../lib/simple-auth/stores/local-storage.js | 62 +++------------ .../stores/shared/store-behavior.js | 29 +------ 7 files changed, 56 insertions(+), 159 deletions(-) diff --git a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js index c35e1a426..65e9dd8b8 100644 --- a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js +++ b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js @@ -6,16 +6,16 @@ var global = (typeof window !== 'undefined') ? window : {}, Ember = global.Ember; /** - Store that saves its data in cookies. + Store that saves its data in a cookie. __In order to keep multiple tabs/windows of an application in sync, this - store has to periodically (every 500ms) check the cookies__ for changes as + store has to periodically (every 500ms) check the cookie__ for changes as there are no events that notify of changes in cookies. The recommended alternative is `Stores.LocalStorage` that also persistently stores data but instead of cookies relies on the `localStorage` API and does not need to poll for external changes. - By default the cookie store will use session cookies that expire and are + By default the cookie store will use a session cookie that expires and is deleted when the browser is closed. The cookie expiration period can be configured via setting [`Stores.Cooke#cookieExpirationTime`](#SimpleAuth-Stores-Cookie-cookieExpirationTime) @@ -46,27 +46,26 @@ var global = (typeof window !== 'undefined') ? window : {}, */ export default Base.extend({ /** - The prefix to use for the store's cookie names so they can be distinguished - from other cookies. + The name of the cookie the store stores its data in. This value can be configured via the global environment object: ```js window.ENV = window.ENV || {}; window.ENV['simple-auth-cookie-store'] = { - cookieNamePrefix: 'my_app_auth_' + cookieName: 'my_app_auth_session' } ``` - @property cookieNamePrefix + @property cookieName @type String @default 'ember_simple_auth:' */ - cookieNamePrefix: 'ember_simple_auth:', + cookieName: 'ember_simple_auth:session', /** - The expiration time in seconds to use for the cookies. A value of `null` - will make the cookies session cookies that expire when the browser is + The expiration time in seconds to use for the cookie. A value of `null` + will make the cookie a session cookie that expires when the browser is closed. This value can be configured via the global environment object: @@ -102,7 +101,7 @@ export default Base.extend({ */ init: function() { var globalConfig = getGlobalConfig('simple-auth-cookie-store'); - this.cookieNamePrefix = globalConfig.cookieNamePrefix || this.cookieNamePrefix; + this.cookieName = globalConfig.cookieName || this.cookieName; this.cookieExpirationTime = globalConfig.cookieExpirationTime || this.cookieExpirationTime; this.syncData(); }, @@ -114,42 +113,36 @@ export default Base.extend({ @param {Object} data The data to persist */ persist: function(data) { - for (var property in data) { - this.write(property, data[property], !!this.cookieExpirationTime ? new Date().getTime() + this.cookieExpirationTime * 1000 : null); - } + data = JSON.stringify(data || {}); + var expiration = !!this.cookieExpirationTime ? new Date().getTime() + this.cookieExpirationTime * 1000 : null; + this.write(data, expiration); this._lastData = this.restore(); }, /** - Restores all data currently saved in the session cookies identified by the - `cookieNamePrefix` (see - [`Stores.Cookie#cookieNamePrefix`](#SimpleAuth-Stores-Cookie-cookieNamePrefix)) - as a plain object. + Restores all data currently saved in the cookie as a plain object. @method restore - @return {Object} All data currently persisted in the session cookies + @return {Object} All data currently persisted in the cookie */ restore: function() { - var _this = this; - var data = {}; - this.knownCookies().forEach(function(cookie) { - data[cookie] = _this.read(cookie); - }); - return data; + var data = this.read(); + if (Ember.isEmpty(data)) { + return {}; + } else { + return JSON.parse(data); + } }, /** Clears the store by deleting all session cookies prefixed with the - `cookieNamePrefix` (see - [`SimpleAuth.Stores.Cookie#cookieNamePrefix`](#SimpleAuth-Stores-Cookie-cookieNamePrefix)). + `cookieName` (see + [`SimpleAuth.Stores.Cookie#cookieName`](#SimpleAuth-Stores-Cookie-cookieName)). @method clear */ clear: function() { - var _this = this; - this.knownCookies().forEach(function(cookie) { - _this.write(cookie, null, 0); - }); + this.write(null, 0); this._lastData = null; }, @@ -157,8 +150,8 @@ export default Base.extend({ @method read @private */ - read: function(name) { - var value = document.cookie.match(new RegExp(this.cookieNamePrefix + name + '=([^;]+)')) || []; + read: function() { + var value = document.cookie.match(new RegExp(this.cookieName + name + '=([^;]+)')) || []; return decodeURIComponent(value[1] || ''); }, @@ -166,23 +159,10 @@ export default Base.extend({ @method write @private */ - write: function(name, value, expiration) { + write: function(value, expiration) { var expires = Ember.isEmpty(expiration) ? '' : '; expires=' + new Date(expiration).toUTCString(); var secure = !!this._secureCookies ? ';secure' : ''; - document.cookie = this.cookieNamePrefix + name + '=' + encodeURIComponent(value) + expires + secure; - }, - - /** - @method knownCookies - @private - */ - knownCookies: function() { - var _this = this; - return Ember.A(document.cookie.split(/[=;\s]+/)).filter(function(element) { - return new RegExp('^' + _this.cookieNamePrefix).test(element); - }).map(function(cookie) { - return cookie.replace(_this.cookieNamePrefix, ''); - }); + document.cookie = this.cookieName + '=' + encodeURIComponent(value) + expires + secure; }, /** diff --git a/packages/ember-simple-auth-cookie-store/tests/simple-auth-cookie-store/stores/cookie-test.js b/packages/ember-simple-auth-cookie-store/tests/simple-auth-cookie-store/stores/cookie-test.js index 1b44378b8..79505e884 100644 --- a/packages/ember-simple-auth-cookie-store/tests/simple-auth-cookie-store/stores/cookie-test.js +++ b/packages/ember-simple-auth-cookie-store/tests/simple-auth-cookie-store/stores/cookie-test.js @@ -15,8 +15,8 @@ describe('Stores.Cookie', function() { describe('initilization', function() { describe('when no global environment object is defined', function() { - it('defaults cookieNamePrefix to "ember_simple_auth:"', function() { - expect(Cookie.create().cookieNamePrefix).to.eq('ember_simple_auth:'); + it('defaults cookieName to "ember_simple_auth:session"', function() { + expect(Cookie.create().cookieName).to.eq('ember_simple_auth:session'); }); it('defaults cookieExpirationTime to null', function() { @@ -28,13 +28,13 @@ describe('Stores.Cookie', function() { beforeEach(function() { window.ENV = window.ENV || {}; window.ENV['simple-auth-cookie-store'] = { - cookieNamePrefix: 'cookieNamePrefix', + cookieName: 'cookieName', cookieExpirationTime: 1 }; }); - it('uses the defined value for cookieNamePrefix', function() { - expect(Cookie.create().cookieNamePrefix).to.eq('cookieNamePrefix'); + it('uses the defined value for cookieName', function() { + expect(Cookie.create().cookieName).to.eq('cookieName'); }); it('uses the defined value for cookieExpirationTime', function() { @@ -48,12 +48,12 @@ describe('Stores.Cookie', function() { }); describe('#persist', function() { - it('respects the configured cookieNamePrefix', function() { + it('respects the configured cookieName', function() { this.store = Cookie.create(); - this.store.cookieNamePrefix = 'test:'; + this.store.cookieName = 'test:session'; this.store.persist({ key: 'value' }); - expect(document.cookie).to.contain('test:key=value'); + expect(document.cookie).to.contain('test:session=%7B%22key%22%3A%22value%22%7D'); }); }); @@ -68,7 +68,7 @@ describe('Stores.Cookie', function() { }); it('is not triggered when the cookie has not actually changed', function(done) { - document.cookie = 'ember_simple_auth:key=value;'; + document.cookie = 'ember_simple_auth:session=%7B%22key%22%3A%22value%22%7D;'; this.store.syncData(); Ember.run.next(this, function() { @@ -78,7 +78,7 @@ describe('Stores.Cookie', function() { }); it('is triggered when the cookie changed', function(done) { - document.cookie = 'ember_simple_auth:key=other value;'; + document.cookie = 'ember_simple_auth:session=%7B%22key%22%3A%22other%20value%22%7D;'; this.store.syncData(); Ember.run.next(this, function() { diff --git a/packages/ember-simple-auth/lib/simple-auth/session.js b/packages/ember-simple-auth/lib/simple-auth/session.js index 656c531e9..d6230cdda 100644 --- a/packages/ember-simple-auth/lib/simple-auth/session.js +++ b/packages/ember-simple-auth/lib/simple-auth/session.js @@ -245,7 +245,7 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { }); this.bindToAuthenticatorEvents(); var data = Ember.$.extend({ authenticator: authenticator }, this.content); - this.store.replace(data); + this.store.persist(data); this.endPropertyChanges(); if (trigger) { this.trigger('sessionAuthenticationSucceeded'); diff --git a/packages/ember-simple-auth/lib/simple-auth/stores/base.js b/packages/ember-simple-auth/lib/simple-auth/stores/base.js index 637f960b8..ddc5a3699 100644 --- a/packages/ember-simple-auth/lib/simple-auth/stores/base.js +++ b/packages/ember-simple-auth/lib/simple-auth/stores/base.js @@ -39,7 +39,8 @@ export default Ember.Object.extend(Ember.Evented, { */ /** - Persists the `data` in the store. + Persists the `data` in the store. This actually replaces all currently + stored data. `Stores.Base`'s implementation does nothing. @@ -61,23 +62,6 @@ export default Ember.Object.extend(Ember.Evented, { return {}; }, - /** - Replaces all data currently saved in the store with the specified `data`. - - `Stores.Base`'s implementation clears the store, then persists the - specified `data`. If the store's current content is equal to the specified - `data`, nothing is done. - - @method replace - @param {Object} data The data to replace the store's content with - */ - replace: function(data) { - if (!flatObjectsAreEqual(data, this.restore())) { - this.clear(); - this.persist(data); - } - }, - /** Clears the store. diff --git a/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js b/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js index c2ce01f3c..a041cf93c 100644 --- a/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js +++ b/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js @@ -34,7 +34,7 @@ export default Base.extend({ @param {Object} data The data to persist */ persist: function(data) { - this._data = Ember.$.extend(data, this._data); + this._data = JSON.stringify(data || {}); }, /** @@ -44,7 +44,7 @@ export default Base.extend({ @return {Object} All data currently persisted */ restore: function() { - return Ember.$.extend({}, this._data); + return JSON.parse(this._data) || {}; }, /** @@ -54,6 +54,6 @@ export default Base.extend({ */ clear: function() { delete this._data; - this._data = {}; + this._data = '{}'; } }); diff --git a/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js b/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js index e26b309b5..93e70e5d8 100644 --- a/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js +++ b/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js @@ -19,20 +19,13 @@ import flatObjectsAreEqual from '../utils/flat-objects-are-equal'; */ export default Base.extend({ /** - The prefix to use for the store's keys so they can be distinguished from - others. + The key the store stores the data in. - @property keyPrefix + @property key @type String - @default 'ember_simple_auth:' + @default 'ember_simple_auth:session' */ - keyPrefix: 'ember_simple_auth:', - - /** - @property _triggerChangeEventTimeout - @private - */ - _triggerChangeEventTimeout: null, + key: 'ember_simple_auth:session', /** @method init @@ -49,10 +42,8 @@ export default Base.extend({ @param {Object} data The data to persist */ persist: function(data) { - for (var property in data) { - var key = this.buildStorageKey(property); - localStorage.setItem(key, data[property]); - } + data = JSON.stringify(data || {}); + localStorage.setItem(this.key, data); this._lastData = this.restore(); }, @@ -64,13 +55,8 @@ export default Base.extend({ @return {Object} All data currently persisted in the `localStorage` */ restore: function() { - var _this = this; - var data = {}; - this.knownKeys().forEach(function(key) { - var originalKey = key.replace(_this.keyPrefix, ''); - data[originalKey] = localStorage.getItem(key); - }); - return data; + var data = localStorage.getItem(this.key); + return JSON.parse(data) || {}; }, /** @@ -80,35 +66,10 @@ export default Base.extend({ @method clear */ clear: function() { - this.knownKeys().forEach(function(key) { - localStorage.removeItem(key); - }); + localStorage.removeItem(this.key); this._lastData = null; }, - /** - @method buildStorageKey - @private - */ - buildStorageKey: function(property) { - return this.keyPrefix + property; - }, - - /** - @method knownKeys - @private - */ - knownKeys: function(callback) { - var keys = Ember.A([]); - for (var i = 0, l = localStorage.length; i < l; i++) { - var key = localStorage.key(i); - if (key.indexOf(this.keyPrefix) === 0) { - keys.push(key); - } - } - return keys; - }, - /** @method bindToStorageEvents @private @@ -119,10 +80,7 @@ export default Base.extend({ var data = _this.restore(); if (!flatObjectsAreEqual(data, _this._lastData)) { _this._lastData = data; - Ember.run.cancel(_this._triggerChangeEventTimeout); - _this._triggerChangeEventTimeout = Ember.run.next(_this, function() { - this.trigger('sessionDataUpdated', data); - }); + _this.trigger('sessionDataUpdated', data); } }); } diff --git a/packages/ember-simple-auth/tests/simple-auth/stores/shared/store-behavior.js b/packages/ember-simple-auth/tests/simple-auth/stores/shared/store-behavior.js index 3b68a5248..2eb0a0b62 100644 --- a/packages/ember-simple-auth/tests/simple-auth/stores/shared/store-behavior.js +++ b/packages/ember-simple-auth/tests/simple-auth/stores/shared/store-behavior.js @@ -8,11 +8,11 @@ export default function(options) { expect(this.store.restore()).to.eql({ key: 'value' }); }); - it('does not override existing data', function() { + it('overrides existing data', function() { this.store.persist({ key1: 'value1' }); this.store.persist({ key2: 'value2' }); - expect(this.store.restore()).to.eql({ key1: 'value1', key2: 'value2' }); + expect(this.store.restore()).to.eql({ key2: 'value2' }); }); it('does not trigger the "updated" event', function(done) { @@ -55,31 +55,6 @@ export default function(options) { }); }); - describe('#replace', function() { - beforeEach(function() { - this.store.persist({ key: 'value' }); - }); - - describe("when the store's content and the specified data are equal", function() { - it('does not do anything', function() { - sinon.spy(this.store, 'clear'); - sinon.spy(this.store, 'persist'); - this.store.replace({ key: 'value' }); - - expect(this.store.clear).to.not.have.been.called; - expect(this.store.persist).to.not.have.been.called; - }); - }); - - describe("when the store's content and the specified data are not equal", function() { - it('replaces all existing data in the store', function() { - this.store.replace({ otherKey: 'other value' }); - - expect(this.store.restore()).to.eql({ otherKey: 'other value' }); - }); - }); - }); - describe('#clear', function() { it('empties the store', function() { this.store.persist({ key1: 'value1', key2: 'value2' }); From d32a0102b3d5808c221bc0e01169261d5dbd3065 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Sat, 19 Jul 2014 13:18:33 +0200 Subject: [PATCH 13/41] return promise from LoginControllerMixin's authenticate action --- .../simple-auth/mixins/authentication-controller-mixin.js | 1 + .../lib/simple-auth/mixins/login-controller-mixin.js | 2 +- .../mixins/authentication-controller-mixin-test.js | 2 +- .../simple-auth/mixins/login-controller-mixin-test.js | 8 ++++++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js index 4ec3e0b00..6337c4ff1 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js @@ -40,6 +40,7 @@ export default Ember.Mixin.create({ authenticate: function(options) { var authenticator = this.get('authenticator'); Ember.assert('AuthenticationControllerMixin/LoginControllerMixin require the authenticator property to be set on the controller', !Ember.isEmpty(authenticator)); + console.log('here'); return this.get(Configuration.sessionPropertyName).authenticate(this.get('authenticator'), options); } } diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js index 3d2ac74b9..63d370f29 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js @@ -50,7 +50,7 @@ export default Ember.Mixin.create(AuthenticationControllerMixin, { authenticate: function() { var data = this.getProperties('identification', 'password'); this.set('password', null); - this._super(data); + return this._super(data); } } }); diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js index 28bb60372..63ede27c8 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/authentication-controller-mixin-test.js @@ -19,7 +19,7 @@ describe('AuthenticationControllerMixin', function() { expect(this.session.authenticate).to.have.been.calledWith('authenticator', { some: 'options' }); }); - it('returns the prmoise returned by the session', function() { + it('returns the promise returned by the session', function() { var promise = new Ember.RSVP.Promise(function() {}); sinon.stub(this.session, 'authenticate').returns(promise); diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js index 816cbea0c..27f27b113 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/login-controller-mixin-test.js @@ -36,6 +36,14 @@ describe('LoginControllerMixin', function() { { identification: 'identification', password: 'password' }); }); + + it('returns the promise returned by the session', function() { + var promise = new Ember.RSVP.Promise(function() {}); + this.session.authenticate.restore(); + sinon.stub(this.session, 'authenticate').returns(promise); + + expect(this.controller._actions.authenticate.apply(this.controller, [{ some: 'options' }])).to.eq(promise); + }); }); }); From 3ca23b2409ea746ce5e625052ce5a3fb89d472bc Mon Sep 17 00:00:00 2001 From: Michael Cleaver Date: Mon, 21 Jul 2014 16:41:04 +1000 Subject: [PATCH 14/41] Update application route mixin to support deep links when not authenticated. Prevent event handlers from being assigned more than once in the beforeModel hook. This could happen when the very first route transition was aborted to redirect to the login route. Aborted transitions are no longer 'lost' due to the sessionAuthenticationSucceeded action being called a second time after the attemptedTransition was already cleared. --- .../simple-auth/mixins/application-route-mixin.js | 2 ++ .../mixins/application-route-mixin-test.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js index 4e511b069..4897a313e 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js @@ -66,6 +66,8 @@ export default Ember.Mixin.create({ */ beforeModel: function(transition) { this._super(transition); + if (this.get('_authEventListenersAssigned')) return; + this.set('_authEventListenersAssigned', true); var _this = this; Ember.A([ 'sessionAuthenticationSucceeded', diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js index 0d6dfd8b3..cb42a4052 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js @@ -77,6 +77,21 @@ describe('ApplicationRouteMixin', function() { done(); }); }); + + describe('and "beforeModel" was triggered multiple times due to an aborted initial transition', function() { + it('the action is only invoked on the transition once', function(done) { + this.route.beforeModel(this.transition); + + this.transition.isActive = true; + this.session.trigger('sessionAuthenticationSucceeded'); + + Ember.run.next(this, function() { + expect(this.transition.send).to.have.been.calledWith('sessionAuthenticationSucceeded'); + expect(this.transition.send).to.have.been.calledOnce; + done(); + }); + }); + }); }); }); From 4545393e3804afc0fb54e59fb8d4c1a4c36e6b50 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 21 Jul 2014 09:43:38 +0200 Subject: [PATCH 15/41] removed debug output --- .../lib/simple-auth/mixins/authentication-controller-mixin.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js index 6337c4ff1..4ec3e0b00 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js @@ -40,7 +40,6 @@ export default Ember.Mixin.create({ authenticate: function(options) { var authenticator = this.get('authenticator'); Ember.assert('AuthenticationControllerMixin/LoginControllerMixin require the authenticator property to be set on the controller', !Ember.isEmpty(authenticator)); - console.log('here'); return this.get(Configuration.sessionPropertyName).authenticate(this.get('authenticator'), options); } } From f082a72928d43e3eea90e0e0440abfda69f3cc25 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 21 Jul 2014 09:49:58 +0200 Subject: [PATCH 16/41] cleanup --- .../mixins/application-route-mixin.js | 31 ++++++++++--------- .../mixins/application-route-mixin-test.js | 25 ++++++--------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js index 4897a313e..90b5eebaa 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js @@ -66,22 +66,23 @@ export default Ember.Mixin.create({ */ beforeModel: function(transition) { this._super(transition); - if (this.get('_authEventListenersAssigned')) return; - this.set('_authEventListenersAssigned', true); - var _this = this; - Ember.A([ - 'sessionAuthenticationSucceeded', - 'sessionAuthenticationFailed', - 'sessionInvalidationSucceeded', - 'sessionInvalidationFailed', - 'authorizationFailed' - ]).forEach(function(event) { - _this.get(Configuration.sessionPropertyName).on(event, function(error) { - Array.prototype.unshift.call(arguments, event); - var target = transition.isActive ? transition : _this; - target.send.apply(target, arguments); + if (!this.get('_authEventListenersAssigned')) { + this.set('_authEventListenersAssigned', true); + var _this = this; + Ember.A([ + 'sessionAuthenticationSucceeded', + 'sessionAuthenticationFailed', + 'sessionInvalidationSucceeded', + 'sessionInvalidationFailed', + 'authorizationFailed' + ]).forEach(function(event) { + _this.get(Configuration.sessionPropertyName).on(event, function(error) { + Array.prototype.unshift.call(arguments, event); + var target = transition.isActive ? transition : _this; + target.send.apply(target, arguments); + }); }); - }); + } }, actions: { diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js index e7bcd6891..de494fec4 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js @@ -68,6 +68,16 @@ describe('ApplicationRouteMixin', function() { }); }); + it('does not attach the event listeners twice', function(done) { + this.route.beforeModel(this.transition); + this.session.trigger('sessionAuthenticationSucceeded'); + + Ember.run.next(this, function() { + expect(this.route.send).to.have.been.calledOnce; + done(); + }); + }); + describe('when the initial transition is still active', function() { it('invokes the action on the transition', function(done) { this.transition.isActive = true; @@ -78,21 +88,6 @@ describe('ApplicationRouteMixin', function() { done(); }); }); - - describe('and "beforeModel" was triggered multiple times due to an aborted initial transition', function() { - it('the action is only invoked on the transition once', function(done) { - this.route.beforeModel(this.transition); - - this.transition.isActive = true; - this.session.trigger('sessionAuthenticationSucceeded'); - - Ember.run.next(this, function() { - expect(this.transition.send).to.have.been.calledWith('sessionAuthenticationSucceeded'); - expect(this.transition.send).to.have.been.calledOnce; - done(); - }); - }); - }); }); }); From e4628ee4439f29635c93c23cfd15eec0080b5530 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 21 Jul 2014 12:20:08 +0200 Subject: [PATCH 17/41] nicely display the library names and versions --- Gruntfile.js | 20 ++++++++++++++++++- package.json | 1 + .../wrap/browser.end | 2 ++ .../ember-simple-auth-devise/wrap/browser.end | 2 ++ .../ember-simple-auth-oauth2/wrap/browser.end | 2 ++ .../ember-simple-auth-torii/wrap/browser.end | 2 ++ packages/ember-simple-auth/wrap/browser.end | 2 ++ 7 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 15cda3757..41ff6802a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -12,7 +12,8 @@ module.exports = function(grunt) { 'clean', 'transpile:amd', 'concat:amd', - 'concat:browser' + 'concat:browser', + 'string-replace:version' ]); this.registerTask('build_tests', "Builds Ember Simple Auth's tests", [ @@ -149,6 +150,22 @@ module.exports = function(grunt) { } }, + 'string-replace': { + version: { + files: { + 'tmp/': 'tmp/*.js' + }, + options: { + replacements: packages.map(function(pkg) { + return { + pattern: '{{ VERSION }}', + replacement: pkg.version + }; + }) + } + } + }, + copy: { plain_download: { files: packages.map(function(pkg) { @@ -281,4 +298,5 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-contrib-yuidoc'); grunt.loadNpmTasks('grunt-compile-handlebars'); grunt.loadNpmTasks('grunt-lintspaces'); + grunt.loadNpmTasks('grunt-string-replace'); }; diff --git a/package.json b/package.json index a6865559a..8bf797682 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "grunt-contrib-copy": "^0.5.0", "grunt-contrib-concat": "^0.3.0", "grunt-contrib-yuidoc": "^0.5.1", + "grunt-string-replace": "^0.2.7", "yuidocjs": "^0.3.49", "grunt-compile-handlebars": "^0.6.3", "grunt-lintspaces": "^0.4.1", diff --git a/packages/ember-simple-auth-cookie-store/wrap/browser.end b/packages/ember-simple-auth-cookie-store/wrap/browser.end index 1143fec7b..bab55d177 100644 --- a/packages/ember-simple-auth-cookie-store/wrap/browser.end +++ b/packages/ember-simple-auth-cookie-store/wrap/browser.end @@ -14,4 +14,6 @@ var Cookie = requireModule('simple-auth-cookie-store/stores/cookie').defaul global.SimpleAuth.Stores.Cookie = Cookie; requireModule('simple-auth-cookie-store/ember'); + +Ember.libraries.register('Ember Simple Auth Cookie Store', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-devise/wrap/browser.end b/packages/ember-simple-auth-devise/wrap/browser.end index 776e77555..1a068bf92 100644 --- a/packages/ember-simple-auth-devise/wrap/browser.end +++ b/packages/ember-simple-auth-devise/wrap/browser.end @@ -19,4 +19,6 @@ global.SimpleAuth.Authenticators.Devise = Authenticator; global.SimpleAuth.Authorizers.Devise = Authorizer; requireModule('simple-auth-devise/ember'); + +Ember.libraries.register('Ember Simple Auth Devise', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-oauth2/wrap/browser.end b/packages/ember-simple-auth-oauth2/wrap/browser.end index 7e66338d2..7a74e1c87 100644 --- a/packages/ember-simple-auth-oauth2/wrap/browser.end +++ b/packages/ember-simple-auth-oauth2/wrap/browser.end @@ -19,4 +19,6 @@ global.SimpleAuth.Authenticators.OAuth2 = Authenticator; global.SimpleAuth.Authorizers.OAuth2 = Authorizer; requireModule('simple-auth-oauth2/ember'); + +Ember.libraries.register('Ember Simple Auth OAuth 2.0', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-torii/wrap/browser.end b/packages/ember-simple-auth-torii/wrap/browser.end index 5e688026d..72c0a08a4 100644 --- a/packages/ember-simple-auth-torii/wrap/browser.end +++ b/packages/ember-simple-auth-torii/wrap/browser.end @@ -7,4 +7,6 @@ var Authenticator = requireModule('simple-auth-torii/authenticators/torii').defa global.SimpleAuth.Authenticators.Torii = Authenticator; requireModule('simple-auth-torii/ember'); + +Ember.libraries.register('Ember Simple Auth Torii', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth/wrap/browser.end b/packages/ember-simple-auth/wrap/browser.end index a6cd57207..092e5e3ca 100644 --- a/packages/ember-simple-auth/wrap/browser.end +++ b/packages/ember-simple-auth/wrap/browser.end @@ -51,4 +51,6 @@ if (global.Ember.testing) { requireModule('simple-auth/test-helpers/authenticate-session'); requireModule('simple-auth/test-helpers/invalidate-session'); } + +Ember.libraries.register('Ember Simple Auth', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); From 341cc9019c8cd9db1735227e85aa33c88e1ef6aa Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 21 Jul 2014 12:34:25 +0200 Subject: [PATCH 18/41] don't explicitely get Ember global --- .../lib/simple-auth-cookie-store/ember.js | 3 --- .../lib/simple-auth-cookie-store/initializer.js | 3 --- .../lib/simple-auth-cookie-store/stores/cookie.js | 3 --- .../lib/simple-auth-devise/authenticators/devise.js | 3 --- .../lib/simple-auth-devise/authorizers/devise.js | 3 --- .../ember-simple-auth-devise/lib/simple-auth-devise/ember.js | 3 --- .../lib/simple-auth-devise/initializer.js | 3 --- .../lib/simple-auth-oauth2/authenticators/oauth2.js | 3 --- .../lib/simple-auth-oauth2/authorizers/oauth2.js | 3 --- .../ember-simple-auth-oauth2/lib/simple-auth-oauth2/ember.js | 3 --- .../lib/simple-auth-oauth2/initializer.js | 3 --- .../lib/simple-auth-torii/authenticators/torii.js | 3 --- .../ember-simple-auth-torii/lib/simple-auth-torii/ember.js | 3 --- .../ember-simple-auth/lib/simple-auth/authenticators/base.js | 3 --- packages/ember-simple-auth/lib/simple-auth/authorizers/base.js | 3 --- packages/ember-simple-auth/lib/simple-auth/ember.js | 3 --- packages/ember-simple-auth/lib/simple-auth/initializer.js | 3 --- .../lib/simple-auth/mixins/application-route-mixin.js | 3 --- .../lib/simple-auth/mixins/authenticated-route-mixin.js | 3 --- .../lib/simple-auth/mixins/authentication-controller-mixin.js | 3 --- .../lib/simple-auth/mixins/login-controller-mixin.js | 3 --- packages/ember-simple-auth/lib/simple-auth/session.js | 3 --- packages/ember-simple-auth/lib/simple-auth/stores/base.js | 3 --- packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js | 3 --- .../ember-simple-auth/lib/simple-auth/stores/local-storage.js | 3 --- .../lib/simple-auth/test-helpers/authenticate-session.js | 3 --- .../lib/simple-auth/test-helpers/invalidate-session.js | 3 --- 27 files changed, 81 deletions(-) diff --git a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/ember.js b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/ember.js index 47e259a66..ce439619a 100644 --- a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/ember.js +++ b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/ember.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import initializer from './initializer'; Ember.onLoad('Ember.Application', function(Application) { diff --git a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/initializer.js b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/initializer.js index fc2f5530e..c0d046866 100644 --- a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/initializer.js +++ b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/initializer.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Store from 'simple-auth-cookie-store/stores/cookie'; export default { diff --git a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js index 65e9dd8b8..aeaa362fb 100644 --- a/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js +++ b/packages/ember-simple-auth-cookie-store/lib/simple-auth-cookie-store/stores/cookie.js @@ -2,9 +2,6 @@ import Base from 'simple-auth/stores/base'; import flatObjectsAreEqual from 'simple-auth/utils/flat-objects-are-equal'; import getGlobalConfig from 'simple-auth/utils/get-global-config'; -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** Store that saves its data in a cookie. diff --git a/packages/ember-simple-auth-devise/lib/simple-auth-devise/authenticators/devise.js b/packages/ember-simple-auth-devise/lib/simple-auth-devise/authenticators/devise.js index 5940bfe26..2a3586ffa 100644 --- a/packages/ember-simple-auth-devise/lib/simple-auth-devise/authenticators/devise.js +++ b/packages/ember-simple-auth-devise/lib/simple-auth-devise/authenticators/devise.js @@ -2,9 +2,6 @@ import Base from 'simple-auth/authenticators/base'; import isSecureUrl from 'simple-auth/utils/is-secure-url'; import getGlobalConfig from 'simple-auth/utils/get-global-config'; -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** Authenticator that works with the Ruby gem [Devise](https://github.com/plataformatec/devise). diff --git a/packages/ember-simple-auth-devise/lib/simple-auth-devise/authorizers/devise.js b/packages/ember-simple-auth-devise/lib/simple-auth-devise/authorizers/devise.js index 35c3410f9..0cc1ecc78 100644 --- a/packages/ember-simple-auth-devise/lib/simple-auth-devise/authorizers/devise.js +++ b/packages/ember-simple-auth-devise/lib/simple-auth-devise/authorizers/devise.js @@ -1,9 +1,6 @@ import Base from 'simple-auth/authorizers/base'; import isSecureUrl from 'simple-auth/utils/is-secure-url'; -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** Authenticator that works with the Ruby gem [Devise](https://github.com/plataformatec/devise) by sending the `user_token` diff --git a/packages/ember-simple-auth-devise/lib/simple-auth-devise/ember.js b/packages/ember-simple-auth-devise/lib/simple-auth-devise/ember.js index 47e259a66..ce439619a 100644 --- a/packages/ember-simple-auth-devise/lib/simple-auth-devise/ember.js +++ b/packages/ember-simple-auth-devise/lib/simple-auth-devise/ember.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import initializer from './initializer'; Ember.onLoad('Ember.Application', function(Application) { diff --git a/packages/ember-simple-auth-devise/lib/simple-auth-devise/initializer.js b/packages/ember-simple-auth-devise/lib/simple-auth-devise/initializer.js index 76e47cd1a..1b5046fec 100644 --- a/packages/ember-simple-auth-devise/lib/simple-auth-devise/initializer.js +++ b/packages/ember-simple-auth-devise/lib/simple-auth-devise/initializer.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Authenticator from 'simple-auth-devise/authenticators/devise'; import Authorizer from 'simple-auth-devise/authorizers/devise'; diff --git a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js index 569dcd26c..7807a02d5 100644 --- a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js +++ b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js @@ -2,9 +2,6 @@ import Base from 'simple-auth/authenticators/base'; import isSecureUrl from 'simple-auth/utils/is-secure-url'; import getGlobalConfig from 'simple-auth/utils/get-global-config'; -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** Authenticator that conforms to OAuth 2 ([RFC 6749](http://tools.ietf.org/html/rfc6749)), specifically the _"Resource diff --git a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authorizers/oauth2.js b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authorizers/oauth2.js index 23c6fd74a..dfc599976 100644 --- a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authorizers/oauth2.js +++ b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authorizers/oauth2.js @@ -1,9 +1,6 @@ import Base from 'simple-auth/authorizers/base'; import isSecureUrl from 'simple-auth/utils/is-secure-url'; -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** Authorizer that conforms to OAuth 2 ([RFC 6749](http://tools.ietf.org/html/rfc6749)) by sending a bearer token diff --git a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/ember.js b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/ember.js index 47e259a66..ce439619a 100644 --- a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/ember.js +++ b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/ember.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import initializer from './initializer'; Ember.onLoad('Ember.Application', function(Application) { diff --git a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/initializer.js b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/initializer.js index bbd9e6009..6b3d32354 100644 --- a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/initializer.js +++ b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/initializer.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Authenticator from 'simple-auth-oauth2/authenticators/oauth2'; import Authorizer from 'simple-auth-oauth2/authorizers/oauth2'; diff --git a/packages/ember-simple-auth-torii/lib/simple-auth-torii/authenticators/torii.js b/packages/ember-simple-auth-torii/lib/simple-auth-torii/authenticators/torii.js index 2b8710562..2e5c5347d 100644 --- a/packages/ember-simple-auth-torii/lib/simple-auth-torii/authenticators/torii.js +++ b/packages/ember-simple-auth-torii/lib/simple-auth-torii/authenticators/torii.js @@ -1,8 +1,5 @@ import Base from 'simple-auth/authenticators/base'; -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** Authenticator that wraps the [Torii library](https://github.com/Vestorly/torii). diff --git a/packages/ember-simple-auth-torii/lib/simple-auth-torii/ember.js b/packages/ember-simple-auth-torii/lib/simple-auth-torii/ember.js index 47e259a66..ce439619a 100644 --- a/packages/ember-simple-auth-torii/lib/simple-auth-torii/ember.js +++ b/packages/ember-simple-auth-torii/lib/simple-auth-torii/ember.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import initializer from './initializer'; Ember.onLoad('Ember.Application', function(Application) { diff --git a/packages/ember-simple-auth/lib/simple-auth/authenticators/base.js b/packages/ember-simple-auth/lib/simple-auth/authenticators/base.js index 2700da0c4..253d21856 100644 --- a/packages/ember-simple-auth/lib/simple-auth/authenticators/base.js +++ b/packages/ember-simple-auth/lib/simple-auth/authenticators/base.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** The base for all authenticators. __This serves as a starting point for implementing custom authenticators and must not be used directly.__ diff --git a/packages/ember-simple-auth/lib/simple-auth/authorizers/base.js b/packages/ember-simple-auth/lib/simple-auth/authorizers/base.js index 844b50dfa..48e52df66 100644 --- a/packages/ember-simple-auth/lib/simple-auth/authorizers/base.js +++ b/packages/ember-simple-auth/lib/simple-auth/authorizers/base.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** The base for all authorizers. __This serves as a starting point for implementing custom authorizers and must not be used directly.__ diff --git a/packages/ember-simple-auth/lib/simple-auth/ember.js b/packages/ember-simple-auth/lib/simple-auth/ember.js index 47e259a66..ce439619a 100644 --- a/packages/ember-simple-auth/lib/simple-auth/ember.js +++ b/packages/ember-simple-auth/lib/simple-auth/ember.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import initializer from './initializer'; Ember.onLoad('Ember.Application', function(Application) { diff --git a/packages/ember-simple-auth/lib/simple-auth/initializer.js b/packages/ember-simple-auth/lib/simple-auth/initializer.js index cbdf2d2d7..2c84280fc 100644 --- a/packages/ember-simple-auth/lib/simple-auth/initializer.js +++ b/packages/ember-simple-auth/lib/simple-auth/initializer.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import setup from './setup'; export default { diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js index 90b5eebaa..b786ac11d 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Configuration from './../configuration'; /** diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/authenticated-route-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/authenticated-route-mixin.js index 6ce4bc876..9fdc7ad51 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/authenticated-route-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/authenticated-route-mixin.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Configuration from './../configuration'; /** diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js index 4ec3e0b00..2e146411b 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/authentication-controller-mixin.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Configuration from './../configuration'; /** diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js index 63d370f29..25ae5bd7a 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/login-controller-mixin.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Configuration from './../configuration'; import AuthenticationControllerMixin from './authentication-controller-mixin'; diff --git a/packages/ember-simple-auth/lib/simple-auth/session.js b/packages/ember-simple-auth/lib/simple-auth/session.js index d6230cdda..36c5b94e7 100644 --- a/packages/ember-simple-auth/lib/simple-auth/session.js +++ b/packages/ember-simple-auth/lib/simple-auth/session.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - /** __The session provides access to the current authentication state as well as any data the authenticator resolved with__ (see diff --git a/packages/ember-simple-auth/lib/simple-auth/stores/base.js b/packages/ember-simple-auth/lib/simple-auth/stores/base.js index ddc5a3699..bd84e6ae6 100644 --- a/packages/ember-simple-auth/lib/simple-auth/stores/base.js +++ b/packages/ember-simple-auth/lib/simple-auth/stores/base.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import flatObjectsAreEqual from '../utils/flat-objects-are-equal'; /** diff --git a/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js b/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js index a041cf93c..d5530e21b 100644 --- a/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js +++ b/packages/ember-simple-auth/lib/simple-auth/stores/ephemeral.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Base from './base'; /** diff --git a/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js b/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js index 93e70e5d8..44230e814 100644 --- a/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js +++ b/packages/ember-simple-auth/lib/simple-auth/stores/local-storage.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Base from './base'; import flatObjectsAreEqual from '../utils/flat-objects-are-equal'; diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js index 6760bb2fa..ea87cc83b 100644 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Configuration from '../configuration'; export default Ember.Test.registerAsyncHelper('authenticateSession', function(app) { diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js index df016cf7d..2c67fdc4c 100644 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js +++ b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js @@ -1,6 +1,3 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - import Configuration from '../configuration'; export default Ember.Test.registerAsyncHelper('invalidateSession', function(app) { From d157cf7c4b646f9b94f40bc0aba8fc1fe63b22c4 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Mon, 21 Jul 2014 13:15:37 +0200 Subject: [PATCH 19/41] fixed OAuth 2.0 refresh on restore, closes #249 --- .../authenticators/oauth2.js | 24 +-- .../authenticators/oauth2-test.js | 146 +++++++++--------- 2 files changed, 86 insertions(+), 84 deletions(-) diff --git a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js index 569dcd26c..010a455b0 100644 --- a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js +++ b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js @@ -121,22 +121,22 @@ export default Base.extend({ restore: function(data) { var _this = this; return new Ember.RSVP.Promise(function(resolve, reject) { - if (!Ember.isEmpty(data.access_token)) { - var now = (new Date()).getTime(); - if (!Ember.isEmpty(data.expires_at) && data.expires_at < now) { - if (_this.refreshAccessTokens) { - _this.refreshAccessToken(data.expires_in, data.refresh_token).then(function(data) { - resolve(data); - }, reject); - } else { - reject(); - } + var now = (new Date()).getTime(); + if (!Ember.isEmpty(data.expires_at) && data.expires_at < now) { + if (_this.refreshAccessTokens) { + _this.refreshAccessToken(data.expires_in, data.refresh_token).then(function(data) { + resolve(data); + }, reject); + } else { + reject(); + } + } else { + if (Ember.isEmpty(data.access_token)) { + reject(); } else { _this.scheduleAccessTokenRefresh(data.expires_in, data.expires_at, data.refresh_token); resolve(data); } - } else { - reject(); } }); }, diff --git a/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authenticators/oauth2-test.js b/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authenticators/oauth2-test.js index 946dcf490..c64cf7031 100644 --- a/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authenticators/oauth2-test.js +++ b/packages/ember-simple-auth-oauth2/tests/simple-auth-oauth2/authenticators/oauth2-test.js @@ -53,58 +53,36 @@ describe('OAuth2', function() { }); describe('#restore', function() { - describe('when the data contains an access_token', function() { + describe('when the data includes expiration data', function() { it('resolves with the correct data', function(done) { - this.authenticator.restore({ access_token: 'secret token!' }).then(function(data) { - expect(data).to.eql({ access_token: 'secret token!' }); + this.authenticator.restore({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }).then(function(data) { + expect(data).to.eql({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }); done(); }); }); - describe('when the data includes expiration data', function() { - it('resolves with the correct data', function(done) { - this.authenticator.restore({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }).then(function(data) { - expect(data).to.eql({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }); - done(); - }); - }); - - describe('when the data includes an expiration time in the past', function() { - describe('when automatic token refreshing is enabled', function() { - describe('when the refresh request is successful', function() { - beforeEach(function() { - this.server.respondWith('POST', '/token', [ - 200, - { 'Content-Type': 'application/json' }, - '{ "access_token": "secret token 2!", "expires_in": 67890, "refresh_token": "refresh token 2!" }' - ]); - }); - - it('resolves with the correct data', function(done) { - this.authenticator.restore({ access_token: 'secret token!', expires_at: 1 }).then(function(data) { - expect(data.expires_at).to.be.greaterThan(new Date().getTime()); - delete data.expires_at; - expect(data).to.eql({ access_token: 'secret token 2!', expires_in: 67890, refresh_token: 'refresh token 2!' }); - done(); - }); - }); + describe('when the data includes an expiration time in the past', function() { + describe('when automatic token refreshing is enabled', function() { + describe('when the refresh request is successful', function() { + beforeEach(function() { + this.server.respondWith('POST', '/token', [ + 200, + { 'Content-Type': 'application/json' }, + '{ "access_token": "secret token 2!", "expires_in": 67890, "refresh_token": "refresh token 2!" }' + ]); }); - describe('when the access token is not refreshed successfully', function() { - it('returns a rejecting promise', function(done) { - this.authenticator.restore({ access_token: 'secret token!', expires_at: 1 }).then(null, function() { - expect(true).to.be.true; - done(); - }); + it('resolves with the correct data', function(done) { + this.authenticator.restore({ access_token: 'secret token!', expires_at: 1 }).then(function(data) { + expect(data.expires_at).to.be.greaterThan(new Date().getTime()); + delete data.expires_at; + expect(data).to.eql({ access_token: 'secret token 2!', expires_in: 67890, refresh_token: 'refresh token 2!' }); + done(); }); }); }); - describe('when automatic token refreshing is disabled', function() { - beforeEach(function() { - this.authenticator.set('refreshAccessTokens', false); - }); - + describe('when the access token is not refreshed successfully', function() { it('returns a rejecting promise', function(done) { this.authenticator.restore({ access_token: 'secret token!', expires_at: 1 }).then(null, function() { expect(true).to.be.true; @@ -114,58 +92,82 @@ describe('OAuth2', function() { }); }); - describe('when automatic token refreshing is enabled', function() { + describe('when automatic token refreshing is disabled', function() { beforeEach(function() { - sinon.spy(Ember.run, 'later'); + this.authenticator.set('refreshAccessTokens', false); }); - it('schedules a token refresh', function(done) { - var _this = this; - - this.authenticator.restore({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }).then(function(data) { - var spyCall = Ember.run.later.getCall(0); - - expect(spyCall.args[1]).to.eql(_this.authenticator.refreshAccessToken); - expect(spyCall.args[2]).to.eql(12345); - expect(spyCall.args[3]).to.eql('refresh token!'); + it('returns a rejecting promise', function(done) { + this.authenticator.restore({ access_token: 'secret token!', expires_at: 1 }).then(null, function() { + expect(true).to.be.true; done(); }); }); + }); + }); + }); - afterEach(function() { - Ember.run.later.restore(); + describe('when the data does not include expiration data', function() { + describe('when the data contains an access_token', function() { + it('resolves with the correct data', function(done) { + this.authenticator.restore({ access_token: 'secret token!' }).then(function(data) { + expect(data).to.eql({ access_token: 'secret token!' }); + done(); }); }); + }); - describe('when automatic token refreshing is disabled', function() { - beforeEach(function() { - this.authenticator.set('refreshAccessTokens', false); - sinon.spy(Ember.run, 'later'); + describe('when the data does not contain an access_token', function() { + it('returns a rejecting promise', function(done) { + this.authenticator.restore().then(null, function() { + expect(true).to.be.true; + done(); }); + }); + }); + }); - it('does not schedule a token refresh', function(done) { - var _this = this; + describe('when automatic token refreshing is enabled', function() { + beforeEach(function() { + sinon.spy(Ember.run, 'later'); + }); - this.authenticator.restore({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }).then(function(data) { - expect(Ember.run.later).to.not.have.been.called; - done(); - }); - }); + it('schedules a token refresh', function(done) { + var _this = this; - afterEach(function() { - Ember.run.later.restore(); - }); + this.authenticator.restore({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }).then(function(data) { + var spyCall = Ember.run.later.getCall(0); + + expect(spyCall.args[1]).to.eql(_this.authenticator.refreshAccessToken); + expect(spyCall.args[2]).to.eql(12345); + expect(spyCall.args[3]).to.eql('refresh token!'); + done(); }); }); + + afterEach(function() { + Ember.run.later.restore(); + }); }); - describe('when the data does not contain an access_token', function() { - it('returns a rejecting promise', function(done) { - this.authenticator.restore().then(null, function() { - expect(true).to.be.true; + describe('when automatic token refreshing is disabled', function() { + beforeEach(function() { + this.authenticator.set('refreshAccessTokens', false); + sinon.spy(Ember.run, 'later'); + }); + + it('does not schedule a token refresh', function(done) { + var _this = this; + + this.authenticator.restore({ access_token: 'secret token!', expires_in: 12345, refresh_token: 'refresh token!' }).then(function(data) { + expect(Ember.run.later).to.not.have.been.called; done(); }); }); + + afterEach(function() { + Ember.run.later.restore(); + }); }); }); From 048ba366bc2e9d5f209869f362f799f376a8be80 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Tue, 22 Jul 2014 11:18:50 +0200 Subject: [PATCH 20/41] always send the events to the initial transition as that delegates to the router anyway which is what we want - no need to check for whether that transition was successful or not etc. --- .../mixins/application-route-mixin.js | 3 +-- .../mixins/application-route-mixin-test.js | 25 +++++-------------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js index 90b5eebaa..0771db679 100644 --- a/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js +++ b/packages/ember-simple-auth/lib/simple-auth/mixins/application-route-mixin.js @@ -78,8 +78,7 @@ export default Ember.Mixin.create({ ]).forEach(function(event) { _this.get(Configuration.sessionPropertyName).on(event, function(error) { Array.prototype.unshift.call(arguments, event); - var target = transition.isActive ? transition : _this; - target.send.apply(target, arguments); + transition.send.apply(transition, arguments); }); }); } diff --git a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js index de494fec4..aa4c58638 100644 --- a/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/mixins/application-route-mixin-test.js @@ -18,7 +18,6 @@ describe('ApplicationRouteMixin', function() { describe('#beforeModel', function() { beforeEach(function() { this.transition = { send: function() {}, isActive: false }; - sinon.spy(this.route, 'send'); sinon.spy(this.transition, 'send'); this.route.beforeModel(this.transition); }); @@ -27,7 +26,7 @@ describe('ApplicationRouteMixin', function() { this.session.trigger('sessionAuthenticationSucceeded'); Ember.run.next(this, function() { - expect(this.route.send).to.have.been.calledWith('sessionAuthenticationSucceeded'); + expect(this.transition.send).to.have.been.calledWith('sessionAuthenticationSucceeded'); done(); }); }); @@ -36,7 +35,7 @@ describe('ApplicationRouteMixin', function() { this.session.trigger('sessionAuthenticationFailed', 'error'); Ember.run.next(this, function() { - expect(this.route.send).to.have.been.calledWith('sessionAuthenticationFailed', 'error'); + expect(this.transition.send).to.have.been.calledWith('sessionAuthenticationFailed', 'error'); done(); }); }); @@ -45,7 +44,7 @@ describe('ApplicationRouteMixin', function() { this.session.trigger('sessionInvalidationSucceeded'); Ember.run.next(this, function() { - expect(this.route.send).to.have.been.calledWith('sessionInvalidationSucceeded'); + expect(this.transition.send).to.have.been.calledWith('sessionInvalidationSucceeded'); done(); }); }); @@ -54,7 +53,7 @@ describe('ApplicationRouteMixin', function() { this.session.trigger('sessionInvalidationFailed', 'error'); Ember.run.next(this, function() { - expect(this.route.send).to.have.been.calledWith('sessionInvalidationFailed', 'error'); + expect(this.transition.send).to.have.been.calledWith('sessionInvalidationFailed', 'error'); done(); }); }); @@ -63,7 +62,7 @@ describe('ApplicationRouteMixin', function() { this.session.trigger('authorizationFailed'); Ember.run.next(this, function() { - expect(this.route.send).to.have.been.calledWith('authorizationFailed'); + expect(this.transition.send).to.have.been.calledWith('authorizationFailed'); done(); }); }); @@ -73,22 +72,10 @@ describe('ApplicationRouteMixin', function() { this.session.trigger('sessionAuthenticationSucceeded'); Ember.run.next(this, function() { - expect(this.route.send).to.have.been.calledOnce; + expect(this.transition.send).to.have.been.calledOnce; done(); }); }); - - describe('when the initial transition is still active', function() { - it('invokes the action on the transition', function(done) { - this.transition.isActive = true; - this.session.trigger('sessionAuthenticationSucceeded'); - - Ember.run.next(this, function() { - expect(this.transition.send).to.have.been.calledWith('sessionAuthenticationSucceeded'); - done(); - }); - }); - }); }); describe('the "authenticateSession" action', function() { From fd04098190fa61074909e816568bb41f98bfaf1b Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Tue, 22 Jul 2014 11:53:27 +0200 Subject: [PATCH 21/41] try to get Ember via require in AMD distribution also includes better library registration that also works in the AMD build --- Gruntfile.js | 6 +++--- packages/ember-simple-auth-cookie-store/wrap/amd.end | 1 + packages/ember-simple-auth-cookie-store/wrap/amd.start | 7 +++++++ packages/ember-simple-auth-cookie-store/wrap/browser.end | 2 -- .../ember-simple-auth-cookie-store/wrap/register-library | 1 + packages/ember-simple-auth-devise/wrap/amd.end | 1 + packages/ember-simple-auth-devise/wrap/amd.start | 7 +++++++ packages/ember-simple-auth-devise/wrap/browser.end | 2 -- packages/ember-simple-auth-devise/wrap/register-library | 1 + packages/ember-simple-auth-oauth2/wrap/amd.end | 1 + packages/ember-simple-auth-oauth2/wrap/amd.start | 7 +++++++ packages/ember-simple-auth-oauth2/wrap/browser.end | 2 -- packages/ember-simple-auth-oauth2/wrap/register-library | 1 + packages/ember-simple-auth-torii/wrap/amd.end | 1 + packages/ember-simple-auth-torii/wrap/amd.start | 7 +++++++ packages/ember-simple-auth-torii/wrap/browser.end | 2 -- packages/ember-simple-auth-torii/wrap/register-library | 1 + packages/ember-simple-auth/wrap/amd.end | 1 + packages/ember-simple-auth/wrap/amd.start | 7 +++++++ packages/ember-simple-auth/wrap/browser.end | 2 -- packages/ember-simple-auth/wrap/register-library | 1 + 21 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 packages/ember-simple-auth-cookie-store/wrap/amd.end create mode 100644 packages/ember-simple-auth-cookie-store/wrap/amd.start create mode 100644 packages/ember-simple-auth-cookie-store/wrap/register-library create mode 100644 packages/ember-simple-auth-devise/wrap/amd.end create mode 100644 packages/ember-simple-auth-devise/wrap/amd.start create mode 100644 packages/ember-simple-auth-devise/wrap/register-library create mode 100644 packages/ember-simple-auth-oauth2/wrap/amd.end create mode 100644 packages/ember-simple-auth-oauth2/wrap/amd.start create mode 100644 packages/ember-simple-auth-oauth2/wrap/register-library create mode 100644 packages/ember-simple-auth-torii/wrap/amd.end create mode 100644 packages/ember-simple-auth-torii/wrap/amd.start create mode 100644 packages/ember-simple-auth-torii/wrap/register-library create mode 100644 packages/ember-simple-auth/wrap/amd.end create mode 100644 packages/ember-simple-auth/wrap/amd.start create mode 100644 packages/ember-simple-auth/wrap/register-library diff --git a/Gruntfile.js b/Gruntfile.js index 41ff6802a..3ba6ca1ba 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -125,7 +125,7 @@ module.exports = function(grunt) { files: (function() { var files = {}; packages.forEach(function(pkg) { - files['tmp/' + pkg.name + '.amd.js'] = ['tmp/libs/' + pkg.main + '.js', 'tmp/libs/' + pkg.main + '/**/*.js']; + files['tmp/' + pkg.name + '.amd.js'] = ['packages/' + pkg.name + '/wrap/amd.start', 'packages/' + pkg.name + '/wrap/register-library', 'tmp/libs/' + pkg.main + '.js', 'tmp/libs/' + pkg.main + '/**/*.js', 'packages/' + pkg.name + '/wrap/amd.end']; }); return files; })() @@ -134,7 +134,7 @@ module.exports = function(grunt) { files: (function() { var files = {}; packages.forEach(function(pkg) { - files['tmp/' + pkg.name + '.js'] = ['packages/' + pkg.name + '/wrap/browser.start', 'vendor/loader.js', 'tmp/libs/' + pkg.main + '.js', 'tmp/libs/' + pkg.main + '/**/*.js', 'packages/' + pkg.name + '/wrap/browser.end']; + files['tmp/' + pkg.name + '.js'] = ['packages/' + pkg.name + '/wrap/browser.start', 'packages/' + pkg.name + '/wrap/register-library', 'vendor/loader.js', 'tmp/libs/' + pkg.main + '.js', 'tmp/libs/' + pkg.main + '/**/*.js', 'packages/' + pkg.name + '/wrap/browser.end']; }); return files; })() @@ -143,7 +143,7 @@ module.exports = function(grunt) { files: (function() { var files = {}; packages.forEach(function(pkg) { - files['tmp/' + pkg.name + '-tests.amd.js'] = ['tmp/tests/' + pkg.name + '/**/*.js']; + files['tmp/' + pkg.name + '-tests.amd.js'] = ['packages/' + pkg.name + '/wrap/amd.start', 'packages/' + pkg.name + '/wrap/register-library', 'tmp/tests/' + pkg.name + '/**/*.js', 'packages/' + pkg.name + '/wrap/amd.end']; }); return files; })() diff --git a/packages/ember-simple-auth-cookie-store/wrap/amd.end b/packages/ember-simple-auth-cookie-store/wrap/amd.end new file mode 100644 index 000000000..b45c74a14 --- /dev/null +++ b/packages/ember-simple-auth-cookie-store/wrap/amd.end @@ -0,0 +1 @@ +})((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-cookie-store/wrap/amd.start b/packages/ember-simple-auth-cookie-store/wrap/amd.start new file mode 100644 index 000000000..9fc36ee5e --- /dev/null +++ b/packages/ember-simple-auth-cookie-store/wrap/amd.start @@ -0,0 +1,7 @@ +(function(global) { + var define = global.define; + var require = global.require; + var Ember = global.Ember; + if (typeof Ember === 'undefined' && typeof require !== 'undefined') { + Ember = require('ember'); + } diff --git a/packages/ember-simple-auth-cookie-store/wrap/browser.end b/packages/ember-simple-auth-cookie-store/wrap/browser.end index bab55d177..1143fec7b 100644 --- a/packages/ember-simple-auth-cookie-store/wrap/browser.end +++ b/packages/ember-simple-auth-cookie-store/wrap/browser.end @@ -14,6 +14,4 @@ var Cookie = requireModule('simple-auth-cookie-store/stores/cookie').defaul global.SimpleAuth.Stores.Cookie = Cookie; requireModule('simple-auth-cookie-store/ember'); - -Ember.libraries.register('Ember Simple Auth Cookie Store', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-cookie-store/wrap/register-library b/packages/ember-simple-auth-cookie-store/wrap/register-library new file mode 100644 index 000000000..5b8c4d17a --- /dev/null +++ b/packages/ember-simple-auth-cookie-store/wrap/register-library @@ -0,0 +1 @@ +Ember.libraries.register('Ember Simple Auth Cookie Store', '{{ VERSION }}'); diff --git a/packages/ember-simple-auth-devise/wrap/amd.end b/packages/ember-simple-auth-devise/wrap/amd.end new file mode 100644 index 000000000..b45c74a14 --- /dev/null +++ b/packages/ember-simple-auth-devise/wrap/amd.end @@ -0,0 +1 @@ +})((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-devise/wrap/amd.start b/packages/ember-simple-auth-devise/wrap/amd.start new file mode 100644 index 000000000..9fc36ee5e --- /dev/null +++ b/packages/ember-simple-auth-devise/wrap/amd.start @@ -0,0 +1,7 @@ +(function(global) { + var define = global.define; + var require = global.require; + var Ember = global.Ember; + if (typeof Ember === 'undefined' && typeof require !== 'undefined') { + Ember = require('ember'); + } diff --git a/packages/ember-simple-auth-devise/wrap/browser.end b/packages/ember-simple-auth-devise/wrap/browser.end index 1a068bf92..776e77555 100644 --- a/packages/ember-simple-auth-devise/wrap/browser.end +++ b/packages/ember-simple-auth-devise/wrap/browser.end @@ -19,6 +19,4 @@ global.SimpleAuth.Authenticators.Devise = Authenticator; global.SimpleAuth.Authorizers.Devise = Authorizer; requireModule('simple-auth-devise/ember'); - -Ember.libraries.register('Ember Simple Auth Devise', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-devise/wrap/register-library b/packages/ember-simple-auth-devise/wrap/register-library new file mode 100644 index 000000000..a1bc98a04 --- /dev/null +++ b/packages/ember-simple-auth-devise/wrap/register-library @@ -0,0 +1 @@ +Ember.libraries.register('Ember Simple Auth Devise', '{{ VERSION }}'); diff --git a/packages/ember-simple-auth-oauth2/wrap/amd.end b/packages/ember-simple-auth-oauth2/wrap/amd.end new file mode 100644 index 000000000..b45c74a14 --- /dev/null +++ b/packages/ember-simple-auth-oauth2/wrap/amd.end @@ -0,0 +1 @@ +})((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-oauth2/wrap/amd.start b/packages/ember-simple-auth-oauth2/wrap/amd.start new file mode 100644 index 000000000..9fc36ee5e --- /dev/null +++ b/packages/ember-simple-auth-oauth2/wrap/amd.start @@ -0,0 +1,7 @@ +(function(global) { + var define = global.define; + var require = global.require; + var Ember = global.Ember; + if (typeof Ember === 'undefined' && typeof require !== 'undefined') { + Ember = require('ember'); + } diff --git a/packages/ember-simple-auth-oauth2/wrap/browser.end b/packages/ember-simple-auth-oauth2/wrap/browser.end index 7a74e1c87..7e66338d2 100644 --- a/packages/ember-simple-auth-oauth2/wrap/browser.end +++ b/packages/ember-simple-auth-oauth2/wrap/browser.end @@ -19,6 +19,4 @@ global.SimpleAuth.Authenticators.OAuth2 = Authenticator; global.SimpleAuth.Authorizers.OAuth2 = Authorizer; requireModule('simple-auth-oauth2/ember'); - -Ember.libraries.register('Ember Simple Auth OAuth 2.0', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-oauth2/wrap/register-library b/packages/ember-simple-auth-oauth2/wrap/register-library new file mode 100644 index 000000000..607102603 --- /dev/null +++ b/packages/ember-simple-auth-oauth2/wrap/register-library @@ -0,0 +1 @@ +Ember.libraries.register('Ember Simple Auth OAuth 2.0', '{{ VERSION }}'); diff --git a/packages/ember-simple-auth-torii/wrap/amd.end b/packages/ember-simple-auth-torii/wrap/amd.end new file mode 100644 index 000000000..b45c74a14 --- /dev/null +++ b/packages/ember-simple-auth-torii/wrap/amd.end @@ -0,0 +1 @@ +})((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-torii/wrap/amd.start b/packages/ember-simple-auth-torii/wrap/amd.start new file mode 100644 index 000000000..9fc36ee5e --- /dev/null +++ b/packages/ember-simple-auth-torii/wrap/amd.start @@ -0,0 +1,7 @@ +(function(global) { + var define = global.define; + var require = global.require; + var Ember = global.Ember; + if (typeof Ember === 'undefined' && typeof require !== 'undefined') { + Ember = require('ember'); + } diff --git a/packages/ember-simple-auth-torii/wrap/browser.end b/packages/ember-simple-auth-torii/wrap/browser.end index 72c0a08a4..5e688026d 100644 --- a/packages/ember-simple-auth-torii/wrap/browser.end +++ b/packages/ember-simple-auth-torii/wrap/browser.end @@ -7,6 +7,4 @@ var Authenticator = requireModule('simple-auth-torii/authenticators/torii').defa global.SimpleAuth.Authenticators.Torii = Authenticator; requireModule('simple-auth-torii/ember'); - -Ember.libraries.register('Ember Simple Auth Torii', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-torii/wrap/register-library b/packages/ember-simple-auth-torii/wrap/register-library new file mode 100644 index 000000000..8b3c5dd7c --- /dev/null +++ b/packages/ember-simple-auth-torii/wrap/register-library @@ -0,0 +1 @@ +Ember.libraries.register('Ember Simple Auth Torii', '{{ VERSION }}'); diff --git a/packages/ember-simple-auth/wrap/amd.end b/packages/ember-simple-auth/wrap/amd.end new file mode 100644 index 000000000..b45c74a14 --- /dev/null +++ b/packages/ember-simple-auth/wrap/amd.end @@ -0,0 +1 @@ +})((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth/wrap/amd.start b/packages/ember-simple-auth/wrap/amd.start new file mode 100644 index 000000000..9fc36ee5e --- /dev/null +++ b/packages/ember-simple-auth/wrap/amd.start @@ -0,0 +1,7 @@ +(function(global) { + var define = global.define; + var require = global.require; + var Ember = global.Ember; + if (typeof Ember === 'undefined' && typeof require !== 'undefined') { + Ember = require('ember'); + } diff --git a/packages/ember-simple-auth/wrap/browser.end b/packages/ember-simple-auth/wrap/browser.end index 092e5e3ca..a6cd57207 100644 --- a/packages/ember-simple-auth/wrap/browser.end +++ b/packages/ember-simple-auth/wrap/browser.end @@ -51,6 +51,4 @@ if (global.Ember.testing) { requireModule('simple-auth/test-helpers/authenticate-session'); requireModule('simple-auth/test-helpers/invalidate-session'); } - -Ember.libraries.register('Ember Simple Auth', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth/wrap/register-library b/packages/ember-simple-auth/wrap/register-library new file mode 100644 index 000000000..ef7be3fe3 --- /dev/null +++ b/packages/ember-simple-auth/wrap/register-library @@ -0,0 +1 @@ +Ember.libraries.register('Ember Simple Auth', '{{ VERSION }}'); From d363fcca7e310ec0e60b950dfe0a897f15a13d7d Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Tue, 22 Jul 2014 13:30:33 +0200 Subject: [PATCH 22/41] update store when session content changes --- .../lib/simple-auth/session.js | 27 +++++++++++++- .../tests/simple-auth/session-test.js | 37 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/packages/ember-simple-auth/lib/simple-auth/session.js b/packages/ember-simple-auth/lib/simple-auth/session.js index d6230cdda..13a370134 100644 --- a/packages/ember-simple-auth/lib/simple-auth/session.js +++ b/packages/ember-simple-auth/lib/simple-auth/session.js @@ -244,8 +244,7 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { content: content }); this.bindToAuthenticatorEvents(); - var data = Ember.$.extend({ authenticator: authenticator }, this.content); - this.store.persist(data); + this.updateStore(); this.endPropertyChanges(); if (trigger) { this.trigger('sessionAuthenticationSucceeded'); @@ -271,6 +270,30 @@ export default Ember.ObjectProxy.extend(Ember.Evented, { } }, + /** + @method setUnknownProperty + @private + */ + setUnknownProperty: function(key, value) { + var result = this._super(key, value); + this.updateStore(); + return result; + }, + + /** + @method updateStore + @private + */ + updateStore: function() { + var data = this.content; + if (!Ember.isEmpty(this.authenticator)) { + data = Ember.$.extend({ authenticator: this.authenticator }, data); + } + if (!Ember.isEmpty(data)) { + this.store.persist(data); + } + }, + /** @method bindToAuthenticatorEvents @private diff --git a/packages/ember-simple-auth/tests/simple-auth/session-test.js b/packages/ember-simple-auth/tests/simple-auth/session-test.js index 134211659..339d55149 100644 --- a/packages/ember-simple-auth/tests/simple-auth/session-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/session-test.js @@ -505,6 +505,43 @@ describe('Session', function() { }); }); + describe("when the session's content changes", function() { + describe('when a single property is set', function() { + beforeEach(function() { + this.session = Session.create({ store: this.store }); + this.session.set('some', 'property'); + }); + + it('persists its content in the store', function(done) { + Ember.run.next(this, function() { + var properties = this.store.restore(); + delete properties.authenticator; + + expect(properties).to.eql({ some: 'property' }); + done(); + }); + }); + }); + + describe('when multiple properties are set at once', function() { + beforeEach(function() { + this.session = Session.create({ store: this.store }); + this.session.set('some', 'property'); + this.session.setProperties({ multiple: 'properties' }); + }); + + it('persists its content in the store', function(done) { + Ember.run.next(this, function() { + var properties = this.store.restore(); + delete properties.authenticator; + + expect(properties).to.eql({ some: 'property', multiple: 'properties' }); + done(); + }); + }); + }); + }); + describe('when the store triggers the "updated" event', function() { beforeEach(function() { this.session = Session.create(); From 89b0f3c8f6965b870586b48b7b071ca0915cafd5 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Tue, 22 Jul 2014 15:19:31 +0200 Subject: [PATCH 23/41] improved token refresh --- examples/3-token-refresh.html | 2 +- .../lib/simple-auth-oauth2/authenticators/oauth2.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/3-token-refresh.html b/examples/3-token-refresh.html index 93c1c3b9c..a0cd6b5e0 100644 --- a/examples/3-token-refresh.html +++ b/examples/3-token-refresh.html @@ -48,7 +48,7 @@

Example with token refresh

{{#if session.isAuthenticated}}
- The token is automatically refreshed every 10 seconds, it currently is: {{ session.access_token }}. + The token is automatically refreshed every 5 to 10 seconds, it currently is: {{ session.access_token }}.
{{else}}
diff --git a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js index 010a455b0..b68491ff7 100644 --- a/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js +++ b/packages/ember-simple-auth-oauth2/lib/simple-auth-oauth2/authenticators/oauth2.js @@ -251,7 +251,7 @@ export default Base.extend({ if (Ember.isEmpty(expiresAt) && !Ember.isEmpty(expiresIn)) { expiresAt = new Date(now + expiresIn * 1000).getTime(); } - var offset = (Math.floor(Math.random() * 15) + 5) * 1000; + var offset = (Math.floor(Math.random() * 5) + 5) * 1000; if (!Ember.isEmpty(refreshToken) && !Ember.isEmpty(expiresAt) && expiresAt > now - offset) { Ember.run.cancel(this._refreshTokenTimeout); delete this._refreshTokenTimeout; @@ -293,7 +293,7 @@ export default Base.extend({ */ absolutizeExpirationTime: function(expiresIn) { if (!Ember.isEmpty(expiresIn)) { - return new Date((new Date().getTime()) + (expiresIn - 5) * 1000).getTime(); + return new Date((new Date().getTime()) + expiresIn * 1000).getTime(); } } }); From 30ca3af43027fb1c31e7533f6face27bc562ac81 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Wed, 23 Jul 2014 13:55:44 +0200 Subject: [PATCH 24/41] added testing package --- packages/ember-simple-auth-testing/README.md | 68 +++++++++++++++++++ .../authenticators/test.js | 2 +- .../lib/simple-auth-testing/initializer.js | 12 ++++ .../lib/simple-auth-testing/test-helpers.js | 22 ++++++ .../ember-simple-auth-testing/package.json | 8 +++ .../authenticators/test-test.js | 2 +- .../wrap/browser.end | 11 +++ .../wrap/browser.start | 1 + .../lib/simple-auth/setup.js | 4 -- .../test-helpers/authenticate-session.js | 10 --- .../test-helpers/invalidate-session.js | 12 ---- .../tests/simple-auth/setup-test.js | 27 -------- packages/ember-simple-auth/wrap/browser.end | 5 -- test/index.html | 2 + 14 files changed, 126 insertions(+), 60 deletions(-) create mode 100644 packages/ember-simple-auth-testing/README.md rename packages/{ember-simple-auth/lib/simple-auth => ember-simple-auth-testing/lib/simple-auth-testing}/authenticators/test.js (83%) create mode 100644 packages/ember-simple-auth-testing/lib/simple-auth-testing/initializer.js create mode 100644 packages/ember-simple-auth-testing/lib/simple-auth-testing/test-helpers.js create mode 100644 packages/ember-simple-auth-testing/package.json rename packages/{ember-simple-auth/tests/simple-auth => ember-simple-auth-testing/tests/simple-auth-testing}/authenticators/test-test.js (92%) create mode 100644 packages/ember-simple-auth-testing/wrap/browser.end create mode 100644 packages/ember-simple-auth-testing/wrap/browser.start delete mode 100644 packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js delete mode 100644 packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js diff --git a/packages/ember-simple-auth-testing/README.md b/packages/ember-simple-auth-testing/README.md new file mode 100644 index 000000000..418d5ac7c --- /dev/null +++ b/packages/ember-simple-auth-testing/README.md @@ -0,0 +1,68 @@ +# Ember Simple Auth Testing + +This is an extension to the Ember Simple Auth library that provides test +helpers to authenticate and invalidate the session without having to stub +server responses etc. + +## The Helpers + +To authenticate the session, use the `authenticateSession` helper, e.g.: + +```js +test('a protected route is accessible when the session is authenticated', function() { + expect(1); + authenticateSession(); + visit('/protected'); + + andThen(function() { + equal(currentRouteName(), 'protected'); + }); +}); +``` + +and to invalidate the session, use the `invaldiateSession` helper, e.g.: + +```js +test('a protected route is not accessible when the session is not authenticated', function() { + expect(1); + invalidateSession(); + visit('/protected'); + + andThen(function() { + notEqual(currentRouteName(), 'protected'); + }); +}); +``` + +## Installation + +To install Ember Simple Auth Testing in an Ember.js application there are +several options: + +* If you're using [Ember CLI](https://github.com/stefanpenner/ember-cli), just + add the + [Ember CLI Addon](https://github.com/simplabs/ember-cli-simple-auth-testing) + to your project. Also import the test helpers in `tests/helpers/start-app.js` + before the export: + + ```js + … + import 'simple-auth-testing/test-helpers'; + + export default function startApp(attrs) { + … + ``` + +* The Ember Simple Auth testing extension library is also included in the + _"ember-simple-auth"_ bower package both in a browserified version as well as + an AMD build. If you're using the AMD build from bower be sure to require the + test helpers explicitly: + + ```js + require('simple-auth-testing/test-helpers'); + ``` + + The browserified version will automatically register the test helpers once it + is loaded in the application. +* Download a prebuilt version from + [the releases page](https://github.com/simplabs/ember-simple-auth/releases) diff --git a/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js b/packages/ember-simple-auth-testing/lib/simple-auth-testing/authenticators/test.js similarity index 83% rename from packages/ember-simple-auth/lib/simple-auth/authenticators/test.js rename to packages/ember-simple-auth-testing/lib/simple-auth-testing/authenticators/test.js index 884683290..bc9d54c22 100644 --- a/packages/ember-simple-auth/lib/simple-auth/authenticators/test.js +++ b/packages/ember-simple-auth-testing/lib/simple-auth-testing/authenticators/test.js @@ -1,4 +1,4 @@ -import Base from './base'; +import Base from 'simple-auth/authenticators/base'; export default Base.extend({ restore: function(data) { diff --git a/packages/ember-simple-auth-testing/lib/simple-auth-testing/initializer.js b/packages/ember-simple-auth-testing/lib/simple-auth-testing/initializer.js new file mode 100644 index 000000000..0fe2e9674 --- /dev/null +++ b/packages/ember-simple-auth-testing/lib/simple-auth-testing/initializer.js @@ -0,0 +1,12 @@ +var global = (typeof window !== 'undefined') ? window : {}, + Ember = global.Ember; + +import TestAuthenticator from 'simple-auth-testing/authenticators/test'; + +export default { + name: 'simple-auth-testing', + before: 'simple-auth', + initialize: function(container, application) { + container.register('simple-auth-authenticator:test', TestAuthenticator); + } +}; diff --git a/packages/ember-simple-auth-testing/lib/simple-auth-testing/test-helpers.js b/packages/ember-simple-auth-testing/lib/simple-auth-testing/test-helpers.js new file mode 100644 index 000000000..647f2ad81 --- /dev/null +++ b/packages/ember-simple-auth-testing/lib/simple-auth-testing/test-helpers.js @@ -0,0 +1,22 @@ +var global = (typeof window !== 'undefined') ? window : {}, + Ember = global.Ember; + +import Configuration from '../configuration'; + +var testHelpers = function() { + Ember.Test.registerAsyncHelper('authenticateSession', function(app) { + var session = app.__container__.lookup(Configuration.session); + session.authenticate('simple-auth-authenticator:test'); + return wait(); + }); + + Ember.Test.registerAsyncHelper('invalidateSession', function(app) { + var session = app.__container__.lookup(Configuration.session); + if (session.get('isAuthenticated')) { + session.invalidate(); + } + return wait(); + }); +}(); + +export default testHelpers; diff --git a/packages/ember-simple-auth-testing/package.json b/packages/ember-simple-auth-testing/package.json new file mode 100644 index 000000000..8f0b35fbc --- /dev/null +++ b/packages/ember-simple-auth-testing/package.json @@ -0,0 +1,8 @@ +{ + "name": "ember-simple-auth-testing", + "main": "simple-auth-testing", + "version": "0.6.3", + "description": "Test helpers for Ember Simple Auth to authenticate and invalidate the session.", + "author": "Marco Otte-Witte", + "license": "MIT" +} diff --git a/packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js b/packages/ember-simple-auth-testing/tests/simple-auth-testing/authenticators/test-test.js similarity index 92% rename from packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js rename to packages/ember-simple-auth-testing/tests/simple-auth-testing/authenticators/test-test.js index a17f011c4..7612c2c18 100644 --- a/packages/ember-simple-auth/tests/simple-auth/authenticators/test-test.js +++ b/packages/ember-simple-auth-testing/tests/simple-auth-testing/authenticators/test-test.js @@ -1,4 +1,4 @@ -import Test from 'simple-auth/authenticators/test'; +import Test from 'simple-auth-testing/authenticators/test'; describe('Authenticators.Test', function() { beforeEach(function() { diff --git a/packages/ember-simple-auth-testing/wrap/browser.end b/packages/ember-simple-auth-testing/wrap/browser.end new file mode 100644 index 000000000..338049622 --- /dev/null +++ b/packages/ember-simple-auth-testing/wrap/browser.end @@ -0,0 +1,11 @@ +define('simple-auth/authenticators/base', ['exports'], function(__exports__) { + __exports__['default'] = global.SimpleAuth.Authenticators.Base; +}); + +if (global.Ember.testing) { + requireModule('simple-auth/test-helpers/authenticate-session'); + requireModule('simple-auth/test-helpers/invalidate-session'); +} + +Ember.libraries.register('Ember Simple Auth Testing', '{{ VERSION }}'); +})((typeof global !== 'undefined') ? global : window); diff --git a/packages/ember-simple-auth-testing/wrap/browser.start b/packages/ember-simple-auth-testing/wrap/browser.start new file mode 100644 index 000000000..eb8107aea --- /dev/null +++ b/packages/ember-simple-auth-testing/wrap/browser.start @@ -0,0 +1 @@ +(function(global) { diff --git a/packages/ember-simple-auth/lib/simple-auth/setup.js b/packages/ember-simple-auth/lib/simple-auth/setup.js index a701cf758..9ce1dba11 100644 --- a/packages/ember-simple-auth/lib/simple-auth/setup.js +++ b/packages/ember-simple-auth/lib/simple-auth/setup.js @@ -2,7 +2,6 @@ import Configuration from './configuration'; import Session from './session'; import LocalStorage from './stores/local-storage'; import Ephemeral from './stores/ephemeral'; -import TestAuthenticator from 'simple-auth/authenticators/test'; function extractLocationOrigin(location) { if (Ember.typeOf(location) === 'string') { @@ -36,9 +35,6 @@ function registerFactories(container) { container.register('simple-auth-session-store:local-storage', LocalStorage); container.register('simple-auth-session-store:ephemeral', Ephemeral); container.register('simple-auth-session:main', Session); - if (Ember.testing) { - container.register('simple-auth-authenticator:test', TestAuthenticator); - } } /** diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js deleted file mode 100644 index 6760bb2fa..000000000 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/authenticate-session.js +++ /dev/null @@ -1,10 +0,0 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - -import Configuration from '../configuration'; - -export default Ember.Test.registerAsyncHelper('authenticateSession', function(app) { - var session = app.__container__.lookup(Configuration.session); - session.authenticate('simple-auth-authenticator:test'); - return wait(); -}); diff --git a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js b/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js deleted file mode 100644 index df016cf7d..000000000 --- a/packages/ember-simple-auth/lib/simple-auth/test-helpers/invalidate-session.js +++ /dev/null @@ -1,12 +0,0 @@ -var global = (typeof window !== 'undefined') ? window : {}, - Ember = global.Ember; - -import Configuration from '../configuration'; - -export default Ember.Test.registerAsyncHelper('invalidateSession', function(app) { - var session = app.__container__.lookup(Configuration.session); - if (session.get('isAuthenticated')) { - session.invalidate(); - } - return wait(); -}); diff --git a/packages/ember-simple-auth/tests/simple-auth/setup-test.js b/packages/ember-simple-auth/tests/simple-auth/setup-test.js index c2b0c716a..30b477e6b 100644 --- a/packages/ember-simple-auth/tests/simple-auth/setup-test.js +++ b/packages/ember-simple-auth/tests/simple-auth/setup-test.js @@ -3,7 +3,6 @@ import Configuration from 'simple-auth/configuration'; import Session from 'simple-auth/session'; import LocalStorageStore from 'simple-auth/stores/local-storage'; import EphemeralStore from 'simple-auth/stores/ephemeral'; -import TestAuthenticator from 'simple-auth/authenticators/test'; describe('setup', function() { beforeEach(function() { @@ -53,32 +52,6 @@ describe('setup', function() { expect(this.container.register).to.have.been.calledWith('simple-auth-session:main', Session); }); - describe('when Ember.testing is true', function() { - beforeEach(function() { - Ember.testing = true; - }); - - it('registers the Test authenticator', function() { - sinon.spy(this.container, 'register'); - setup(this.container, this.application); - - expect(this.container.register).to.have.been.calledWith('simple-auth-authenticator:test', TestAuthenticator); - }); - }); - - describe('when Ember.testing is false', function() { - beforeEach(function() { - Ember.testing = false; - }); - - it('registers the Test authenticator', function() { - sinon.spy(this.container, 'register'); - setup(this.container, this.application); - - expect(this.container.register).to.not.have.been.calledWith('simple-auth-authenticator:test', TestAuthenticator); - }); - }); - describe('the session instance', function() { beforeEach(function() { Configuration.store = 'simple-auth-session-store:local-storage'; diff --git a/packages/ember-simple-auth/wrap/browser.end b/packages/ember-simple-auth/wrap/browser.end index 092e5e3ca..a3c90db1c 100644 --- a/packages/ember-simple-auth/wrap/browser.end +++ b/packages/ember-simple-auth/wrap/browser.end @@ -47,10 +47,5 @@ global.SimpleAuth = { requireModule('simple-auth/ember'); -if (global.Ember.testing) { - requireModule('simple-auth/test-helpers/authenticate-session'); - requireModule('simple-auth/test-helpers/invalidate-session'); -} - Ember.libraries.register('Ember Simple Auth', '{{ VERSION }}'); })((typeof global !== 'undefined') ? global : window); diff --git a/test/index.html b/test/index.html index 6f7d2e71c..70d63be7b 100644 --- a/test/index.html +++ b/test/index.html @@ -20,11 +20,13 @@ + +