Skip to content

Commit

Permalink
change cookie default key names to be rfc2616 compliant (#978)
Browse files Browse the repository at this point in the history
fixes #977
  • Loading branch information
wzrdtales authored and marcoow committed May 13, 2016
1 parent 5e121d6 commit 103c2ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions addon/session-stores/adaptive.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default Base.extend({
@property localStorageKey
@type String
@default 'ember_simple_auth:session'
@default 'ember_simple_auth-session'
@public
*/
localStorageKey: 'ember_simple_auth:session',
localStorageKey: 'ember_simple_auth-session',

/**
The domain to use for the cookie if `localStorage` is not available, e.g.,
Expand All @@ -51,10 +51,10 @@ export default Base.extend({
@property cookieName
@type String
@default ember_simple_auth:session
@default ember_simple_auth-session
@public
*/
cookieName: 'ember_simple_auth:session',
cookieName: 'ember_simple_auth-session',

/**
The expiration time for the cookie in seconds if `localStorage` is not
Expand Down
10 changes: 5 additions & 5 deletions addon/session-stores/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export default BaseStore.extend({
@property cookieName
@type String
@default ember_simple_auth:session
@default ember_simple_auth-session
@public
*/
cookieName: 'ember_simple_auth:session',
cookieName: 'ember_simple_auth-session',

/**
The expiration time for the cookie in seconds. A value of `null` will make
Expand Down Expand Up @@ -148,7 +148,7 @@ export default BaseStore.extend({
},

_calculateExpirationTime() {
let cachedExpirationTime = this._read(`${this.cookieName}:expiration_time`);
let cachedExpirationTime = this._read(`${this.cookieName}-expiration_time`);
cachedExpirationTime = !!cachedExpirationTime ? new Date().getTime() + cachedExpirationTime * 1000 : null;
return !!this.cookieExpirationTime ? new Date().getTime() + this.cookieExpirationTime * 1000 : cachedExpirationTime;
},
Expand All @@ -160,8 +160,8 @@ export default BaseStore.extend({
let secure = !!this._secureCookies ? ';secure' : '';
document.cookie = `${this.cookieName}=${encodeURIComponent(value)}${domain}${path}${expires}${secure}`;
if (expiration !== null) {
let cachedExpirationTime = this._read(`${this.cookieName}:expiration_time`);
document.cookie = `${this.cookieName}:expiration_time=${encodeURIComponent(this.cookieExpirationTime || cachedExpirationTime)}${domain}${path}${expires}${secure}`;
let cachedExpirationTime = this._read(`${this.cookieName}-expiration_time`);
document.cookie = `${this.cookieName}-expiration_time=${encodeURIComponent(this.cookieExpirationTime || cachedExpirationTime)}${domain}${path}${expires}${secure}`;
}
},

Expand Down
4 changes: 2 additions & 2 deletions addon/session-stores/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default BaseStore.extend({
@property key
@type String
@default 'ember_simple_auth:session'
@default 'ember_simple_auth-session'
@public
*/
key: 'ember_simple_auth:session',
key: 'ember_simple_auth-session',

init() {
this._super(...arguments);
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/session-stores/shared/cookie-store-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ export default function(options) {

describe('#persist', () => {
it('respects the configured cookieName', () => {
store = createStore({ cookieName: 'test:session' });
store = createStore({ cookieName: 'test-session' });
store.persist({ key: 'value' });

expect(document.cookie).to.contain('test:session=%7B%22key%22%3A%22value%22%7D');
expect(document.cookie).to.contain('test-session=%7B%22key%22%3A%22value%22%7D');
});

it('respects the configured cookieDomain', () => {
store = createStore({ cookieDomain: 'example.com' });
store.persist({ key: 'value' });

expect(document.cookie).to.not.contain('test:session=%7B%22key%22%3A%22value%22%7D');
expect(document.cookie).to.not.contain('test-session=%7B%22key%22%3A%22value%22%7D');
});
});

describe('#renew', () => {
beforeEach(() => {
store = createStore({
cookieName: 'test:session',
cookieName: 'test-session',
cookieExpirationTime: 60,
expires: new Date().getTime() + store.cookieExpirationTime * 1000
});
store.persist({ key: 'value' });
renew(store);
});

it('stores the expiration time in a cookie named "test:session:expiration_time"', () => {
expect(document.cookie).to.contain(`${store.cookieName}:expiration_time=60`);
it('stores the expiration time in a cookie named "test-session-expiration_time"', () => {
expect(document.cookie).to.contain(`${store.cookieName}-expiration_time=60`);
});
});

Expand All @@ -65,7 +65,7 @@ export default function(options) {
});

it('is not triggered when the cookie has not actually changed', (done) => {
document.cookie = 'ember_simple_auth:session=%7B%22key%22%3A%22value%22%7D;path=/;';
document.cookie = 'ember_simple_auth-session=%7B%22key%22%3A%22value%22%7D;path=/;';
sync(store);

Ember.run.next(() => {
Expand All @@ -75,7 +75,7 @@ export default function(options) {
});

it('is triggered when the cookie changed', (done) => {
document.cookie = 'ember_simple_auth:session=%7B%22key%22%3A%22other%20value%22%7D;path=/;';
document.cookie = 'ember_simple_auth-session=%7B%22key%22%3A%22other%20value%22%7D;path=/;';
sync(store);

Ember.run.next(() => {
Expand Down

0 comments on commit 103c2ac

Please sign in to comment.