Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api/admin/integration): new endpoint and tweak in url path

Gergő Móricz47ad0e5c2092
diff --git a/apps/api/src/controllers/v0/admin/create-user.ts b/apps/api/src/controllers/v0/admin/create-user.ts
index 2d0936d86..e832ca6fc 100644
--- a/apps/api/src/controllers/v0/admin/create-user.ts
+++ b/apps/api/src/controllers/v0/admin/create-user.ts
@@ -30,7 +30,7 @@ async function addCoupon(teamId: string, integration: any) {
}
}
-export async function createUserController(req: Request, res: Response) {
+export async function integCreateUserController(req: Request, res: Response) {
let logger = _logger.child({
module: "v0/admin/create-user",
method: "createUserController",
diff --git a/apps/api/src/controllers/v0/admin/validate-api-key.ts b/apps/api/src/controllers/v0/admin/validate-api-key.ts
new file mode 100644
index 000000000..f4661ff1b
--- /dev/null
+++ b/apps/api/src/controllers/v0/admin/validate-api-key.ts
@@ -0,0 +1,133 @@
+import { logger as _logger } from "../../../lib/logger";
+import { Request, Response } from "express";
+import { supabase_service } from "../../../services/supabase";
+import crypto from "crypto";
+import { z } from "zod";
+
+export async function integValidateApiKeyController(
+ req: Request,
+ res: Response,
+) {
+ let logger = _logger.child({
+ module: "v0/admin/validate-api-key",
+ method: "validateApiKeyController",
+ });
+
+ try {
+ const auth = req.headers.authorization;
+ if (!auth) {
+ return res.status(401).json({ error: "Unauthorized" });
+ }
+
+ const [type, token] = auth.split(" ");
+ if (type !== "Bearer") {
+ return res.status(401).json({ error: "Unauthorized" });
+ }
+
+ // sha-256 hash the token
+ if (!token) {
+ return res.status(401).json({ error: "Unauthorized" });
+ }
+ const hashedToken = crypto.createHash("sha256").update(token).digest("hex");
+
+ // Look up integration by key
+ const { data: integration, error: integrationError } =
+ await supabase_service
+ .from("user_referring_integration")
+ .select("*")
+ .eq("key", hashedToken)
+ .single();
+
+ if (integrationError || !integration) {
+ return res.status(401).json({ error: "Unauthorized" });
+ }
+
+ logger = logger.child({
+ integration: integration.slug,
+ });
+
+ const bodySchema = z.object({
+ apiKey: z.string(),
+ });
+
+ const body = bodySchema.parse(req.body);
+
+ const normalizedApiKey = body.apiKey.replace(/^fc-/, "");
+
+ const { data: apiKeyData, error: apiKeyError } = await supabase_service
+ .from("api_keys")
+ .select("*")
+ .eq("key", normalizedApiKey)
+ .limit(1);
+
+ if (apiKeyError) {
+ throw apiKeyError;
+ }
+
+ if (!apiKeyData || apiKeyData.length === 0) {
+ return res.status(404).json({ error: "API key not identifiable" });
+ }
+
+ const teamId = apiKeyData[0].team_id;
+
+ logger = logger.child({
+ teamId,
+ });
+
+ const { data: teamData, error: teamError } = await supabase_service
+ .from("teams")
+ .select("*")
+ .eq("id", teamId)
+ .limit(1);
+ if (teamError) {
+ throw teamError;
+ }
+
+ if (!teamData || teamData.length === 0) {
+ return res.status(404).json({ error: "API key not identifiable" });
+ }
+
+ const team = teamData[0];
+
+ if (team.referrer_integration !== integration.slug) {
+ return res.status(404).json({ error: "API key not identifiable" });
+ }
+
+ const { data: userTeams, error: userTeamsError } = await supabase_service
+ .from("user_teams")
+ .select("*")
+ .eq("team_id", teamId)
+ .limit(1);
+ if (userTeamsError) {
+ throw userTeamsError;
+ }
+
+ if (!userTeams || userTeams.length === 0) {
+ throw new Error("user_teams in invalid state");
+ }
+
+ const userId = userTeams[0].user_id;
+
+ const { data: userData, error: userError } = await supabase_service
+ .from("users")
+ .select("*")
+ .eq("id", userId)
+ .limit(1);
+ if (userError) {
+ throw userError;
+ }
+
+ if (!userData || userData.length === 0) {
+ throw new Error("users in invalid state");
+ }
+
+ const user = userData[0];
+
+ return res.status(200).json({
+ email: user.email,
+ });
+ } catch (error) {
+ logger.error("Error validating API key", { error });
+ return res.status(500).json({ error: "Internal server error" });
+ }
+}
diff --git a/apps/api/src/routes/admin.ts b/apps/api/src/routes/admin.ts
index 0e2d19d3c..9ec91598c 100644
--- a/apps/api/src/routes/admin.ts
+++ b/apps/api/src/routes/admin.ts
@@ -14,7 +14,8 @@ import {
import { crawlCheckController } from "../controllers/v0/admin/crawl-check";
import { realtimeSearchController } from "../controllers/v2/f-search";
import { concurrencyQueueBackfillController } from "../controllers/v0/admin/concurrency-queue-backfill";
-import { createUserController } from "../controllers/v0/admin/create-user";
+import { integCreateUserController } from "../controllers/v0/admin/create-user";
+import { integValidateApiKeyController } from "../controllers/v0/admin/validate-api-key";
export const adminRouter = express.Router();
@@ -78,4 +79,12 @@ adminRouter.post(
wrap(concurrencyQueueBackfillController),
);
-adminRouter.post(`/admin/create-user`, wrap(createUserController));
+adminRouter.post(
+ `/admin/integration/create-user`,
+ wrap(integCreateUserController),
+);
+
+adminRouter.post(
+ `/admin/integration/validate-api-key`,
+ wrap(integValidateApiKeyController),
+);