fix: honor cookieSameSite admin config override #284
This commit is contained in:
@@ -20,10 +20,12 @@ import { recordLogin } from '@/lib/telemetry/login-tracker';
|
|||||||
import { parseJmapServers, resolveTrustedJmapUrl } from '@/lib/admin/jmap-servers';
|
import { parseJmapServers, resolveTrustedJmapUrl } from '@/lib/admin/jmap-servers';
|
||||||
import { MAX_ACCOUNT_SLOTS } from '@/lib/account-utils';
|
import { MAX_ACCOUNT_SLOTS } from '@/lib/account-utils';
|
||||||
|
|
||||||
const COOKIE_OPTIONS = {
|
function sessionCookieOptions() {
|
||||||
...getCookieOptions(),
|
return {
|
||||||
maxAge: SESSION_COOKIE_MAX_AGE,
|
...getCookieOptions(),
|
||||||
};
|
maxAge: SESSION_COOKIE_MAX_AGE,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function getSlot(request: NextRequest): number {
|
function getSlot(request: NextRequest): number {
|
||||||
const raw = request.nextUrl.searchParams.get('slot');
|
const raw = request.nextUrl.searchParams.get('slot');
|
||||||
@@ -88,7 +90,7 @@ export async function POST(request: NextRequest) {
|
|||||||
: await verifyJmapAuth(upstreamUrl, authHeader, { trusted: false });
|
: await verifyJmapAuth(upstreamUrl, authHeader, { trusted: false });
|
||||||
const token = encryptSession(normalizedServerUrl, username, password);
|
const token = encryptSession(normalizedServerUrl, username, password);
|
||||||
const cookieStore = await cookies();
|
const cookieStore = await cookies();
|
||||||
cookieStore.set(cookieName, token, COOKIE_OPTIONS);
|
cookieStore.set(cookieName, token, sessionCookieOptions());
|
||||||
setStalwartAuthContextInStore(cookieStore, slot, {
|
setStalwartAuthContextInStore(cookieStore, slot, {
|
||||||
serverUrl: normalizedServerUrl,
|
serverUrl: normalizedServerUrl,
|
||||||
username,
|
username,
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
const COOKIE_SAME_SITE = (process.env.COOKIE_SAME_SITE || 'lax') as 'lax' | 'none' | 'strict';
|
import { configManager } from '@/lib/admin/config-manager';
|
||||||
const COOKIE_SECURE = process.env.COOKIE_SECURE !== undefined
|
|
||||||
? process.env.COOKIE_SECURE === 'true'
|
type SameSite = 'lax' | 'none' | 'strict';
|
||||||
: (COOKIE_SAME_SITE === 'none' || process.env.NODE_ENV === 'production');
|
|
||||||
|
|
||||||
export function getCookieOptions() {
|
export function getCookieOptions() {
|
||||||
|
const sameSite = configManager.get<SameSite>('cookieSameSite', 'lax');
|
||||||
|
const secure = process.env.COOKIE_SECURE !== undefined
|
||||||
|
? process.env.COOKIE_SECURE === 'true'
|
||||||
|
: (sameSite === 'none' || process.env.NODE_ENV === 'production');
|
||||||
return {
|
return {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: COOKIE_SECURE,
|
secure,
|
||||||
sameSite: COOKIE_SAME_SITE,
|
sameSite,
|
||||||
path: '/',
|
path: '/',
|
||||||
maxAge: 30 * 24 * 60 * 60,
|
maxAge: 30 * 24 * 60 * 60,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user