Skip to content

Commit

A data request or form post refused for its access token now says so with a WWW-Authenticate header, which the Worker adds since React Router drops the headers of what middleware throws, and the token form's warning says plainly that the website does not hold a token to its permissions.

syntaqxcommitted Parente683d07Browse files
2 files+9−50/2 viewed
+3−4
5151 */
5252 export const viewerMiddleware: MiddlewareFunction<Response> = async ({ request, context }, next) => {
5353 const verdict = await tokenVerdict(request, (token) => identity.userForAccessToken(token));
54− if (verdict.kind === "refused") {
55− const headers: HeadersInit = verdict.status === 401 ? { "www-authenticate": TOKEN_CHALLENGE } : {};
56− throw data(verdict.body, { status: verdict.status, headers });
57− }
54+ // Thrown, so pages and client navigations show it as any error; the
55+ // Worker adds the 401's challenge header (workers/app.ts).
56+ if (verdict.kind === "refused") throw data(verdict.body, { status: verdict.status });
5857 if (verdict.kind === "signed-out") {
5958 // The page as anyone signed out sees it, saying the token was not taken.
6059 const response = await next();
+6−1
1212 import { repositoryOfPage, stillPublic } from "../app/lib/public-cache";
1313 import { registryWorkspace, servicePath } from "../app/lib/registry-paths";
1414 import { usercontentPath } from "../app/lib/usercontent";
15+import { TOKEN_CHALLENGE } from "../app/lib/website-token";
1516 import { serveUsercontent } from "./usercontent";
1617
1718 const loadBuild = () => import("virtual:react-router/server-build");
103104 // and keeps the reader's D1 bookmarks (app/lib/perf.server.ts).
104105 const render = () => withRequestPerf(request, async () => finishResponse(request, await requestHandler(request)));
105106 if (anonymousPage(request, pathname)) return servePublic(env, request, ctx, render);
106− return render();
107+ const answer = await render();
108+ // A token the site refused (app/lib/website-token.ts) is told so as the
109+ // API tells it: React Router drops the headers of what middleware throws.
110+ if (answer.status === 401 && request.headers.has("authorization")) answer.headers.set("www-authenticate", TOKEN_CHALLENGE);
111+ return answer;
107112 }
108113
109114 /**