feat: move totp and passkeys into plugins (C1)

This commit is contained in:
LukeGus committed 2026-09-24 16:57:24 -05:00
1 parent f4f26a9b8b
commit 5c18435105
235 files changed
+22545 -5610

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
import type { Router } from "express";
import type { PluginContext } from "@termix/plugin-sdk/backend";
import { enrollments } from "./tables.js";
import { createEnrollmentRepository } from "./repository.js";
import { createTotpService, FACTOR_ID, normalizeCode } from "./totp.js";
import { registerTotpRoutes } from "./routes.js";
export async function activate(ctx: PluginContext) {
const table = await ctx.db.define(enrollments);
const totp = createTotpService(
ctx,
createEnrollmentRepository(ctx.db, table),
);
ctx.auth.registerSecondFactor({
id: FACTOR_ID,
labelKey: "factor",
isEnrolled: (userId) => totp.isEnrolled(userId),
verify: async (userId, body) => {
const result = await totp.checkCode(
userId,
normalizeCode(body.totp_code ?? body.code),
);
if (result === null) {
return {
ok: false,
error:
"Your authenticator could not be read. Contact an admin to reset your second factor.",
};
}
return result;
},
reset: (userId) => totp.remove(userId),
});
registerTotpRoutes(ctx.http.router<Router>(), ctx, totp);
}
export async function deactivate() {
// Everything above was registered through ctx and is disposed by core.
}