cloudflare sign-in: the end of a brief access is tried and reported for the refresh token too
Jonas Templestein, 2026-11-27 09:31 UTC · 66ad702287816cdd3927e59e7ee9b9c4aabda8fb · parent 8d890b7
3 files changed
- M core/lib/src/cli/cloudflare-access.test.ts
- M core/lib/src/cli/cloudflare-access.ts
- M core/lib/src/cli/deploy.ts
M core/lib/src/cli/cloudflare-access.test.ts
@@ -239,6 +239,7 @@ const calls: { path: string; form: Record<string, string> }[] = []; let revokeFails = false; + let extra: Record<string, string> = {}; const cloudflareSays = (granted: string) => vi.stubGlobal("fetch", async (url: string | URL, init?: RequestInit) => { const path = new URL(String(url)).pathname; @@ -253,7 +254,7 @@ interval: 1, } : path.endsWith("/token") - ? { access_token: "billing-token", expires_in: 3600, scope: granted } + ? { access_token: "billing-token", expires_in: 3600, scope: granted, ...extra } : {}; if (path.endsWith("/revoke") && revokeFails) return new Response("{}", { status: 400 }); return new Response(JSON.stringify(answer), { headers: { "content-type": "application/json" } }); @@ -306,7 +307,11 @@ async () => "acted", async (ms) => void waits.push(ms), ); - expect(notEnded).toEqual({ lacks: [], done: "acted", notEnded: "HTTP 400" }); + expect(notEnded).toEqual({ + lacks: [], + done: "acted", + notEnded: "the access token: HTTP 400", + }); expect(revoked()).toEqual(["billing-token", "billing-token", "billing-token"]); expect(waits).toEqual([1000, 2000]); await expect( @@ -318,7 +323,18 @@ }, async () => {}, ), - ).rejects.toThrow("The act failed. The access for it was not ended at Cloudflare (HTTP 400)"); + ).rejects.toThrow( + "The act failed. The access for it was not ended at Cloudflare (the access token: HTTP 400)", + ); + + // a refresh token that came unasked: its end is tried three times and its failure is named + calls.length = 0; + extra = { refresh_token: "refresh-unasked" }; + const both = await briefAccess([BILLING_SCOPE], () => {}, async () => "acted", async () => {}); + expect(both.notEnded).toBe( + "the access token: HTTP 400; the refresh token, which does not end by itself: HTTP 400", + ); + expect(revoked().filter((token) => token === "refresh-unasked")).toHaveLength(3); } finally { vi.unstubAllGlobals(); }
M core/lib/src/cli/cloudflare-access.ts
@@ -427,7 +427,8 @@ * token, then the token's end at Cloudflare, whatever `act` did. The token is in no file and in * no other call of the run. A sign-in that did not grant every scope does not act: `lacks` names * what is missing. THE END IS ASKED FOR UP TO THREE TIMES, AND A FAILURE IS SAID: `notEnded` is - * Cloudflare's error when the token may still work (it then ends by itself within the hour), + * Cloudflare's error for each token that may still work (the access token then ends by itself + * within the hour; a refresh token does not), * and an `act` that throws carries the same words. It throws what the sign-in throws (refused, * or nobody approved in time): nothing was acted on then. */ export async function briefAccess<T>( @@ -437,14 +438,24 @@ pause: (ms: number) => Promise<unknown> = sleep, ): Promise<{ lacks: string[]; done?: T; notEnded?: string }> { const answer = await deviceSignIn(scopes, show); - const end = async () => { - await revoke(answer.refresh_token); + // each token's end is asked for up to three times; none was asked for a refresh token, and one + // that comes all the same lives longer than the hour, so its failure is named as such + const ended = async (token: string | undefined) => { for (let attempt = 1; ; attempt++) { - const problem = await revoke(answer.access_token); + const problem = await revoke(token); if (!problem || attempt === 3) return problem; await pause(attempt * 1000); } }; + const end = async () => { + const refresh = await ended(answer.refresh_token); + const access = await ended(answer.access_token); + const problems = [ + ...(access ? [`the access token: ${access}`] : []), + ...(refresh ? [`the refresh token, which does not end by itself: ${refresh}`] : []), + ]; + return problems.length > 0 ? problems.join("; ") : undefined; + }; const lacks = scopesLacking(scopes, answer.scope); let done: T | undefined; try { @@ -452,7 +463,7 @@ } catch (error) { const notEnded = await end(); if (notEnded && error instanceof Error) - error.message += ` The access for it was not ended at Cloudflare (${notEnded}): it ends by itself within the hour.`; + error.message += ` The access for it was not ended at Cloudflare (${notEnded}).`; throw error; } const notEnded = await end();
M core/lib/src/cli/deploy.ts
@@ -3274,7 +3274,7 @@ } if (typeof made !== "string" && made.notEnded) p.log.warn( - `Cloudflare did not confirm the end of the billing access (${made.notEnded}). It ends by itself within the hour. To end it now, remove this application's access in your Cloudflare profile.`, + `Cloudflare did not confirm the end of the billing access (${made.notEnded}). An access token ends by itself within the hour. Remove this application's access in your Cloudflare profile now.`, ); } if (!paid) {