mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-15 08:49:59 +02:00
send email upon generate virtual api key
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db, userOrgs, virtualApiKeys } from "@server/db";
|
||||
import { db, orgs, userOrgs, virtualApiKeys } from "@server/db";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
@@ -18,6 +18,10 @@ import {
|
||||
} from "@server/lib/virtualApiKey";
|
||||
import type { CreateOrEditVirtualApiKeyResponse } from "@server/routers/virtualApiKey/types";
|
||||
import { createVirtualApiKeyBodySchema } from "@server/routers/virtualApiKey/validation";
|
||||
import {
|
||||
resolveVirtualApiKeyEmailRecipients,
|
||||
sendVirtualApiKeyEmails
|
||||
} from "@server/lib/sendVirtualApiKeyEmail";
|
||||
|
||||
const paramsSchema = z.strictObject({
|
||||
orgId: z.string().nonempty()
|
||||
@@ -78,7 +82,10 @@ export async function createVirtualApiKey(
|
||||
userId,
|
||||
allResources,
|
||||
resourceIds,
|
||||
validForSeconds
|
||||
validForSeconds,
|
||||
sendEmail: doEmail,
|
||||
sendToAttributedUser,
|
||||
emails
|
||||
} = parsedBody.data;
|
||||
|
||||
if (req.user && orgId && orgId !== req.userOrgId) {
|
||||
@@ -121,6 +128,18 @@ export async function createVirtualApiKey(
|
||||
);
|
||||
}
|
||||
|
||||
const emailRecipients = await resolveVirtualApiKeyEmailRecipients({
|
||||
sendEmail: doEmail,
|
||||
sendToAttributedUser,
|
||||
userId,
|
||||
emails
|
||||
});
|
||||
if (!emailRecipients.ok) {
|
||||
return next(
|
||||
createHttpError(HttpCode.BAD_REQUEST, emailRecipients.message)
|
||||
);
|
||||
}
|
||||
|
||||
const minted = mintVirtualApiKeySecret();
|
||||
const expiresAt = validForSeconds
|
||||
? createDate(new TimeSpan(validForSeconds, "s")).getTime()
|
||||
@@ -156,6 +175,24 @@ export async function createVirtualApiKey(
|
||||
return row;
|
||||
});
|
||||
|
||||
if (emailRecipients.recipients.length > 0) {
|
||||
const [org] = await db
|
||||
.select()
|
||||
.from(orgs)
|
||||
.where(eq(orgs.orgId, orgId))
|
||||
.limit(1);
|
||||
|
||||
await sendVirtualApiKeyEmails({
|
||||
recipients: emailRecipients.recipients,
|
||||
orgName: org?.name || orgId,
|
||||
orgId,
|
||||
keyName: created.name,
|
||||
virtualApiKeyId: created.virtualApiKeyId,
|
||||
secret: minted.secret,
|
||||
allResources: created.allResources
|
||||
});
|
||||
}
|
||||
|
||||
return response<CreateOrEditVirtualApiKeyResponse>(res, {
|
||||
data: {
|
||||
virtualApiKey: {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
db,
|
||||
orgs,
|
||||
userOrgs,
|
||||
virtualApiKeyResources,
|
||||
virtualApiKeys
|
||||
@@ -16,11 +17,16 @@ import { and, eq } from "drizzle-orm";
|
||||
import { createDate, TimeSpan } from "oslo";
|
||||
import {
|
||||
assertManualKeyResourcesInOrg,
|
||||
decryptVirtualApiKeyToken,
|
||||
replaceVirtualApiKeyResources,
|
||||
toPublicVirtualApiKey
|
||||
} from "@server/lib/virtualApiKey";
|
||||
import type { CreateOrEditVirtualApiKeyResponse } from "@server/routers/virtualApiKey/types";
|
||||
import { updateVirtualApiKeyBodySchema } from "@server/routers/virtualApiKey/validation";
|
||||
import {
|
||||
resolveVirtualApiKeyEmailRecipients,
|
||||
sendVirtualApiKeyEmails
|
||||
} from "@server/lib/sendVirtualApiKeyEmail";
|
||||
|
||||
const paramsSchema = z.strictObject({
|
||||
virtualApiKeyId: z.string().nonempty()
|
||||
@@ -146,6 +152,20 @@ export async function updateVirtualApiKey(
|
||||
}
|
||||
}
|
||||
|
||||
const nextUserId =
|
||||
body.userId !== undefined ? body.userId : existing.userId;
|
||||
const emailRecipients = await resolveVirtualApiKeyEmailRecipients({
|
||||
sendEmail: body.sendEmail,
|
||||
sendToAttributedUser: body.sendToAttributedUser,
|
||||
userId: nextUserId,
|
||||
emails: body.emails
|
||||
});
|
||||
if (!emailRecipients.ok) {
|
||||
return next(
|
||||
createHttpError(HttpCode.BAD_REQUEST, emailRecipients.message)
|
||||
);
|
||||
}
|
||||
|
||||
const updates: Partial<typeof virtualApiKeys.$inferInsert> = {};
|
||||
|
||||
if (body.name !== undefined) {
|
||||
@@ -192,6 +212,24 @@ export async function updateVirtualApiKey(
|
||||
return row;
|
||||
});
|
||||
|
||||
if (emailRecipients.recipients.length > 0) {
|
||||
const [org] = await db
|
||||
.select()
|
||||
.from(orgs)
|
||||
.where(eq(orgs.orgId, existing.orgId))
|
||||
.limit(1);
|
||||
|
||||
await sendVirtualApiKeyEmails({
|
||||
recipients: emailRecipients.recipients,
|
||||
orgName: org?.name || existing.orgId,
|
||||
orgId: existing.orgId,
|
||||
keyName: updated.name,
|
||||
virtualApiKeyId: updated.virtualApiKeyId,
|
||||
secret: decryptVirtualApiKeyToken(updated.token),
|
||||
allResources: updated.allResources
|
||||
});
|
||||
}
|
||||
|
||||
const resourceRows = await db
|
||||
.select({ resourceId: virtualApiKeyResources.resourceId })
|
||||
.from(virtualApiKeyResources)
|
||||
|
||||
@@ -4,6 +4,43 @@ export const virtualApiKeyResourceIdsSchema = z
|
||||
.array(z.coerce.number().int().positive())
|
||||
.optional();
|
||||
|
||||
const virtualApiKeyEmailFieldsSchema = {
|
||||
sendEmail: z.boolean().optional().default(false),
|
||||
sendToAttributedUser: z.boolean().optional().default(false),
|
||||
emails: z.array(z.email().toLowerCase()).max(20).optional().default([])
|
||||
};
|
||||
|
||||
function refineVirtualApiKeyEmailFields(
|
||||
data: {
|
||||
sendEmail: boolean;
|
||||
sendToAttributedUser: boolean;
|
||||
emails: string[];
|
||||
userId?: string | null;
|
||||
},
|
||||
ctx: z.RefinementCtx
|
||||
) {
|
||||
if (!data.sendEmail) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.sendToAttributedUser && data.emails.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message:
|
||||
"Select the associated user or add at least one email address",
|
||||
path: ["sendEmail"]
|
||||
});
|
||||
}
|
||||
|
||||
if (data.sendToAttributedUser && !data.userId) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Associate a user to email the key to that user",
|
||||
path: ["sendToAttributedUser"]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const createVirtualApiKeyBodySchema = z
|
||||
.strictObject({
|
||||
name: z.string().nonempty(),
|
||||
@@ -11,7 +48,8 @@ export const createVirtualApiKeyBodySchema = z
|
||||
userId: z.string().optional().nullable(),
|
||||
allResources: z.boolean().optional().default(false),
|
||||
resourceIds: virtualApiKeyResourceIdsSchema,
|
||||
validForSeconds: z.int().positive().optional()
|
||||
validForSeconds: z.int().positive().optional(),
|
||||
...virtualApiKeyEmailFieldsSchema
|
||||
})
|
||||
.refine(
|
||||
(data) => data.allResources || (data.resourceIds?.length ?? 0) > 0,
|
||||
@@ -20,13 +58,30 @@ export const createVirtualApiKeyBodySchema = z
|
||||
"Select at least one public inference resource, or enable all public inference resources",
|
||||
path: ["resourceIds"]
|
||||
}
|
||||
);
|
||||
)
|
||||
.superRefine(refineVirtualApiKeyEmailFields);
|
||||
|
||||
export const updateVirtualApiKeyBodySchema = z.strictObject({
|
||||
name: z.string().nonempty().optional(),
|
||||
description: z.string().optional().nullable(),
|
||||
userId: z.string().optional().nullable(),
|
||||
allResources: z.boolean().optional(),
|
||||
resourceIds: virtualApiKeyResourceIdsSchema,
|
||||
validForSeconds: z.int().positive().optional().nullable()
|
||||
});
|
||||
export const updateVirtualApiKeyBodySchema = z
|
||||
.strictObject({
|
||||
name: z.string().nonempty().optional(),
|
||||
description: z.string().optional().nullable(),
|
||||
userId: z.string().optional().nullable(),
|
||||
allResources: z.boolean().optional(),
|
||||
resourceIds: virtualApiKeyResourceIdsSchema,
|
||||
validForSeconds: z.int().positive().optional().nullable(),
|
||||
...virtualApiKeyEmailFieldsSchema
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (!data.sendEmail) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.sendToAttributedUser && data.emails.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message:
|
||||
"Select the associated user or add at least one email address",
|
||||
path: ["sendEmail"]
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user