Option to regenerate olm keys inside client

This commit is contained in:
Pallavi Kumari
2025-10-25 22:42:51 +05:30
parent 3f38080b46
commit c2f607bb9a
5 changed files with 255 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { Client, db, exitNodes, sites } from "@server/db";
import { Client, db, exitNodes, olms, sites } from "@server/db";
import { clients, clientSites } from "@server/db";
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
@@ -18,6 +18,7 @@ import {
deletePeer as olmDeletePeer
} from "../olm/peers";
import { sendToExitNode } from "#dynamic/lib/exitNodes";
import { hashPassword } from "@server/auth/password";
const updateClientParamsSchema = z
.object({
@@ -30,7 +31,10 @@ const updateClientSchema = z
name: z.string().min(1).max(255).optional(),
siteIds: z
.array(z.number().int().positive())
.optional()
.optional(),
olmId: z.string().min(1).optional(),
secret: z.string().min(1).optional(),
})
.strict();
@@ -75,7 +79,7 @@ export async function updateClient(
);
}
const { name, siteIds } = parsedBody.data;
const { name, siteIds, olmId, secret } = parsedBody.data;
const parsedParams = updateClientParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
@@ -89,6 +93,12 @@ export async function updateClient(
const { clientId } = parsedParams.data;
let secretHash = undefined;
if (secret) {
secretHash = await hashPassword(secret);
}
// Fetch the client to make sure it exists and the user has access to it
const [client] = await db
.select()
@@ -136,6 +146,22 @@ export async function updateClient(
.where(eq(clients.clientId, clientId));
}
const [existingOlm] = await trx
.select()
.from(olms)
.where(eq(olms.clientId, clientId))
.limit(1);
if (existingOlm && olmId && secretHash) {
await trx
.update(olms)
.set({
olmId,
secretHash
})
.where(eq(olms.clientId, clientId));
}
// Update site associations if provided
// Remove sites that are no longer associated
for (const siteId of sitesRemoved) {