A host token's grant is read by its own key, not from KV's list
Jonas Templestein, 2026-11-26 13:07 UTC · 917cc3720703597e472009aac59c598081d3c428 · parent 507f80b
2 files changed
M core/auth-worker/src/host-tokens.ts
@@ -19,8 +19,10 @@ // came from: an hour at most. There is no refresh token: the holder exchanges again. // // EVERY USE ASKS AGAIN (`hostTokenIdentity`): the session's grant must still be held, and its -// person must still be a member. So a sign-in that a member ended on /sessions, a sign-out and a -// removed member each stop the token at its next request. +// person must still be a member. So a removed member stops the token at its next request, and a +// sign-in that a member ended on /sessions, or a sign-out, stops it as it stops the session's own +// token: at the next read of the grant's row, which KV may answer from its cache for up to a +// minute at a place that read it just before. import type { TokenIdentity } from "iterate/workers"; import { mindApiUrlOf } from "iterate/mind-api"; import { SignInMethod } from "iterate/principal"; @@ -29,7 +31,7 @@ import { formOf, oauthError } from "./device/protocol.ts"; import { configOf, type Env, issuerOf } from "./env.ts"; import { isMember, isMemberByName } from "./members.ts"; -import { grantHeld, mindHostResourceOf, oauthHelpers, tokenIdentity } from "./oauth.ts"; +import { mindHostResourceOf, tokenIdentity } from "./oauth.ts"; export const TOKEN_EXCHANGE_GRANT = "urn:ietf:params:oauth:grant-type:token-exchange"; const ACCESS_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token"; @@ -144,7 +146,7 @@ claims.method === "passkey" ? await isMember(env, claims.email) : await isMemberByName(env, claims.email); - if (!member || !(await grantHeld(oauthHelpers(env), claims.sub, claims.grant))) return null; + if (!member || !(await grantKept(env, claims.sub, claims.grant))) return null; return { principal: { actor: claims.sub, email: claims.email }, grant: claims.grant, @@ -153,6 +155,14 @@ method: claims.method, }; } + +/** Whether the OAuth library still keeps a grant: it was not ended, and has not lapsed. READ BY + * ITS OWN KEY, as the library reads it for a token of its own, and not from the list of a + * person's grants: KV's list shows a new key late, so a token made right after a sign-in was + * nobody's for some seconds (seen on a dev mind, 26 Nov 2026). The key's form is the library's + * (`grant:<userId>:<grantId>`); ./host-tokens.test.ts fails when a new version changes it. */ +const grantKept = async (env: Env, userId: string, grantId: string) => + (await env.OAUTH_KV.get(`grant:${userId}:${grantId}`)) !== null; // RFC 8693 §2.2.2: `invalid_request` for a subject token that is not taken, `invalid_target` for a // resource that is not.
M core/auth-worker/src/oauth.ts
@@ -652,8 +652,7 @@ return signIns.sort((a, b) => b.createdAt - a.createdAt); } -/** Whether a person still holds a grant: it was not ended, and has not lapsed. */ -export async function grantHeld( +async function grantHeld( helpers: ReturnType<typeof oauthHelpers>, userId: string, grantId: string,