style: configure prettier (no semicolons, trailing commas)
This commit is contained in:
+68
-74
@@ -5,21 +5,21 @@
|
||||
* website POSTs the user's API key to /callback after they authenticate.
|
||||
*/
|
||||
|
||||
import { createServer, type Server } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import { createServer, type Server } from "node:http"
|
||||
import type { AddressInfo } from "node:net"
|
||||
|
||||
export interface AuthCallback {
|
||||
apiKey: string;
|
||||
state: string;
|
||||
userId: string;
|
||||
userName: string;
|
||||
keyName: string;
|
||||
apiKey: string
|
||||
state: string
|
||||
userId: string
|
||||
userName: string
|
||||
keyName: string
|
||||
}
|
||||
|
||||
export interface AuthServer {
|
||||
server: Server;
|
||||
port: number;
|
||||
waitForCallback: Promise<AuthCallback>;
|
||||
server: Server
|
||||
port: number
|
||||
waitForCallback: Promise<AuthCallback>
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,127 +29,121 @@ export interface AuthServer {
|
||||
* The server accepts exactly one valid POST to /callback and then closes.
|
||||
*/
|
||||
export function startAuthServer(): Promise<AuthServer> {
|
||||
let resolveCallback: (value: AuthCallback) => void;
|
||||
let rejectCallback: (error: Error) => void;
|
||||
let resolveCallback: (value: AuthCallback) => void
|
||||
let rejectCallback: (error: Error) => void
|
||||
|
||||
const waitForCallback = new Promise<AuthCallback>((resolve, reject) => {
|
||||
resolveCallback = resolve;
|
||||
rejectCallback = reject;
|
||||
});
|
||||
resolveCallback = resolve
|
||||
rejectCallback = reject
|
||||
})
|
||||
|
||||
const server = createServer((req, res) => {
|
||||
// CORS: allow requests from Command Code domains and localhost for dev
|
||||
const origin = req.headers.origin || "";
|
||||
const origin = req.headers.origin || ""
|
||||
const allowedOrigins = [
|
||||
"http://localhost:3000",
|
||||
"https://staging.commandcode.ai",
|
||||
"https://commandcode.ai",
|
||||
];
|
||||
const responseOrigin = allowedOrigins.includes(origin)
|
||||
? origin
|
||||
: allowedOrigins[0];
|
||||
]
|
||||
const responseOrigin = allowedOrigins.includes(origin) ? origin : allowedOrigins[0]
|
||||
|
||||
res.setHeader("Access-Control-Allow-Origin", responseOrigin);
|
||||
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.setHeader("Access-Control-Allow-Origin", responseOrigin)
|
||||
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type")
|
||||
res.setHeader("Content-Type", "application/json")
|
||||
|
||||
// Handle CORS preflight
|
||||
if (req.method === "OPTIONS") {
|
||||
res.writeHead(204);
|
||||
res.end();
|
||||
return;
|
||||
res.writeHead(204)
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
|
||||
if (req.url !== "/callback") {
|
||||
res.writeHead(404);
|
||||
res.end(JSON.stringify({ success: false, error: "Not found" }));
|
||||
return;
|
||||
res.writeHead(404)
|
||||
res.end(JSON.stringify({ success: false, error: "Not found" }))
|
||||
return
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
res.writeHead(405);
|
||||
res.writeHead(405)
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
error: "Method not allowed. Use POST.",
|
||||
}),
|
||||
);
|
||||
return;
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
let body = "";
|
||||
let body = ""
|
||||
req.on("data", (chunk) => {
|
||||
body += chunk.toString();
|
||||
if (body.length > 10_000) req.destroy();
|
||||
});
|
||||
body += chunk.toString()
|
||||
if (body.length > 10_000) req.destroy()
|
||||
})
|
||||
|
||||
req.on("end", () => {
|
||||
try {
|
||||
const parsed = JSON.parse(body) as Record<string, unknown>;
|
||||
const parsed = JSON.parse(body) as Record<string, unknown>
|
||||
|
||||
if (parsed.error) {
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify({ success: true }));
|
||||
res.writeHead(200)
|
||||
res.end(JSON.stringify({ success: true }))
|
||||
const description =
|
||||
typeof parsed.error_description === "string"
|
||||
? parsed.error_description
|
||||
: String(parsed.error);
|
||||
: String(parsed.error)
|
||||
if (parsed.error === "access_denied") {
|
||||
rejectCallback(
|
||||
new Error(description || "Authorization was denied by the user"),
|
||||
);
|
||||
rejectCallback(new Error(description || "Authorization was denied by the user"))
|
||||
} else {
|
||||
rejectCallback(new Error(description || String(parsed.error)));
|
||||
rejectCallback(new Error(description || String(parsed.error)))
|
||||
}
|
||||
server.close();
|
||||
return;
|
||||
server.close()
|
||||
return
|
||||
}
|
||||
|
||||
const apiKey = typeof parsed.apiKey === "string" ? parsed.apiKey : "";
|
||||
const state = typeof parsed.state === "string" ? parsed.state : "";
|
||||
const userId = typeof parsed.userId === "string" ? parsed.userId : "";
|
||||
const userName =
|
||||
typeof parsed.userName === "string" ? parsed.userName : "";
|
||||
const keyName =
|
||||
typeof parsed.keyName === "string" ? parsed.keyName : "";
|
||||
const apiKey = typeof parsed.apiKey === "string" ? parsed.apiKey : ""
|
||||
const state = typeof parsed.state === "string" ? parsed.state : ""
|
||||
const userId = typeof parsed.userId === "string" ? parsed.userId : ""
|
||||
const userName = typeof parsed.userName === "string" ? parsed.userName : ""
|
||||
const keyName = typeof parsed.keyName === "string" ? parsed.keyName : ""
|
||||
|
||||
if (!apiKey || !state || !userId || !userName || !keyName) {
|
||||
res.writeHead(400);
|
||||
res.writeHead(400)
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
error: "Missing required fields",
|
||||
}),
|
||||
);
|
||||
return;
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify({ success: true }));
|
||||
res.writeHead(200)
|
||||
res.end(JSON.stringify({ success: true }))
|
||||
|
||||
resolveCallback({ apiKey, state, userId, userName, keyName });
|
||||
server.close();
|
||||
resolveCallback({ apiKey, state, userId, userName, keyName })
|
||||
server.close()
|
||||
} catch {
|
||||
res.writeHead(400);
|
||||
res.end(JSON.stringify({ success: false, error: "Invalid JSON" }));
|
||||
res.writeHead(400)
|
||||
res.end(JSON.stringify({ success: false, error: "Invalid JSON" }))
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
req.on("error", () => {
|
||||
res.writeHead(500);
|
||||
res.end(JSON.stringify({ success: false, error: "Request error" }));
|
||||
});
|
||||
});
|
||||
res.writeHead(500)
|
||||
res.end(JSON.stringify({ success: false, error: "Request error" }))
|
||||
})
|
||||
})
|
||||
|
||||
return new Promise((resolve) => {
|
||||
server.on("error", (err: NodeJS.ErrnoException) => {
|
||||
rejectCallback(new Error(`Failed to start auth server: ${err.message}`));
|
||||
});
|
||||
rejectCallback(new Error(`Failed to start auth server: ${err.message}`))
|
||||
})
|
||||
|
||||
server.listen(0, "127.0.0.1", () => {
|
||||
const address = server.address() as AddressInfo;
|
||||
resolve({ server, port: address.port, waitForCallback });
|
||||
});
|
||||
});
|
||||
const address = server.address() as AddressInfo
|
||||
resolve({ server, port: address.port, waitForCallback })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user