-
Notifications
You must be signed in to change notification settings - Fork 0
fix: routes that were never mounted, errors that destroyed themselves, tests that lied to each other #94
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
fix: routes that were never mounted, errors that destroyed themselves, tests that lied to each other #94
Changes from 1 commit
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * The round trip "log in with GitHub" is, driven through Postly's OWN declaration: its providers, | ||
| * its link policy, its landing path. Three shipped error codes told the caller to restart at | ||
| * `GET /auth/oauth/<provider>` while nothing anywhere ran the flow — so this asserts the path the | ||
| * refusal names against the path the declaration mounts, never against a string this file repeats. | ||
| */ | ||
|
|
||
| import { beforeEach, describe, expect, test } from 'bun:test'; | ||
| import type { OAuthFetch } from '@ultimat3/auth'; | ||
| import { authenticate, MemoryAdapter, readSessionCookie } from '@ultimat3/auth'; | ||
| import { frozenClock } from '@ultimat3/core'; | ||
| import { AFTER_SIGN_IN, postlyAuth, postlyLogin } from './login'; | ||
|
|
||
| const NOW = new Date('2026-08-15T12:00:00.000Z'); | ||
| /** 32 bytes, the length `handshakeSecret` demands — never Postly's real `SESSION_SECRET`. */ | ||
| const SECRET = 'postly-test-handshake-secret-000'; | ||
| const ORIGIN = 'https://postly.test'; | ||
| const CREDENTIALS = { clientId: 'postly-client-id', clientSecret: 'postly-client-secret' }; | ||
|
|
||
| /** | ||
| * There is no GitHub client id in CI and there never will be, so the provider is the one seam the | ||
| * framework already hands a caller: the three calls a real login makes, answered in process. Every | ||
| * other step — PKCE, the sealed handshake, the state check, the session — is the real code. | ||
| */ | ||
| const githubFetch: OAuthFetch = (input) => { | ||
| if (input === 'https://github.com/login/oauth/access_token') { | ||
| return Promise.resolve(Response.json({ access_token: 'gho_postly', token_type: 'bearer' })); | ||
| } | ||
| if (input === 'https://api.github.com/user') { | ||
| return Promise.resolve(Response.json({ id: 4207, login: 'ada', name: 'Ada Lovelace' })); | ||
| } | ||
| if (input === 'https://api.github.com/user/emails') { | ||
| return Promise.resolve( | ||
| Response.json([{ email: 'ada@postly.test', primary: true, verified: true }]), | ||
| ); | ||
| } | ||
| return Promise.resolve(new Response('a call this login does not make', { status: 500 })); | ||
| }; | ||
|
|
||
| let adapter: MemoryAdapter; | ||
| let auth: ReturnType<typeof postlyAuth>; | ||
| let login: ReturnType<typeof postlyLogin>; | ||
|
|
||
| beforeEach(() => { | ||
| adapter = new MemoryAdapter(); | ||
| auth = postlyAuth({ adapter, clock: frozenClock(NOW) }); | ||
| login = postlyLogin(auth, { | ||
| credentials: CREDENTIALS, | ||
| fetch: githubFetch, | ||
| secret: SECRET, | ||
| baseUrl: ORIGIN, | ||
| }); | ||
| }); | ||
|
|
||
| /** `/auth/oauth/:provider` → the URL a browser is actually sent to. The mount, filled in. */ | ||
| const mounted = (pattern: string, provider: string): string => | ||
| pattern.replace(':provider', provider); | ||
|
|
||
| /** `name=value` — what a browser sends back, without the attributes it keeps to itself. */ | ||
| const cookiePair = (setCookie: string): string => setCookie.slice(0, setCookie.indexOf(';')); | ||
|
|
||
| const bodyOf = async (response: Response): Promise<Record<string, unknown>> => { | ||
| const parsed: unknown = await response.json(); | ||
| expect(parsed).toBeObject(); | ||
| return parsed as Record<string, unknown>; | ||
| }; | ||
|
|
||
| const startRequest = (provider: string): Request => | ||
| new Request(`${ORIGIN}${mounted(login.start.path, provider)}`); | ||
|
|
||
| describe('log in with GitHub', () => { | ||
| test('the start leg leaves for github with a state and an S256 challenge', async () => { | ||
| const response = await login.start.handle(startRequest('github')); | ||
|
|
||
| expect(response.status).toBe(302); | ||
| const authorize = new URL(response.headers.get('location') ?? ''); | ||
| expect(`${authorize.origin}${authorize.pathname}`).toBe( | ||
| 'https://github.com/login/oauth/authorize', | ||
| ); | ||
| expect(authorize.searchParams.get('state')).not.toBe(''); | ||
| expect(authorize.searchParams.get('code_challenge_method')).toBe('S256'); | ||
| expect(authorize.searchParams.get('code_challenge')).not.toBeNull(); | ||
| // The address GitHub sends the browser back to is Postly's own callback mount, not a literal. | ||
| expect(authorize.searchParams.get('redirect_uri')).toBe( | ||
| `${ORIGIN}${mounted(login.callback.path, 'github')}`, | ||
| ); | ||
| // Sealed across the two requests, and readable by nothing in the page. | ||
| const sealed = response.headers.getSetCookie(); | ||
| expect(sealed).toHaveLength(1); | ||
| expect(sealed[0]).toContain('HttpOnly'); | ||
| }); | ||
|
|
||
| test('a forged state is refused, and its fix names a path this app mounts', async () => { | ||
| const start = await login.start.handle(startRequest('github')); | ||
|
|
||
| const done = await login.callback.handle( | ||
| new Request(`${ORIGIN}${mounted(login.callback.path, 'github')}?code=c&state=forged`, { | ||
| headers: { cookie: cookiePair(start.headers.getSetCookie()[0] ?? '') }, | ||
| }), | ||
| ); | ||
|
|
||
| expect(done.status).toBe(400); | ||
| const body = await bodyOf(done); | ||
| expect(body['code']).toBe('X_OAUTH_STATE_INVALID'); | ||
|
|
||
| // Axiom 4, checked as a round trip: the path the fix line tells the caller to restart at has | ||
| // to be one this app's own start descriptor claims. A literal here would pass while the mount | ||
| // moved out from under it, which is exactly how three fix lines outlived their route. | ||
| const named = /GET (\/\S+)/.exec(String(body['fix']))?.[1] ?? ''; | ||
| expect(named).toBe(mounted(login.start.path, 'github')); | ||
|
|
||
| // The code the handshake authorised is spent whether or not the callback succeeded. | ||
| expect(done.headers.getSetCookie().some((c) => c.includes('=;'))).toBe(true); | ||
| }); | ||
|
|
||
| test('a completed login lands on Postly and mints a session Postly can authenticate', async () => { | ||
| const start = await login.start.handle(startRequest('github')); | ||
| const state = new URL(start.headers.get('location') ?? '').searchParams.get('state') ?? ''; | ||
|
|
||
| const done = await login.callback.handle( | ||
| new Request( | ||
| `${ORIGIN}${mounted(login.callback.path, 'github')}?code=the-code&state=${state}`, | ||
| { headers: { cookie: cookiePair(start.headers.getSetCookie()[0] ?? '') } }, | ||
| ), | ||
| ); | ||
|
|
||
| expect(done.status).toBe(303); | ||
| expect(done.headers.get('location')).toBe(AFTER_SIGN_IN); | ||
|
|
||
| // The end of the round trip: the cookie the callback set is one the app's own `authenticate` | ||
| // resolves to an actor. Without this the flow could "succeed" and still sign nobody in. | ||
| const session = new Request(ORIGIN, { | ||
| headers: { cookie: done.headers.getSetCookie().map(cookiePair).join('; ') }, | ||
| }); | ||
| const token = readSessionCookie(session, auth.sessions.policy); | ||
| expect(token).not.toBeNull(); | ||
|
|
||
| const actor = await authenticate(auth, token); | ||
| const user = await adapter.findUserByEmail('ada@postly.test'); | ||
| expect(actor.id).toBe(user?.id ?? ''); | ||
| // `link: 'verified-email'` is only safe because the provider's assertion is recorded. | ||
| expect(user?.emailVerifiedAt).toEqual(NOW); | ||
| expect(await adapter.findAccount('github', '4207')).not.toBeNull(); | ||
| }); | ||
|
|
||
| test('a provider Postly never enabled never reaches a provider', async () => { | ||
| const response = await login.start.handle(startRequest('google')); | ||
|
|
||
| expect(response.status).toBe(404); | ||
| expect((await bodyOf(response))['code']).toBe('X_OAUTH_PROVIDER_UNKNOWN'); | ||
| expect(response.headers.get('location')).toBeNull(); | ||
| }); | ||
| }); | ||
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,53 @@ | ||
| /** | ||
| * "Log in with GitHub", as Postly declares it: one `defineAuth` and one `oauthLogin`, and that is | ||
| * the whole of it. PKCE, the sealed handshake, the state check, the account link and the session | ||
| * cookie are the framework's — this file holds only the three decisions an app owns: which | ||
| * providers, when two identities are one person, and where a signed-in member lands. | ||
| */ | ||
|
|
||
| import type { Auth, AuthAdapter, OAuthLoginOptions, OAuthLoginRoutes } from '@ultimat3/auth'; | ||
| import { BuiltinAdapter, defineAuth, oauthLogin } from '@ultimat3/auth'; | ||
| import type { Clock } from '@ultimat3/core'; | ||
|
|
||
| /** | ||
| * Where a signed-in member lands. A fixed path and never a `?next=` off the callback: the one | ||
| * endpoint whose job is to hand out a session is the classic open redirect. | ||
| */ | ||
| export const AFTER_SIGN_IN = '/feed'; | ||
|
|
||
| export interface PostlyAuthSeams { | ||
| /** | ||
| * Defaults to Postgres, the shape `BuiltinAdapter(client = db())` already uses. Only a test | ||
| * passes anything else, so the production wiring is what the declaration says rather than | ||
| * something assembled a second time somewhere a test never reaches. | ||
| */ | ||
| readonly adapter?: AuthAdapter | undefined; | ||
| readonly clock?: Clock | undefined; | ||
| } | ||
|
|
||
| export function postlyAuth(seams: PostlyAuthSeams = {}): Auth { | ||
| return defineAuth({ | ||
| adapter: seams.adapter ?? new BuiltinAdapter(), | ||
| // GitHub alone. Every provider listed here is a button someone has to keep working, and a | ||
| // provider missing from this list is a 404 rather than a half-configured redirect. | ||
| providers: ['github'], | ||
| // The default, spelled out because it is Postly's to keep: a provider identity joins an | ||
| // existing member only when the provider AND that member both proved the address. | ||
| link: 'verified-email', | ||
| ...(seams.clock === undefined ? {} : { clock: seams.clock }), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * The two legs of the login as route descriptors — `start.path` is `/auth/oauth/:provider` and | ||
| * `callback.path` is `/auth/oauth/:provider/callback`, the one declaration every `X_OAUTH_*` fix | ||
| * line quotes. `options` carries the seams the framework already injects (credentials, the token | ||
| * endpoint's `fetch`, the handshake secret), so a test drives THIS login and not a copy of it. | ||
| */ | ||
| export const postlyLogin = (auth: Auth, options: OAuthLoginOptions = {}): OAuthLoginRoutes => | ||
| oauthLogin(auth, { successPath: AFTER_SIGN_IN, ...options }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** Postly's own, built once: the app has one boot and therefore one identity resolver. */ | ||
| export const auth = postlyAuth(); | ||
|
|
||
| export const { start, callback } = postlyLogin(auth); | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
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.