···
1
+
import { getSessionAgent } from "../../../lib/sessions.ts";
2
+
import { define } from "../../../utils.ts";
3
+
import * as plc from "@did-plc/lib";
6
+
* Verify if a rotation key exists in the PLC document
8
+
* - key: The rotation key to verify
9
+
* @param ctx - The context object containing the request and response
10
+
* @returns A response object with the verification result
12
+
export const handler = define.handlers({
14
+
const res = new Response();
16
+
const body = await ctx.req.json();
17
+
const { key: newKey } = body;
18
+
console.log("Request body:", { newKey });
21
+
console.log("Missing key in request");
22
+
return new Response("Missing param key in request body", {
27
+
const agent = await getSessionAgent(ctx.req, res);
29
+
console.log("No agent found");
30
+
return new Response("Unauthorized", { status: 401 });
33
+
const session = await agent.com.atproto.server.getSession();
34
+
const did = session.data.did;
36
+
console.log("No DID found in session");
37
+
return new Response(
40
+
message: "No DID found in your session",
44
+
headers: { "Content-Type": "application/json" },
48
+
console.log("Using agent DID:", did);
50
+
// Fetch the PLC document to check rotation keys
51
+
console.log("Getting did:plc document...");
52
+
const plcClient = new plc.Client("https://plc.directory");
53
+
const didDoc = await plcClient.getDocumentData(did);
55
+
console.log("No DID document found for agent DID");
56
+
return new Response(
59
+
message: "No DID document found for your account",
63
+
headers: { "Content-Type": "application/json" },
67
+
console.log("Got DID document:", didDoc);
69
+
const rotationKeys = didDoc.rotationKeys ?? [];
70
+
if (!rotationKeys.length) {
71
+
console.log("No existing rotation keys found");
72
+
throw new Error("No rotation keys found in did:plc document");
75
+
// Check if the key exists in rotation keys
76
+
if (rotationKeys.includes(newKey)) {
77
+
return new Response(
80
+
message: "Rotation key exists in PLC document",
85
+
"Content-Type": "application/json",
86
+
...Object.fromEntries(res.headers), // Include session cookie headers
92
+
// If we get here, the key was not found
93
+
return new Response(
96
+
message: "Rotation key not found in PLC document",
100
+
headers: { "Content-Type": "application/json" },
104
+
console.error("PLC verification error:", error);
105
+
const errorMessage =
106
+
error instanceof Error
108
+
: "Failed to verify rotation key";
109
+
console.log("Sending error response:", errorMessage);
111
+
return new Response(
114
+
message: errorMessage,
116
+
error instanceof Error
119
+
message: error.message,
120
+
stack: error.stack,
126
+
headers: { "Content-Type": "application/json" },