Git's helper signs in again when the mind refuses the kept session and a person is there
Jonas Templestein, 2026-11-26 13:05 UTC · 507f80b93893cf32ae922c91ca96c7a01d5ea585 · parent a57e25d
3 files changed
M core/lib/src/cli/cli.ts
@@ -223,8 +223,9 @@ /** How git's credential helper reaches the CLI's sign-in to the mind at `address` (./git.ts). */ const gitCredentialIo = (address: string): GitCredentialIo => ({ say: (line) => console.error(line), - async session({ leastLeftMs, signIn }) { + async session({ leastLeftMs, signIn, anew }) { const resource = mindApiUrlOf(address); + if (anew) removeSession(resource); const token = await mindToken({ address, resource,
M core/lib/src/cli/git.test.ts
@@ -177,6 +177,32 @@ expect(readSession("https://git.example.com")).toBeNull(); }); +test("with a person there, a session that the issuer refuses is forgotten and signed in again, once", async () => { + await run(issuerOf(), "erase"); + const issuer = issuerOf(); + let refusals = 1; + const io = { + ...issuer.io, + fetcher: (async (url: string, init: RequestInit) => + refusals-- > 0 + ? Response.json({ error: "invalid_request" }, { status: 400 }) + : issuer.io.fetcher(url, init)) as typeof fetch, + }; + const stdout = await gitCredential({ + operation: "get", + asked: ASKED, + address: ADDRESS, + interactive: true, + io, + }); + expect(stdout).toBe("username=iterate\npassword=host-1\n"); + expect(issuer.sessions).toEqual([ + { leastLeftMs: 2 * HOST_TOKEN_LEAST_LEFT_MS, signIn: true }, + { leastLeftMs: 2 * HOST_TOKEN_LEAST_LEFT_MS, signIn: true, anew: true }, + ]); + expect(issuer.said).toEqual(["iterate: your sign-in to iterate.example.com has ended."]); +}); + test("store, an unknown word and a request that is not https answer nothing and ask nobody", async () => { const issuer = issuerOf(); expect(await run(issuer, "store")).toBe(""); @@ -206,7 +232,7 @@ said, io: { say: (line: string) => void said.push(line), - async session(input: { leastLeftMs: number; signIn: boolean }) { + async session(input: { leastLeftMs: number; signIn: boolean; anew?: boolean }) { sessions.push(input); if (options.session) throw options.session; return session;
M core/lib/src/cli/git.ts
@@ -147,8 +147,9 @@ /** What the helper needs of the CLI's sign-in to the host's mind. */ export type GitCredentialIo = { /** The session for the mind's API with at least `leastLeftMs` left, signed in or renewed on the - * way. With `signIn` false it throws when there is no session to renew. */ - session(input: { leastLeftMs: number; signIn: boolean }): Promise<StoredSession>; + * way. With `signIn` false it throws when there is no session to renew. `anew`: the kept + * session was refused, so it is forgotten and the person signs in again. */ + session(input: { leastLeftMs: number; signIn: boolean; anew?: boolean }): Promise<StoredSession>; fetcher?: typeof fetch; /** A line for the person: stderr, which git passes to the screen. */ say(line: string): void; @@ -211,19 +212,9 @@ const kept = readSession(resource); if (kept?.expiresAt && Date.parse(kept.expiresAt) > Date.now() + HOST_TOKEN_LEAST_LEFT_MS) return credentialOf(kept.token); - let session: StoredSession; - try { - // twice the least, so the host's token starts with more than the least left - session = await input.io.session({ - leastLeftMs: 2 * HOST_TOKEN_LEAST_LEFT_MS, - signIn: input.interactive, - }); - } catch (error) { - return stop( - `not signed in to ${input.address} (${error instanceof Error ? error.message : String(error)}).`, - ); - } - try { + // twice the least, so the host's token starts with more than the least left + const leastLeftMs = 2 * HOST_TOKEN_LEAST_LEFT_MS; + const tokenBy = async (session: StoredSession) => { const { token, expiresAt } = await exchangedHostToken({ tokenEndpoint: session.tokenEndpoint, subjectToken: session.token, @@ -238,10 +229,28 @@ expiresAt, }); return credentialOf(token); + }; + let session: StoredSession; + try { + session = await input.io.session({ leastLeftMs, signIn: input.interactive }); } catch (error) { return stop( - `${input.address} gave no token for ${fields.host} (${error instanceof Error ? error.message : String(error)}).`, + `not signed in to ${input.address} (${error instanceof Error ? error.message : String(error)}).`, ); + } + try { + return await tokenBy(session); + } catch (refusal) { + const why = (error: unknown) => + `${input.address} gave no token for ${fields.host} (${error instanceof Error ? error.message : String(error)}).`; + // the kept session was ended at the mind: with a person there, sign in again, once + if (!input.interactive) return stop(why(refusal)); + try { + input.io.say(`iterate: your sign-in to ${input.address} has ended.`); + return await tokenBy(await input.io.session({ leastLeftMs, signIn: true, anew: true })); + } catch (error) { + return stop(why(error)); + } } }