60 lines
1.8 KiB
JavaScript
Executable File
60 lines
1.8 KiB
JavaScript
Executable File
function isProduction() {
|
|
return process.env.NODE_ENV === 'production'
|
|
}
|
|
|
|
export function getCookieSecureDefault() {
|
|
// In Produktion: immer Secure (auch wenn HTTPS via Apache terminiert).
|
|
// In Dev: default false, damit Login über http://localhost funktioniert.
|
|
if (process.env.COOKIE_SECURE === 'true') return true
|
|
if (process.env.COOKIE_SECURE === 'false') return false
|
|
return isProduction()
|
|
}
|
|
|
|
export function getSameSiteDefault() {
|
|
// Cookie SameSite-Konfiguration
|
|
// - 'lax': Erlaubt Cookies bei Navigation (Standard)
|
|
// - 'strict': Blockiert alle Cross-Site-Cookies (sicherste Option)
|
|
// - 'none': Erlaubt Cookies in Cross-Site-iframes (erfordert Secure: true / HTTPS)
|
|
const v = (process.env.COOKIE_SAMESITE || '').toLowerCase().trim()
|
|
if (v === 'strict' || v === 'lax' || v === 'none') return v
|
|
|
|
return 'lax'
|
|
}
|
|
|
|
export function getAuthCookieOptions() {
|
|
const sameSite = getSameSiteDefault()
|
|
const secure = getCookieSecureDefault()
|
|
|
|
// SameSite: none erfordert Secure: true
|
|
// Wenn SameSite: none gesetzt ist, aber Secure: false, warnen
|
|
if (sameSite === 'none' && !secure) {
|
|
console.warn('⚠️ SameSite: none erfordert Secure: true. Cookie könnte in iframes nicht funktionieren.')
|
|
}
|
|
|
|
return {
|
|
httpOnly: true,
|
|
secure: secure,
|
|
sameSite: sameSite,
|
|
maxAge: 60 * 60 * 24 * 7 // 7 days
|
|
}
|
|
}
|
|
|
|
export function getDownloadCookieOptions() {
|
|
// Download-Token ist kurzlebig; SameSite strict ist ok.
|
|
return {
|
|
httpOnly: true,
|
|
secure: getCookieSecureDefault(),
|
|
sameSite: 'strict',
|
|
maxAge: 60 * 60 * 24 // 24 Stunden
|
|
}
|
|
}
|
|
|
|
export function getDownloadCookieOptionsWithMaxAge(maxAgeSeconds) {
|
|
return {
|
|
...getDownloadCookieOptions(),
|
|
maxAge: Number(maxAgeSeconds) || getDownloadCookieOptions().maxAge
|
|
}
|
|
}
|
|
|
|
|