ADR-WEB-4. Lichess OAuth (PKCE) and Lichess linking
Source:
docs/decisions/ADR-WEB-4-lichess-oauth.md. Generated at build time; edit the source file.
Date: 2026-11-22. Status: accepted, revised after the T18 security review (fix round 1). Implements M4 Task 18. Builds on ADR 0040 (chess.com first, Lichess after), ADR 0041 (private beta), ADR 0043 (User-Agent), ADR-WEB-3 (auth data model, admission policy) and ADR-WEB-5 (account links).
Context
Section titled “Context”Lichess offers OAuth 2.0 with PKCE for public clients. It needs no app registration and no client secret: any client_id works. A Lichess OAuth exchange proves that the person controls that Lichess account. That is stronger than chess.com’s location code, so a Lichess link is verified the moment the exchange succeeds.
Better Auth needs an email for every user. Lichess shows the account’s email only with the email:read scope, and its API does not say whether that address is verified.
Decision
Section titled “Decision”Provider (apps/web/src/server/auth/lichess.ts)
Section titled “Provider (apps/web/src/server/auth/lichess.ts)”lichessProvider(env)is a generic OAuth provider:providerId: "lichess",clientId: LICHESS_CLIENT_ID(defaultchess-improver);- no client secret: the token request carries
client_idandcode_verifieronly, and the tests assert that noclient_secretis sent; pkce: true(S256). Better Auth also sends astate, and binds it to the browser with a signed state cookie (tests: a callback without the cookie is refused, a replay is refused, and a link started by an attacker attaches nothing when the victim’s browser completes it).
- No scope. Since fix round 1 we ask for no scope at all, so there is no
email:read: the app never uses a Lichess email (below), so it does not ask for one. This deviates from the brief, which asked foremail:read. We ask for no game or preference scopes either; imports use the public API. - Two base URLs.
LICHESS_AUTHORIZE_BASE_URLserves/oauth, which the browser visits.LICHESS_API_BASE_URLserves/api/tokenand/api/account, which the server calls.- Both default to
https://lichess.organd must be https outside development and tests (assertLichessBaseUrl), because bearer tokens travel to the API base. - In e2e, the browser (in the qa container) and the server (in the web container) reach the same fake. The e2e override sets both to
http://fakes:3199.
getUserInfo. It fetches/api/accountand returns{ id, name: username, email: lichessPlaceholderEmail(id), emailVerified: false, image: undefined }. The brief’simage: nulldoes not type-check against Better Auth 1.7.24;undefinedstores null.- The project User-Agent (ADR 0043) is set on this call and, through
authorizationHeaders, on the token request. It contains no email. - The call has a 5 s timeout and refuses redirects. It caps the body at 64 KB using the declared length and the bytes counted while streaming.
- Any failure returns null, so Better Auth answers
unable_to_get_user_info. - Neither the token nor the response body is logged.
- The id must equal the lower-cased username.
- The project User-Agent (ADR 0043) is set on this call and, through
mapProfileToUserforces the placeholder email andemailVerified: false, whatever the profile says.
No email from Lichess (review I5)
Section titled “No email from Lichess (review I5)”- The risk. If a Lichess sign-up stored the address Lichess reports, then with
INVITE_ONLY=0a Lichess holder could pre-create an account under someone else’s address. The real owner’s later magic link would sign them into that account, which the Lichess holder can still enter. - Placeholder address. A user created by a Lichess sign-up therefore gets
<lichess id>@lichess.chess-improver.invalid. That is non-routable (RFC 2606.invalid), anddeliverMagicLinknever sends to it. No email from Lichess is stored or even requested. - One shared helper.
isPlaceholderEmail(apps/web/src/lib/placeholder-email.ts) is used by the server and the client.sendMail, the single mail path, never sends to a placeholder, whoever the caller is (any future marketing or notice mail included).- The placeholder is never shown as an email: the account menu reads “Signed in with Lichess” with the Lichess username.
- Magic links. A magic link for any real address lands in that address’s own account, never in a Lichess-created one (tested with
INVITE_ONLY=0). - Attaching an email. An email can join a Lichess-created account only after its owner proves it with a magic link. No such attach flow exists yet; when one is built, it must use Better Auth’s verified change-email.
- No implicit linking. The admission policy (
policy.ts) refuseslink-accountfor Lichess and chess.com unless the request’s live session belongs to the same user, withaccount_not_linked. This closes the T17 final-review gap. The placeholder email also means a Lichess sign-in can never match an existing account by email. - No new accounts under
INVITE_ONLY. A Lichess identity cannot prove an invite, so withINVITE_ONLYon (the beta default) Lichess cannot create accounts (not_invited). Invited players sign in by email, connect Lichess, and from then on can use “Continue with Lichess”. - Better Auth configuration.
trustedProvidersis["google", "lichess"]. Better Auth refuses an explicit link (linkSocial) from an untrusted provider whose email is unverified. Trusting Lichess is safe only because the admission policy blocks every implicit Lichess link.allowDifferentEmails: true. The placeholder never matches the sign-in address. Better Auth applies this only to explicit links, which are proven by the live session (isLinkSessionLive), the state cookie and the OAuth exchange. It applies to Google links from a live session too, which is equally safe.
Ownership (apps/web/src/server/accounts/lichess-link.ts, db-hooks.ts)
Section titled “Ownership (apps/web/src/server/accounts/lichess-link.ts, db-hooks.ts)”- When it runs.
databaseHooks.account.create.afterandupdate.after, forproviderId === "lichess", callonLichessAccount(db, { userId, lichessId, username }). Both run after Better Auth’s transaction commits.createcovers sign-up and a first link.updatecovers a returning sign-in and connecting again while still linked; it keeps the link verified.- If recording fails, the error is logged without the row (which holds the encrypted token) and the next sign-in or connect records the link again.
- What it writes. It upserts
linked_accounts(platform: "lichess",usernamelower-cased,display_nameas Lichess shows it) and the user’saccount_linksrow, withmethod: "lichess_oauth". - The first holder keeps it (review I3). This replaces “newest owner wins”.
- Better Auth’s unique
(provider_id, account_id)refuses a second user’s OAuth link withaccount_already_linked_to_different_user. The app shows that as its own code,lichess_taken: “This Lichess account is connected to another user.” The first holder keeps the account and the verified link (tested through the real callback). onLichessAccountnever clears another user’s verified link. If one exists anyway, the new link is recorded unverified. The check runs under the per-account advisory lock shared with chess.com (OWNER_LOCK_SPACE).
- Better Auth’s unique
- Display name. The account hook sees only the account row, so
getUserInforemembers the username’s capitalisation by Lichess id in a small bounded in-process map. The hook falls back to the id, which is the lower-cased username.
Tokens
Section titled “Tokens”-
Storage. Access tokens are stored encrypted (
encryptOAuthTokens: true, ADR-WEB-3); the test checks that the stored value is not the token. -
Endpoints that return tokens are not mounted (review I1).
disabledPathslists:/get-access-token(which returns the decrypted token);/refresh-token;/account-info(the provider profile);/unlink-account(unlinking goes throughDELETE /api/accounts/:linkId).
All four answer 404 with no token in the body (tested). The account list reads only
access_token_expires_at. Tokens never reach the client, a URL, a cookie or a log line. -
Expiry. Lichess issues one-year tokens (
expires_in) and no refresh token.lichessLinkStatus({ accessTokenExpiresAt }, now)returns:ok;expiringwithin 14 days of expiry;expiredfrom expiry on.
-
In the account list.
listAccountsuses this status for Lichess links. A Lichess link with no OAuth account or noverified_atshows asexpired. Bothexpiringandexpiredoffer “Reconnect Lichess”, which runslinkSocialagain.
Removing a Lichess link (review I2)
Section titled “Removing a Lichess link (review I2)”- What is deleted.
DELETE /api/accounts/:linkId(removeLink) deletes, in one transaction, theaccount_linksrow and the user’s Lichessaccountrow with its stored token. - Revocation. After commit, the token is revoked at Lichess (
DELETE /api/token) as a detached, best-effort task. It is decrypted only for that call and never logged. - No silent restore. Nothing restores the link. A later “Continue with Lichess” is a fresh sign-in:
not_invitedunderINVITE_ONLY, or a separate new account in open mode. A later “Connect Lichess” is a fresh, explicit connect. Both are tested. - The last way in. A Lichess-created account, with the placeholder email and no other provider, cannot remove its only way to sign in. The route answers 409
last_sign_in_method, with the copy “You sign in with this Lichess account, so it can’t be removed.”- Parked (re-review n2): the check over-refuses. A Lichess-only account that has connected a second Lichess account is refused for both, although one would remain. Counting the user’s other Lichess
accountrows as another way in would fix it; it is parked because the beta has no multi-Lichess users.
- Parked (re-review n2): the check over-refuses. A Lichess-only account that has connected a second Lichess account is refused for both, although one would remain. Counting the user’s other Lichess
- Revoke failures. If Lichess answers an error or cannot be reached, the local delete still happens, the route still answers 204, and nothing about the token is logged (tested).
Errors
Section titled “Errors”mapAuthResponse maps errors on /callback/lichess. Better Auth appends its error, so the last one counts. Each failure keeps the page the flow returns to (/sign-in, or /app/accounts, which no longer has a preset error); Better Auth’s raw error page is replaced by /sign-in.
access_deniedbecomeslichess_denied.account_already_linked_to_different_userbecomeslichess_taken.account_not_linkedalways goes to/sign-in?error=account_not_linked.not_invitedandemail_not_verifiedkeep the general mapping.- Anything else becomes
lichess_failed: a missing, stale or replayed state, a failed exchange,unable_to_get_user_infoor a stale session.
Enabled
Section titled “Enabled”lichessProvider(env) always returns the provider, because Lichess needs no registration. “Continue with Lichess” and “Connect Lichess” therefore show by default, which ends the ADR 0040 deferral for Lichess.
Consequences
Section titled “Consequences”- Tests.
apps/web/test/auth/lichess.test.tsdrives the real Better Auth endpoints against an in-process fake Lichess on a random port (test/fakes/main.ts,startFakes), and any other outbound request throws. The fake checks the S256 verifier against the stored challenge and records revoked tokens. The composefakesservice serves the same routes on 3199. - e2e.
accounts.spec.tssigns a player in by email, connects Lichess through the fake, then signs in with “Continue with Lichess”. No real Lichess app is needed. - Multiple processes. The display-name map is per process. If a hook ran on another process, the name would fall back to the lower-case id, which is cosmetic only.