peppa/apps/web/app/oauth/authorize/actions.ts
Асель 39367b1d82 PEP-8: remote MCP for ChatGPT with Peppa as OAuth 2.1 server
- OAuth: RFC 8414/9728 metadata, RFC 7591 dynamic registration,
  authorization code + PKCE S256 with a consent screen after Gitea login,
  refresh token rotation; tokens and codes stored hashed; revoke in web
- /mcp: stateless Streamable HTTP, Bearer → user; PM tools from
  @peppa/mcp/manager: projects, goals, tasks, create, move (board
  semantics, starts agents), inbox, answer/accept — owner-scoped
- login keeps ?next= through Gitea OAuth; Settings page with MCP URL and
  connected apps
- 5 new tests (53 e2e total, green); verified on prod as a client:
  DCR → consent → token → MCP over HTTPS → revoke → 401
- OpenSpec: pep-8 archived into spec remote-mcp

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:38:09 +05:00

36 lines
1.2 KiB
TypeScript

"use server";
import { issueCode } from "@peppa/core";
import { redirect } from "next/navigation";
import { requireUser } from "@/lib/auth";
import { db } from "@/lib/db";
const read = (f: FormData) => ({
clientId: String(f.get("client_id")),
redirectUri: String(f.get("redirect_uri")),
codeChallenge: String(f.get("code_challenge")),
codeChallengeMethod: String(f.get("code_challenge_method")),
responseType: String(f.get("response_type")),
scope: String(f.get("scope") || "") || undefined,
});
function back(redirectUri: string, params: Record<string, string>, state: string) {
const u = new URL(redirectUri);
for (const [k, v] of Object.entries(params)) u.searchParams.set(k, v);
if (state) u.searchParams.set("state", state);
return u.toString();
}
export async function approve(f: FormData) {
const user = await requireUser();
const r = read(f);
const code = await issueCode(db, r, user.id);
redirect(back(r.redirectUri, { code }, String(f.get("state") ?? "")));
}
export async function deny(f: FormData) {
await requireUser();
const r = read(f);
redirect(back(r.redirectUri, { error: "access_denied" }, String(f.get("state") ?? "")));
}