Files
wg-easy-ca-lose/src/server/routes/cnf/[oneTimeLink].ts
T
Bernd StorathandGitHub 2af5cd04b4 chore: disable auto imports (#2672)
* disable auto imports

* fix imports

* improve cli imports

* fix imports

* fix import cycle

* fix imports
2026-06-19 15:24:42 +02:00

55 lines
1.4 KiB
TypeScript

import {
createError,
defineEventHandler,
getValidatedRouterParams,
setHeader,
} from 'h3';
import Database from '#server/utils/Database';
import WireGuard from '#server/utils/WireGuard';
import { validateZod } from '#server/utils/types';
import { OneTimeLinkGetSchema } from '#db/repositories/oneTimeLink/types';
export default defineEventHandler(async (event) => {
const { oneTimeLink } = await getValidatedRouterParams(
event,
validateZod(OneTimeLinkGetSchema, event)
);
const otl = await Database.oneTimeLinks.getByOtl(oneTimeLink);
if (!otl) {
throw createError({
statusCode: 404,
statusMessage: 'Invalid One Time Link',
});
}
if (new Date() > new Date(otl.expiresAt)) {
throw createError({
statusCode: 410,
statusMessage: 'One Time Link has expired',
});
}
const client = await Database.clients.get(otl.id);
if (!client) {
throw createError({
statusCode: 404,
statusMessage: 'Invalid One Time Link',
});
}
const config = await WireGuard.getClientConfiguration({
clientId: client.id,
});
await Database.oneTimeLinks.erase(otl.id);
setHeader(
event,
'Content-Disposition',
`attachment; filename="${WireGuard.cleanClientFilename(client.name) || client.id}.conf"`
);
setHeader(event, 'Content-Type', 'application/octet-stream');
return config;
});