In order to minimise the window of opportunity to steal a session, automatically rotate it onto a new session on a frequent basis. This makes a session cookie older than the automatic rollover time less likely to grant access and more likely to be detected. Should a stolen session cookie get rotated while the attacker is using it, the user will be notificed that their session has been taken the next time they open the app if the user re-visits the website before the session is discarded.
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import { readUsers, writeUsers } from "~/server/database";
|
|
import { DateTime, Info } from "~/shared/utils/luxon";
|
|
import { apiAccountPatchSchema } from "~/shared/types/api";
|
|
import { z } from "zod/v4-mini";
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const session = await requireServerSessionWithUser(event);
|
|
const { success, error, data: patch } = apiAccountPatchSchema.safeParse(await readBody(event));
|
|
if (!success) {
|
|
throw createError({
|
|
status: 400,
|
|
statusText: "Bad Request",
|
|
message: z.prettifyError(error),
|
|
});
|
|
}
|
|
|
|
if (patch.timezone?.length) {
|
|
const zone = Info.normalizeZone(patch.timezone);
|
|
if (!zone.isValid) {
|
|
throw createError({
|
|
status: 400,
|
|
message: `Invalid timezone: the zone "${patch.timezone} is not supported`,
|
|
});
|
|
}
|
|
}
|
|
if (patch.locale?.length) {
|
|
const locale = DateTime.local({ locale: patch.locale }).resolvedLocaleOptions().locale;
|
|
if (locale !== patch.locale) {
|
|
throw createError({
|
|
status: 400,
|
|
message: `Invalid locale: the locale "${patch.locale}" is not supported (was treated as "${locale}")`
|
|
});
|
|
}
|
|
}
|
|
|
|
const users = await readUsers();
|
|
const account = users.find(user => user.id === session.accountId);
|
|
if (!account) {
|
|
throw Error("Account does not exist");
|
|
}
|
|
|
|
if (patch.interestedEventIds !== undefined) {
|
|
if (patch.interestedEventIds.length) {
|
|
account.interestedEventIds = patch.interestedEventIds;
|
|
} else {
|
|
delete account.interestedEventIds;
|
|
}
|
|
}
|
|
if (patch.interestedEventSlotIds !== undefined) {
|
|
if (patch.interestedEventSlotIds.length) {
|
|
account.interestedEventSlotIds = patch.interestedEventSlotIds;
|
|
} else {
|
|
delete account.interestedEventSlotIds;
|
|
}
|
|
}
|
|
if (patch.timezone !== undefined) {
|
|
if (patch.timezone)
|
|
account.timezone = patch.timezone;
|
|
else
|
|
delete account.timezone;
|
|
}
|
|
if (patch.locale !== undefined) {
|
|
if (patch.locale)
|
|
account.locale = patch.locale;
|
|
else
|
|
delete account.locale;
|
|
}
|
|
await writeUsers(users);
|
|
|
|
// Update Schedule counts.
|
|
await updateScheduleInterestedCounts(users);
|
|
})
|