Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.8 kB · 55 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import * as Effect from "effect/Effect";import { OAuthError } from "./errors.ts";import { bodyBytes } from "../server/http.ts";
const privateHeaders = { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer", "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "frame-ancestors 'none'; base-uri 'self'; form-action 'self' https://dash.cloudflare.com",};export function privateResponse(response: Response) { const result = new Response(response.body, response); for (const [key, value] of Object.entries(privateHeaders)) result.headers.set(key, value); return result;}export function json(body: unknown, status = 200) { return privateResponse(Response.json(body, { status }));}export function redirect(path: string, cookies: string[] = []) { const response = new Response(null, { status: 303, headers: { Location: path }, }); for (const value of cookies) response.headers.append("Set-Cookie", value); return privateResponse(response);}export function checkOrigin(request: Request, origin: string) { return Effect.gen(function* () { if ( new URL(request.url).origin !== origin || request.headers.get("Origin") !== origin ) return yield* new OAuthError("forbidden"); });}export function form(request: Request) { return Effect.gen(function* () { if ( !request.headers .get("Content-Type") ?.startsWith("application/x-www-form-urlencoded") || Number(request.headers.get("Content-Length")) > 2048 ) return yield* new OAuthError("invalid_request"); if (!request.body) return new URLSearchParams(); const bytes = yield* bodyBytes( new Response(request.body), 2048, new OAuthError("invalid_request"), ); return new URLSearchParams(new TextDecoder().decode(bytes)); });}