Skip to content

Commit f751300

Browse files
Merge pull request #2811 from Gaurav0/gaurav/feat/support_partitioned_cookies
feat: support partitioned cookies
2 parents 2898b40 + 73ea01b commit f751300

File tree

6 files changed

+99
-23
lines changed

6 files changed

+99
-23
lines changed

Diff for: packages/ember-simple-auth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@embroider/addon-shim": "^1.0.0",
3434
"@embroider/macros": "^1.0.0",
3535
"ember-cli-is-package-missing": "^1.0.0",
36-
"ember-cookies": "^1.0.0",
36+
"ember-cookies": "^1.2.0",
3737
"silent-error": "^1.0.0"
3838
},
3939
"devDependencies": {

Diff for: packages/ember-simple-auth/src/session-stores/adaptive.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ export default Base.extend({
8484
_sameSite: null,
8585
sameSite: proxyToInternalStore(),
8686

87+
/**
88+
Allows servers to assert that a cookie should opt in to partitioned storage,
89+
i.e. use a separate cookie per top level site if the cookie is used in a
90+
third party context
91+
92+
Available options:
93+
- null
94+
- true
95+
96+
@memberof AdaptiveStore
97+
@property partitioned
98+
@type Boolean
99+
@default null
100+
@public
101+
*/
102+
_partitioned: null,
103+
partitioned: proxyToInternalStore(),
104+
87105
/**
88106
The name of the cookie to use if `localStorage` is not available.
89107
@@ -158,7 +176,8 @@ export default Base.extend({
158176
'cookieName',
159177
'cookieExpirationTime',
160178
'cookiePath',
161-
'sameSite'
179+
'sameSite',
180+
'partitioned'
162181
);
163182

164183
cookieStorage.setProperties(options);

Diff for: packages/ember-simple-auth/src/session-stores/cookie.js

+19
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ export default BaseStore.extend({
160160
}
161161
}),
162162

163+
/**
164+
Allows servers to assert that a cookie should opt in to partitioned storage,
165+
i.e. use a separate cookie per top level site if the cookie is used in a
166+
third party context
167+
168+
Available options:
169+
- null
170+
- true
171+
172+
@memberof CookieStore
173+
@property partitioned
174+
@type Boolean
175+
@default null
176+
@public
177+
*/
178+
_partitioned: null,
179+
partitioned: persistingProperty(),
180+
163181
_cookies: service('cookies'),
164182

165183
_secureCookies() {
@@ -272,6 +290,7 @@ export default BaseStore.extend({
272290
path: this.get('cookiePath'),
273291
secure: this._secureCookies(),
274292
sameSite: this.get('sameSite'),
293+
partitioned: this.get('partitioned'),
275294
};
276295
if (this._oldCookieName) {
277296
A([this._oldCookieName, `${this._oldCookieName}-expiration_time`]).forEach(oldCookie => {

Diff for: packages/test-esa/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"ember-maybe-import-regenerator": "1.0.0",
5252
"ember-qunit": "7.0.0",
5353
"ember-resolver": "11.0.1",
54-
"ember-simple-auth": "6.1.0",
54+
"ember-simple-auth": "workspace:*",
5555
"ember-source": "5.12.0",
5656
"ember-source-channel-url": "3.0.0",
5757
"ember-try": "3.0.0",

Diff for: packages/test-esa/tests/unit/session-stores/shared/cookie-store-behavior.js

+46-18
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ export default function (options) {
4646
let cookieService = store.get('_cookies');
4747
await store.persist({ key: 'value' });
4848

49-
assert.ok(
49+
assert.true(
5050
cookieService.write.calledWith('test:session', JSON.stringify({ key: 'value' }), {
5151
domain: null,
5252
expires: null,
5353
path: '/',
5454
sameSite: null,
5555
secure: false,
56+
partitioned: null,
5657
})
5758
);
5859
});
@@ -65,13 +66,14 @@ export default function (options) {
6566
});
6667
await store.persist({ key: 'value' });
6768

68-
assert.ok(
69+
assert.true(
6970
cookieService.write.calledWith('session-cookie-domain', JSON.stringify({ key: 'value' }), {
7071
domain: 'example.com',
7172
expires: null,
7273
path: '/',
7374
sameSite: null,
7475
secure: false,
76+
partitioned: null,
7577
})
7678
);
7779
});
@@ -85,13 +87,14 @@ export default function (options) {
8587
let cookieService = store.get('_cookies');
8688
await store.persist({ key: 'value' });
8789

88-
assert.ok(
90+
assert.true(
8991
cookieService.write.calledWith('session-cookie-domain', JSON.stringify({ key: 'value' }), {
9092
domain: 'example.com',
9193
expires: null,
9294
path: '/hello-world',
9395
sameSite: null,
9496
secure: false,
97+
partitioned: null,
9598
})
9699
);
97100
});
@@ -104,17 +107,42 @@ export default function (options) {
104107
});
105108
let cookieService = store.get('_cookies');
106109
await store.persist({ key: 'value' });
107-
assert.ok(
110+
assert.true(
108111
cookieService.write.calledWith('session-cookie-domain', JSON.stringify({ key: 'value' }), {
109112
domain: 'example.com',
110113
expires: null,
111114
path: '/',
112115
sameSite: 'Strict',
113116
secure: false,
117+
partitioned: null,
114118
})
115119
);
116120
});
117121

122+
test('respects the configured partitioned', async function (assert) {
123+
run(() => {
124+
store.set('cookieName', 'session-cookie-partitioned');
125+
store.set('cookieDomain', 'example.com');
126+
store.set('partitioned', true);
127+
});
128+
let cookieService = store.get('_cookies');
129+
await store.persist({ key: 'value' });
130+
assert.true(
131+
cookieService.write.calledWith(
132+
'session-cookie-partitioned',
133+
JSON.stringify({ key: 'value' }),
134+
{
135+
domain: 'example.com',
136+
expires: null,
137+
path: '/',
138+
sameSite: null,
139+
secure: false,
140+
partitioned: true,
141+
}
142+
)
143+
);
144+
});
145+
118146
test('sends a warning when `cookieExpirationTime` is less than 90 seconds', async function (assert) {
119147
assert.expect(2);
120148
run(() => {
@@ -155,7 +183,7 @@ export default function (options) {
155183
});
156184

157185
test('stores the expiration time in a cookie named "test-session-expiration_time"', function (assert) {
158-
assert.ok(
186+
assert.true(
159187
cookieService.write.calledWith(
160188
'test-session-expiration_time',
161189
60,
@@ -212,7 +240,7 @@ export default function (options) {
212240
await new Promise(resolve => {
213241
next(() => {
214242
next(() => {
215-
assert.ok(triggered);
243+
assert.true(triggered);
216244
resolve();
217245
});
218246
});
@@ -264,11 +292,11 @@ export default function (options) {
264292
});
265293
await store.persist({ key: 'value' });
266294

267-
assert.ok(cookieService.clear.calledWith('session-foo'));
295+
assert.true(cookieService.clear.calledWith('session-foo'));
268296

269-
assert.ok(cookieService.clear.calledWith('session-foo-expiration_time'));
297+
assert.true(cookieService.clear.calledWith('session-foo-expiration_time'));
270298

271-
assert.ok(
299+
assert.true(
272300
cookieService.write.calledWith(
273301
'session-bar',
274302
JSON.stringify({ key: 'value' }),
@@ -283,7 +311,7 @@ export default function (options) {
283311
)
284312
);
285313

286-
assert.ok(
314+
assert.true(
287315
cookieService.write.calledWith(
288316
'session-bar-expiration_time',
289317
1000,
@@ -307,11 +335,11 @@ export default function (options) {
307335
});
308336
await store.persist({ key: 'value' });
309337

310-
assert.ok(cookieService.clear.calledWith(defaultName));
338+
assert.true(cookieService.clear.calledWith(defaultName));
311339

312-
assert.ok(cookieService.clear.calledWith(`${defaultName}-expiration_time`));
340+
assert.true(cookieService.clear.calledWith(`${defaultName}-expiration_time`));
313341

314-
assert.ok(
342+
assert.true(
315343
cookieService.write.calledWith(
316344
'session-bar',
317345
JSON.stringify({ key: 'value' }),
@@ -336,11 +364,11 @@ export default function (options) {
336364
});
337365
await store.persist({ key: 'value' });
338366

339-
assert.ok(cookieService.clear.calledWith(defaultName));
367+
assert.true(cookieService.clear.calledWith(defaultName));
340368

341-
assert.ok(cookieService.clear.calledWith(`${defaultName}-expiration_time`));
369+
assert.true(cookieService.clear.calledWith(`${defaultName}-expiration_time`));
342370

343-
assert.ok(
371+
assert.true(
344372
cookieService.write.calledWith(
345373
'session-bar',
346374
JSON.stringify({ key: 'value' }),
@@ -364,7 +392,7 @@ export default function (options) {
364392

365393
await new Promise(resolve => {
366394
next(() => {
367-
assert.ok(cookieService.clear.calledWith('session-foo-expiration_time'));
395+
assert.true(cookieService.clear.calledWith('session-foo-expiration_time'));
368396
resolve();
369397
});
370398
});
@@ -379,7 +407,7 @@ export default function (options) {
379407

380408
await new Promise(resolve => {
381409
next(() => {
382-
assert.ok(cookieSpy.calledOnce);
410+
assert.true(cookieSpy.calledOnce);
383411
resolve();
384412
});
385413
});

Diff for: pnpm-lock.yaml

+12-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)