-
Notifications
You must be signed in to change notification settings - Fork 952
Auth cookie persistence #8839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Auth cookie persistence #8839
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d3792b5
First.
jamesdaniels 00b0059
Merge branch 'main' into jamesdaniels_authCookiePersistence
jamesdaniels aba574a
First.
jamesdaniels 8acd061
Sentinal as ""
jamesdaniels a727dce
Cleanup and add cookieStore fallback
jamesdaniels 769624f
Cleanup unsubscribes
jamesdaniels 31cb8b0
Cleanup and add docs
jamesdaniels 0bef1eb
Cleanup and add docs
jamesdaniels efdafc5
Changeset
jamesdaniels da747d7
Beta tag
jamesdaniels 27c203a
Merge branch 'main' into jamesdaniels_authCookiePersistence
jamesdaniels 7f850fc
Remove unneeded changes
jamesdaniels f587178
Address feedback, cleanup endpoint check
jamesdaniels 5568a01
Formatting
jamesdaniels 0ac4cad
Cleanup
jamesdaniels d278dcf
Tweak wording for doc
jamesdaniels fc1ce03
Code format COOKIE
jamesdaniels 6acbb6a
Formatting
jamesdaniels 8f97100
Cleanup
jamesdaniels 3291f23
Formatting
jamesdaniels 0fb3804
Update docs
jamesdaniels bdeeead
Fix test
jamesdaniels 5a913c9
Comments!
jamesdaniels d3fa466
Fix for the auth race condition
jamesdaniels 680583e
Address erics feedback
jamesdaniels 1e50af6
Forgot to commit the actual source file
jamesdaniels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,7 @@ | |
"@rollup/plugin-strip": "2.1.0", | ||
"@types/express": "4.17.21", | ||
"chromedriver": "119.0.1", | ||
"cookie-store": "4.0.0-next.4", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI using this dev-dep for types, somewhere else I tried this ponyfill and found it unsuitable for prod as it can't be webpacked. |
||
"rollup": "2.79.2", | ||
"rollup-plugin-sourcemaps": "0.6.3", | ||
"rollup-plugin-typescript2": "0.36.0", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/auth/src/platform_browser/persistence/cookie_storage.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Persistence } from '../../model/public_types'; | ||
import type { CookieChangeEvent } from 'cookie-store'; | ||
|
||
const POLLING_INTERVAL_MS = 1_000; | ||
|
||
import { | ||
PersistenceInternal, | ||
PersistenceType, | ||
PersistenceValue, | ||
StorageEventListener | ||
} from '../../core/persistence'; | ||
|
||
const getDocumentCookie = (name: string): string | null => { | ||
jamesdaniels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const escapedName = name.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'); | ||
const matcher = RegExp(`${escapedName}=([^;]+)`); | ||
return document.cookie.match(matcher)?.[1] ?? null; | ||
}; | ||
|
||
export class CookiePersistence implements PersistenceInternal { | ||
static type: 'COOKIE' = 'COOKIE'; | ||
readonly type = PersistenceType.COOKIE; | ||
cookieStoreListeners: Map< | ||
StorageEventListener, | ||
(event: CookieChangeEvent) => void | ||
> = new Map(); | ||
cookiePollingIntervals: Map<StorageEventListener, NodeJS.Timeout> = new Map(); | ||
|
||
async _isAvailable(): Promise<boolean> { | ||
jamesdaniels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (typeof navigator === 'undefined' || typeof document === 'undefined') { | ||
return false; | ||
} | ||
return navigator.cookieEnabled ?? true; | ||
} | ||
|
||
async _set(_key: string, _value: PersistenceValue): Promise<void> { | ||
return; | ||
} | ||
|
||
async _get<T extends PersistenceValue>(key: string): Promise<T | null> { | ||
if (!this._isAvailable()) { | ||
return null; | ||
} | ||
if (window.cookieStore) { | ||
const cookie = await window.cookieStore.get(key); | ||
return cookie?.value as T; | ||
} else { | ||
return getDocumentCookie(key) as T; | ||
} | ||
} | ||
|
||
async _remove(key: string): Promise<void> { | ||
jamesdaniels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!this._isAvailable()) { | ||
return; | ||
} | ||
if (window.cookieStore) { | ||
const cookie = await window.cookieStore.get(key); | ||
if (!cookie) { | ||
return; | ||
} | ||
await window.cookieStore.delete(cookie); | ||
} else { | ||
// TODO how do I get the cookie properties? | ||
document.cookie = `${key}=;Max-Age=34560000;Partitioned;Secure;SameSite=Strict;Path=/`; | ||
} | ||
await fetch(`/__cookies__`, { method: 'DELETE' }).catch(() => undefined); | ||
} | ||
|
||
_addListener(key: string, listener: StorageEventListener): void { | ||
if (!this._isAvailable()) { | ||
return; | ||
} | ||
if (window.cookieStore) { | ||
const cb = (event: CookieChangeEvent): void => { | ||
const changedCookie = event.changed.find(change => change.name === key); | ||
if (changedCookie) { | ||
listener(changedCookie.value as PersistenceValue); | ||
} | ||
const deletedCookie = event.deleted.find(change => change.name === key); | ||
if (deletedCookie) { | ||
listener(null); | ||
} | ||
}; | ||
this.cookieStoreListeners.set(listener, cb); | ||
window.cookieStore.addEventListener('change', cb as EventListener); | ||
} else { | ||
let lastValue = getDocumentCookie(key); | ||
const interval = setInterval(() => { | ||
const currentValue = getDocumentCookie(key); | ||
if (currentValue !== lastValue) { | ||
listener(currentValue as PersistenceValue | null); | ||
lastValue = currentValue; | ||
} | ||
}, POLLING_INTERVAL_MS); | ||
this.cookiePollingIntervals.set(listener, interval); | ||
} | ||
} | ||
|
||
// TODO can we tidy this logic up into a single unsubscribe function? () => void; | ||
_removeListener(_key: string, listener: StorageEventListener): void { | ||
if (!this._isAvailable()) { | ||
return; | ||
} | ||
if (window.cookieStore) { | ||
const cb = this.cookieStoreListeners.get(listener); | ||
if (!cb) { | ||
return; | ||
} | ||
window.cookieStore.removeEventListener('change', cb as EventListener); | ||
this.cookieStoreListeners.delete(listener); | ||
} else { | ||
const interval = this.cookiePollingIntervals.get(listener); | ||
if (!interval) { | ||
return; | ||
} | ||
clearInterval(interval); | ||
this.cookiePollingIntervals.delete(listener); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* An implementation of {@link Persistence} of type 'COOKIE'. | ||
* | ||
* @public | ||
*/ | ||
export const cookiePersistence: Persistence = CookiePersistence; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"extends": "../../config/api-extractor.json", | ||
// Point it to your entry point d.ts file. | ||
"mainEntryPointFilePath": "<projectFolder>/dist/rules-unit-testing/index.d.ts" | ||
"mainEntryPointFilePath": "<projectFolder>/dist/index.d.ts" | ||
jamesdaniels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.