Repository navigation
refactor(account): decompose the session client into one typed commit boundary #17830
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
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
c19c893
refactor(account): make hosts the single source of auth copy
MaanilVerma e211cd3
fix(locales): drop account-existence wording from email-already-in-use
MaanilVerma 64ece9e
test(auth): guard cloud auth copy against account enumeration
MaanilVerma 3aa6c3c
test(auth): address review nits on auth copy resolution
MaanilVerma 36c5790
test(website): assert the collapsed invalid-credential line on user-n…
MaanilVerma c66a653
fix(website): drop the unrenderable user-not-found/wrong-password copy
MaanilVerma 9348238
test(website): broaden the sign-in enumeration oracle to match the cl…
MaanilVerma b742c75
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma 7184764
fix(locales): drop the unrenderable user-not-found/wrong-password copy
MaanilVerma d0e3c13
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma 3633eb7
test(website): use the i18n generic-error copy after the main merge
MaanilVerma cea548a
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma 61b4451
fix(workspace): type the session error key against the locale shape
MaanilVerma b7a3541
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma 885faf4
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma e68e3f0
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma 4db1dbe
Merge remote-tracking branch 'origin/main' into maanil/fe-2191-decomp…
MaanilVerma d69670b
refactor(account): add pure session state transition and mint arbitra…
MaanilVerma 7a16e1f
refactor(account): route every session commit through one transition
MaanilVerma 6c9c0a5
refactor(account): import AccountUser from sessionContracts in identity
MaanilVerma d5e9fc3
refactor(account): carry commit payloads in session events and effects
MaanilVerma 4878540
refactor(account): extract credential cache and mint coordinator from…
MaanilVerma 3ccca0d
refactor(account): keep credential freshness policy with the cache
MaanilVerma 821bdf6
refactor(account): route the expiry commit through the single commit …
MaanilVerma f7e2e23
Merge remote-tracking branch 'origin/main' into maanil/fe-2191-decomp…
MaanilVerma 9874cfe
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma 651e277
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma 71e38b3
refactor(account): split the session transition and mint arbitration …
MaanilVerma 612a1bb
fix(account): tighten the popup mint to exactly one identity event
MaanilVerma 44bbc19
perf(account): read stored credentials only when the in-memory one is…
MaanilVerma 23c5bb6
test(auth): share the enumeration oracle and cover the cancelled seco…
MaanilVerma df8ea1d
Merge branch 'main' into maanil/fe-2188-localization-sso
MaanilVerma 82e272e
Merge remote-tracking branch 'origin/maanil/fe-2188-localization-sso'…
MaanilVerma 448c4b9
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma 52054e1
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma 386d96d
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma ffbec33
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma df6ffd7
refactor(account): name the mint guards by verdict and split the type…
MaanilVerma d48db49
test(account): share the session test fakes from one fixture module
MaanilVerma 9826d1b
Merge remote-tracking branch 'origin/main' into maanil/fe-2188-locali…
MaanilVerma f8e97d4
Merge branch 'maanil/fe-2188-localization-sso' into maanil/fe-2191-de…
MaanilVerma feaeccc
test(account): split the client builder out of the data fixture and d…
MaanilVerma 112bec2
test(account): import the scheduler buffer default instead of copying it
MaanilVerma 4fbdc9b
fix(account): drop a session subscriber whose first replay throws
MaanilVerma 7614f52
Merge remote-tracking branch 'origin/main' into maanil/fe-2191-decomp…
MaanilVerma 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
19 changes: 19 additions & 0 deletions
19
packages/account-core/src/core/__fixtures__/sessionClientFixture.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,19 @@ | ||
| /** | ||
| * The session client builder. Kept apart from `sessionFakes.ts` so the | ||
| * pure-module suites (credential cache, mint coordinator, state machine) that | ||
| * only need the data fakes do not evaluate the whole client graph. | ||
| */ | ||
| import type { SessionClientOptions } from '../session.js' | ||
| import { createSessionClient } from '../session.js' | ||
|
|
||
| import { EXCHANGE_URL, memoryStorage } from './sessionFakes.js' | ||
|
|
||
| export function makeClient(overrides: Partial<SessionClientOptions> = {}) { | ||
| const storage = memoryStorage() | ||
| const client = createSessionClient({ | ||
| exchangeUrl: EXCHANGE_URL, | ||
| storage, | ||
| ...overrides | ||
| }) | ||
| return { client, storage } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
packages/account-core/src/core/__fixtures__/sessionFakes.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,98 @@ | ||
| /** | ||
| * The storage, identity, user, credential and exchange fakes the session | ||
| * suites build on. A suite keeps what only it needs. | ||
| */ | ||
| import { vi } from 'vitest' | ||
|
|
||
| import { createTestIdentity } from '../../testing.js' | ||
| import type { | ||
| AccountCredential, | ||
| AccountUser, | ||
| CredentialStorage | ||
| } from '../session.js' | ||
|
|
||
| export const EXCHANGE_URL = 'https://cloud.test/api/auth/token' | ||
| export const NINETY_MINUTES_MS = 90 * 60 * 1000 | ||
|
|
||
| export function memoryStorage(): CredentialStorage & { | ||
| raw: () => string | null | ||
| } { | ||
| let value: string | null = null | ||
| return { | ||
| read: () => value, | ||
| write: (next) => { | ||
| value = next | ||
| }, | ||
| clear: () => { | ||
| value = null | ||
| }, | ||
| raw: () => value | ||
| } | ||
| } | ||
|
|
||
| export function testUser(uid = 'uid-1', idToken = 'id-token-1'): AccountUser { | ||
| return { uid, getIdToken: vi.fn(async () => idToken) } | ||
| } | ||
|
|
||
| export function credential( | ||
| token: string, | ||
| overrides: Partial<AccountCredential> = {} | ||
| ): AccountCredential { | ||
| return { | ||
| token, | ||
| expiresAt: 1_000_000, | ||
| uid: 'uid-1', | ||
| workspace: { id: 'ws-1', name: 'Personal', type: 'personal' }, | ||
| role: 'owner', | ||
| permissions: ['workspace:read'], | ||
| ...overrides | ||
| } | ||
| } | ||
|
|
||
| export function deferred<T>() { | ||
| let resolve!: (value: T) => void | ||
| const promise = new Promise<T>((r) => (resolve = r)) | ||
| return { promise, resolve } | ||
| } | ||
|
|
||
| export function mintBody(overrides: Record<string, unknown> = {}) { | ||
| return { | ||
| token: 'workspace-jwt', | ||
| permissions: ['workspace:read'], | ||
| expires_at: new Date(Date.now() + NINETY_MINUTES_MS).toISOString(), | ||
| workspace: { id: 'ws-1', name: 'Personal', type: 'personal' }, | ||
| role: 'owner', | ||
| ...overrides | ||
| } | ||
| } | ||
|
|
||
| export function jsonResponse(status: number, body: unknown) { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { 'Content-Type': 'application/json' } | ||
| }) | ||
| } | ||
|
|
||
| export function okFetch(token = 'workspace-jwt') { | ||
| return vi.fn<typeof fetch>(async () => jsonResponse(200, mintBody({ token }))) | ||
| } | ||
|
|
||
| export function mintResponse(token: string) { | ||
|
CodeJuggernaut marked this conversation as resolved.
Resolved
|
||
| return jsonResponse(200, mintBody({ token })) | ||
| } | ||
|
|
||
| export function manualIdentity() { | ||
| let deliver: ((user: AccountUser | null) => void) | undefined | ||
| const unsubscribe = vi.fn() | ||
| const port = createTestIdentity<AccountUser>({ | ||
| onUserChanged: (callback) => { | ||
| deliver = callback | ||
| return unsubscribe | ||
| } | ||
| }) | ||
| return { | ||
| port, | ||
| fire: (user: AccountUser | null) => deliver?.(user), | ||
| unsubscribe | ||
| } | ||
| } | ||
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,210 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { | ||
| credential as makeCredential, | ||
| memoryStorage | ||
| } from './__fixtures__/sessionFakes.js' | ||
| import { | ||
| createCredentialCache, | ||
| decodeAdopted, | ||
| decodeCached, | ||
| encodeCached, | ||
| selectFreshCredential | ||
| } from './credentialCache.js' | ||
| import type { AccountCredential } from './sessionContracts.js' | ||
|
|
||
| const credential = makeCredential('cached-jwt') | ||
|
|
||
| describe('decodeCached', () => { | ||
| it.for<{ name: string; raw: string }>([ | ||
| { name: 'malformed JSON', raw: '{not json' }, | ||
| { name: 'a record missing the contract fields', raw: '{"uid":"uid-1"}' }, | ||
| { | ||
| name: 'a record for another user', | ||
| raw: encodeCached({ ...credential, uid: 'someone-else' }, undefined) | ||
| }, | ||
| { | ||
| name: 'an empty token', | ||
| raw: encodeCached({ ...credential, token: '' }, undefined) | ||
| }, | ||
| { | ||
| name: 'a non-finite expiry', | ||
| raw: encodeCached(credential, undefined).replace( | ||
| '"expiresAt":1000000', | ||
| '"expiresAt":1e999' | ||
| ) | ||
| } | ||
| ])('rejects $name', ({ raw }) => { | ||
| expect(decodeCached(raw, 'uid-1')).toBeUndefined() | ||
| }) | ||
|
|
||
| it.for<{ name: string; target: string | undefined }>([ | ||
| { name: 'a workspace target', target: 'ws-9' }, | ||
| { name: 'the absent personal target', target: undefined } | ||
| ])('round-trips a credential with $name', ({ target }) => { | ||
| expect(decodeCached(encodeCached(credential, target), 'uid-1')).toEqual({ | ||
| credential, | ||
| target | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('decodeAdopted', () => { | ||
| it('returns the credential without the storage-only target', () => { | ||
| expect(decodeAdopted({ ...credential, target: 'ws-9' })).toEqual(credential) | ||
| }) | ||
|
|
||
| it.for<{ name: string; message: unknown }>([ | ||
| { name: 'a non-object', message: 'not even an object' }, | ||
| { name: 'a record missing the contract fields', message: { token: 'x' } }, | ||
| { | ||
| name: 'a non-finite expiry', | ||
| message: { ...credential, expiresAt: Number.POSITIVE_INFINITY } | ||
| }, | ||
| { name: 'an empty token', message: { ...credential, token: '' } } | ||
| ])('rejects $name', ({ message }) => { | ||
| expect(decodeAdopted(message)).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('createCredentialCache', () => { | ||
| it('writes and reads back a credential for its user only', () => { | ||
| const cache = createCredentialCache(memoryStorage()) | ||
|
|
||
| cache.write(credential, 'ws-9') | ||
|
|
||
| expect(cache.read('uid-1')).toEqual({ credential, target: 'ws-9' }) | ||
| expect(cache.read('someone-else')).toBeUndefined() | ||
| }) | ||
|
|
||
| it('reads a miss after clear', () => { | ||
| const cache = createCredentialCache(memoryStorage()) | ||
| cache.write(credential, undefined) | ||
|
|
||
| cache.clear() | ||
|
|
||
| expect(cache.read('uid-1')).toBeUndefined() | ||
| }) | ||
|
|
||
| it('degrades a throwing medium to a miss and swallows failed writes and clears', () => { | ||
| const cache = createCredentialCache({ | ||
| read: () => { | ||
| throw new Error('blocked') | ||
| }, | ||
| write: () => { | ||
| throw new Error('quota') | ||
| }, | ||
| clear: () => { | ||
| throw new Error('blocked') | ||
| } | ||
| }) | ||
|
|
||
| expect(cache.read('uid-1')).toBeUndefined() | ||
| expect(() => cache.write(credential, undefined)).not.toThrow() | ||
| expect(() => cache.clear()).not.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe('selectFreshCredential', () => { | ||
| const fresh = makeCredential('fresh') | ||
| const stale = makeCredential('stale', { expiresAt: 500_000 }) | ||
| const now = 400_000 | ||
| const margin = 300_000 | ||
|
|
||
| it.for<{ | ||
| name: string | ||
| memory: AccountCredential | undefined | ||
| memoryTarget: string | undefined | ||
| stored: { credential: AccountCredential; target: string | undefined } | ||
| target: string | undefined | ||
| expected: string | undefined | ||
| }>([ | ||
| { | ||
| name: 'a fresh in-memory credential for the target wins over storage', | ||
| memory: fresh, | ||
| memoryTarget: 'ws-1', | ||
| stored: { credential: makeCredential('stored'), target: 'ws-1' }, | ||
| target: 'ws-1', | ||
| expected: 'fresh' | ||
| }, | ||
| { | ||
| name: 'a stale in-memory credential falls through to fresh storage', | ||
| memory: stale, | ||
| memoryTarget: 'ws-1', | ||
| stored: { credential: makeCredential('stored'), target: 'ws-1' }, | ||
| target: 'ws-1', | ||
| expected: 'stored' | ||
| }, | ||
| { | ||
| name: 'an in-memory credential for another target is skipped', | ||
| memory: fresh, | ||
| memoryTarget: 'ws-9', | ||
| stored: { credential: makeCredential('stored'), target: 'ws-1' }, | ||
| target: 'ws-1', | ||
| expected: 'stored' | ||
| }, | ||
| { | ||
| name: 'a stored credential for another target is skipped', | ||
| memory: undefined, | ||
| memoryTarget: undefined, | ||
| stored: { credential: makeCredential('stored'), target: 'ws-9' }, | ||
| target: undefined, | ||
| expected: undefined | ||
| }, | ||
| { | ||
| name: 'an in-memory credential for another user is skipped', | ||
| memory: makeCredential('other', { uid: 'uid-2' }), | ||
| memoryTarget: 'ws-1', | ||
| stored: { credential: stale, target: 'ws-1' }, | ||
| target: 'ws-1', | ||
| expected: undefined | ||
| }, | ||
| { | ||
| name: 'nothing fresh anywhere yields no candidate', | ||
| memory: stale, | ||
| memoryTarget: 'ws-1', | ||
| stored: { credential: stale, target: 'ws-1' }, | ||
| target: 'ws-1', | ||
| expected: undefined | ||
| } | ||
| ])('$name', ({ memory, memoryTarget, stored, target, expected }) => { | ||
| const selected = selectFreshCredential( | ||
| [() => ({ credential: memory, target: memoryTarget }), () => stored], | ||
| 'uid-1', | ||
| target, | ||
| now, | ||
| margin | ||
| ) | ||
|
|
||
| expect(selected?.token).toBe(expected) | ||
| }) | ||
|
|
||
| it('treats a storage miss as no candidate', () => { | ||
| expect( | ||
| selectFreshCredential( | ||
| [() => ({ credential: undefined, target: undefined }), () => undefined], | ||
| 'uid-1', | ||
| undefined, | ||
| now, | ||
| margin | ||
| ) | ||
| ).toBeUndefined() | ||
| }) | ||
|
|
||
| it('does not consult a later candidate once an earlier one is fresh', () => { | ||
| const stored = vi.fn(() => ({ | ||
| credential: makeCredential('stored'), | ||
| target: 'ws-1' | ||
| })) | ||
|
|
||
| selectFreshCredential( | ||
| [() => ({ credential: makeCredential('fresh'), target: 'ws-1' }), stored], | ||
| 'uid-1', | ||
| 'ws-1', | ||
| 400_000, | ||
| 300_000 | ||
| ) | ||
|
|
||
| expect(stored).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.