feat: license creation admin PR (#15024)
* WIP: UI created to see if it matches RFC idea * Remove schema as we validate on the backend with old+new key schemas * Add comment about proxy URL request when on older versions * feat: create custom axios instance to use signature middleware * fix: update form to respect new schema * Add cal signature token to trubo env * use backend mutation for creating license keys * add loading state to creation button * update form to have a success state * fix type error for console.warning :') * use api version v1 * restore checkLicense from main * Add env vars to example * nit: rename page export * use US spelling --------- Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
1 parent
a574e5e60f
commit
2a41cd0770
9 files changed
+435
No files matched your search
@@ -2,6 +2,7 @@ import { z } from "zod";
|
||||
|
||||
import { authedAdminProcedure } from "../../../procedures/authedProcedure";
|
||||
import { router, importHandler } from "../../../trpc";
|
||||
import { ZCreateSelfHostedLicenseSchema } from "./createSelfHostedLicenseKey.schema";
|
||||
import { ZListMembersSchema } from "./listPaginated.schema";
|
||||
import { ZAdminLockUserAccountSchema } from "./lockUserAccount.schema";
|
||||
import { ZAdminRemoveTwoFactor } from "./removeTwoFactor.schema";
|
||||
@@ -62,4 +63,13 @@ export const adminRouter = router({
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
createSelfHostedLicense: authedAdminProcedure
|
||||
.input(ZCreateSelfHostedLicenseSchema)
|
||||
.mutation(async (opts) => {
|
||||
const handler = await importHandler(
|
||||
namespaced("createSelfHostedLicense"),
|
||||
() => import("./createSelfHostedLicenseKey.handler")
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import * as crypto from "crypto";
|
||||
import { z } from "zod";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
import type { TCreateSelfHostedLicenseSchema } from "./createSelfHostedLicenseKey.schema";
|
||||
|
||||
type GetOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TCreateSelfHostedLicenseSchema;
|
||||
};
|
||||
|
||||
const generateNonce = (): string => {
|
||||
return crypto.randomBytes(16).toString("hex");
|
||||
};
|
||||
|
||||
// Utility function to create a signature
|
||||
const createSignature = (body: Record<string, unknown>, nonce: string, secretKey: string): string => {
|
||||
return crypto
|
||||
.createHmac("sha256", secretKey)
|
||||
.update(JSON.stringify(body) + nonce)
|
||||
.digest("hex");
|
||||
};
|
||||
|
||||
// Fetch wrapper function
|
||||
const fetchWithSignature = async (
|
||||
url: string,
|
||||
body: Record<string, unknown>,
|
||||
secretKey: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<Response> => {
|
||||
const nonce = generateNonce();
|
||||
const signature = createSignature(body, nonce, secretKey);
|
||||
|
||||
const headers = {
|
||||
...options.headers,
|
||||
"Content-Type": "application/json",
|
||||
nonce: nonce,
|
||||
signature: signature,
|
||||
};
|
||||
|
||||
return await fetch(url, {
|
||||
...options,
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
};
|
||||
|
||||
const createSelfHostedInstance = async ({ input, ctx }: GetOptions) => {
|
||||
const privateApiUrl = process.env.CALCOM_PRIVATE_API_ROUTE;
|
||||
const signatureToken = process.env.CAL_SIGNATURE_TOKEN;
|
||||
|
||||
if (!privateApiUrl || !signatureToken) {
|
||||
throw new Error("Private Api route does not exist in .env");
|
||||
}
|
||||
|
||||
// Ensure admin
|
||||
if (ctx.user.role !== "ADMIN") {
|
||||
console.warn(`${ctx.user.username} just tried to create a license key without permission`);
|
||||
throw new Error("You do not have permission to do this.");
|
||||
}
|
||||
|
||||
const request = await fetchWithSignature(`${privateApiUrl}/v1/license`, input, signatureToken, {
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
const data = await request.json();
|
||||
const schema = z.object({
|
||||
stripeCheckoutUrl: z.string(),
|
||||
});
|
||||
|
||||
return schema.parse(data);
|
||||
};
|
||||
|
||||
export default createSelfHostedInstance;
|
||||
@@ -0,0 +1,15 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const BillingType = z.enum(["PER_BOOKING", "PER_USER"]);
|
||||
const BillingPeriod = z.enum(["MONTHLY", "ANNUALLY"]);
|
||||
|
||||
export const ZCreateSelfHostedLicenseSchema = z.object({
|
||||
billingType: BillingType,
|
||||
entityCount: z.number().int().nonnegative(),
|
||||
entityPrice: z.number().nonnegative(),
|
||||
billingPeriod: BillingPeriod,
|
||||
overages: z.number().nonnegative(),
|
||||
billingEmail: z.string().email(),
|
||||
});
|
||||
|
||||
export type TCreateSelfHostedLicenseSchema = z.infer<typeof ZCreateSelfHostedLicenseSchema>;
|
||||
Reference in new issue
Block a user