fix(plugin): 🐛 Fix misleading error message related to plugin initialization

This commit is contained in:
Jesse Wierzbinski 2024-10-24 18:41:11 +02:00
parent 11bb0a6f49
commit 3b704b4c8c
No known key found for this signature in database
3 changed files with 65 additions and 33 deletions

3
app.ts
View file

@ -138,7 +138,8 @@ export const appFactory = async () => {
serverLogger.fatal`Put your configuration at ${chalk.blueBright( serverLogger.fatal`Put your configuration at ${chalk.blueBright(
"plugins.config.<plugin-name>", "plugins.config.<plugin-name>",
)}`; )}`;
throw new Error("Plugin configuration is invalid");
await Bun.sleep(Number.POSITIVE_INFINITY);
} }
// biome-ignore lint/complexity/useLiteralKeys: AddToApp is a private method // biome-ignore lint/complexity/useLiteralKeys: AddToApp is a private method

View file

@ -1,6 +1,7 @@
import { Hooks, Plugin } from "@versia/kit"; import { Hooks, Plugin } from "@versia/kit";
import chalk from "chalk"; import chalk from "chalk";
import { z } from "zod"; import { z } from "zod";
import { User } from "@versia/kit/db";
import authorizeRoute from "./routes/authorize.ts"; import authorizeRoute from "./routes/authorize.ts";
import jwksRoute from "./routes/jwks.ts"; import jwksRoute from "./routes/jwks.ts";
import ssoLoginCallbackRoute from "./routes/oauth/callback.ts"; import ssoLoginCallbackRoute from "./routes/oauth/callback.ts";
@ -26,8 +27,12 @@ const plugin = new Plugin(
}), }),
) )
.default([]), .default([]),
keys: z.object({ keys: z
public: z.string().transform(async (v) => { .object({
public: z
.string()
.min(1)
.transform(async (v) => {
try { try {
return await crypto.subtle.importKey( return await crypto.subtle.importKey(
"spki", "spki",
@ -38,11 +43,14 @@ const plugin = new Plugin(
); );
} catch { } catch {
throw new Error( throw new Error(
`Public key at keys.public is invalid. Run the ${chalk.bold("generate-keys")} command to generate a new keypair`, "Public key at oidc.keys.public is invalid",
); );
} }
}), }),
private: z.string().transform(async (v) => { private: z
.string()
.min(1)
.transform(async (v) => {
try { try {
return await crypto.subtle.importKey( return await crypto.subtle.importKey(
"pkcs8", "pkcs8",
@ -53,10 +61,25 @@ const plugin = new Plugin(
); );
} catch { } catch {
throw new Error( throw new Error(
`Private key at keys.private is invalid. Run the ${chalk.bold("generate-keys")} command to generate a new keypair`, "Private key at oidc.keys.private is invalid",
); );
} }
}), }),
})
.optional()
.transform(async (v, ctx) => {
if (!(v?.private && v?.public)) {
const { public_key, private_key } =
await User.generateKeys();
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Keys are missing, please add the following to your config:\n\nkeys.public: ${chalk.gray(public_key)}\nkeys.private: ${chalk.gray(private_key)}
`,
});
}
return v as Exclude<typeof v, undefined>;
}), }),
}), }),
); );

View file

@ -1,5 +1,6 @@
import { getLogger } from "@logtape/logtape"; import { getLogger } from "@logtape/logtape";
import chalk from "chalk"; import chalk from "chalk";
import { User } from "~/classes/database/user";
import type { Config } from "~/packages/config-manager"; import type { Config } from "~/packages/config-manager";
export const checkConfig = async (config: Config) => { export const checkConfig = async (config: Config) => {
@ -68,8 +69,15 @@ const checkFederationConfig = async (config: Config) => {
const logger = getLogger("server"); const logger = getLogger("server");
if (!(config.instance.keys.public && config.instance.keys.private)) { if (!(config.instance.keys.public && config.instance.keys.private)) {
logger.fatal`The federation keys are not set in the config at instance.keys.public and instance.keys.private`; logger.fatal`The federation keys are not set in the config`;
logger.fatal`You can generate a keypair using the CLI command ${chalk.bold("generate-keys")}`; logger.fatal`Below are generated keys for you to copy in the config at instance.keys.public and instance.keys.private`;
// Generate a key for them
const { public_key, private_key } = await User.generateKeys();
logger.fatal`Generated public key: ${chalk.gray(public_key)}`;
logger.fatal`Generated private key: ${chalk.gray(private_key)}`;
// Hang until Ctrl+C is pressed // Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY); await Bun.sleep(Number.POSITIVE_INFINITY);
} }