refactor: download restic password from stored db password
This commit is contained in:
parent
22db579973
commit
e59a3eac2f
3 changed files with 39 additions and 8 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
import { createMiddleware } from "hono/factory";
|
import { createMiddleware } from "hono/factory";
|
||||||
import { auth } from "~/lib/auth";
|
import { auth } from "~/lib/auth";
|
||||||
|
import { db } from "~/server/db/db";
|
||||||
|
import { member } from "~/server/db/schema";
|
||||||
|
import { eq, and } from "drizzle-orm";
|
||||||
|
|
||||||
declare module "hono" {
|
declare module "hono" {
|
||||||
interface ContextVariableMap {
|
interface ContextVariableMap {
|
||||||
|
|
@ -33,3 +36,22 @@ export const requireAuth = createMiddleware(async (c, next) => {
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Middleware to require organization owner or admin role
|
||||||
|
* Verifies the user has the required role in the current organization
|
||||||
|
*/
|
||||||
|
export const requireOrgAdmin = createMiddleware(async (c, next) => {
|
||||||
|
const user = c.get("user");
|
||||||
|
const organizationId = c.get("organizationId");
|
||||||
|
|
||||||
|
const membership = await db.query.member.findFirst({
|
||||||
|
where: and(eq(member.userId, user.id), eq(member.organizationId, organizationId)),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!membership || (membership.role !== "owner" && membership.role !== "admin")) {
|
||||||
|
return c.json({ message: "Forbidden" }, 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
await next();
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,12 @@ import {
|
||||||
type UpdateInfoDto,
|
type UpdateInfoDto,
|
||||||
} from "./system.dto";
|
} from "./system.dto";
|
||||||
import { systemService } from "./system.service";
|
import { systemService } from "./system.service";
|
||||||
import { requireAuth } from "../auth/auth.middleware";
|
import { requireAuth, requireOrgAdmin } from "../auth/auth.middleware";
|
||||||
import { RESTIC_PASS_FILE } from "../../core/constants";
|
|
||||||
import { db } from "../../db/db";
|
import { db } from "../../db/db";
|
||||||
import { usersTable } from "../../db/schema";
|
import { organization, usersTable } from "../../db/schema";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { verifyUserPassword } from "../auth/helpers";
|
import { verifyUserPassword } from "../auth/helpers";
|
||||||
|
import { cryptoUtils } from "../../utils/crypto";
|
||||||
|
|
||||||
export const systemController = new Hono()
|
export const systemController = new Hono()
|
||||||
.use(requireAuth)
|
.use(requireAuth)
|
||||||
|
|
@ -30,10 +30,12 @@ export const systemController = new Hono()
|
||||||
})
|
})
|
||||||
.post(
|
.post(
|
||||||
"/restic-password",
|
"/restic-password",
|
||||||
|
requireOrgAdmin,
|
||||||
downloadResticPasswordDto,
|
downloadResticPasswordDto,
|
||||||
validator("json", downloadResticPasswordBodySchema),
|
validator("json", downloadResticPasswordBodySchema),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const user = c.get("user");
|
const user = c.get("user");
|
||||||
|
const organizationId = c.get("organizationId");
|
||||||
const body = c.req.valid("json");
|
const body = c.req.valid("json");
|
||||||
|
|
||||||
const isPasswordValid = await verifyUserPassword({ password: body.password, userId: user.id });
|
const isPasswordValid = await verifyUserPassword({ password: body.password, userId: user.id });
|
||||||
|
|
@ -42,8 +44,15 @@ export const systemController = new Hono()
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const file = Bun.file(RESTIC_PASS_FILE);
|
const org = await db.query.organization.findFirst({
|
||||||
const content = await file.text();
|
where: eq(organization.id, organizationId),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!org?.metadata?.resticPassword) {
|
||||||
|
return c.json({ message: "Organization Restic password not found" }, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const content = await cryptoUtils.resolveSecret(org.metadata.resticPassword);
|
||||||
|
|
||||||
await db.update(usersTable).set({ hasDownloadedResticPassword: true }).where(eq(usersTable.id, user.id));
|
await db.update(usersTable).set({ hasDownloadedResticPassword: true }).where(eq(usersTable.id, user.id));
|
||||||
|
|
||||||
|
|
@ -52,7 +61,7 @@ export const systemController = new Hono()
|
||||||
|
|
||||||
return c.text(content);
|
return c.text(content);
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
return c.json({ message: "Failed to read Restic password file" }, 500);
|
return c.json({ message: "Failed to retrieve Restic password" }, 500);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -65,12 +65,12 @@ export const downloadResticPasswordBodySchema = type({
|
||||||
});
|
});
|
||||||
|
|
||||||
export const downloadResticPasswordDto = describeRoute({
|
export const downloadResticPasswordDto = describeRoute({
|
||||||
description: "Download the Restic password file for backup recovery. Requires password re-authentication.",
|
description: "Download the organization's Restic password for backup recovery. Requires organization owner or admin role and password re-authentication.",
|
||||||
tags: ["System"],
|
tags: ["System"],
|
||||||
operationId: "downloadResticPassword",
|
operationId: "downloadResticPassword",
|
||||||
responses: {
|
responses: {
|
||||||
200: {
|
200: {
|
||||||
description: "Restic password file content",
|
description: "Organization's Restic password",
|
||||||
content: {
|
content: {
|
||||||
"text/plain": {
|
"text/plain": {
|
||||||
schema: { type: "string" },
|
schema: { type: "string" },
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue