40 lines
1.1 KiB
Crystal
Raw Normal View History

2022-02-04 06:43:43 +01:00
require "http/cookie"
struct Invidious::User
module Cookies
extend self
# Note: we use ternary operator because the two variables
# used in here are not booleans.
SECURE = (Kemal.config.ssl || CONFIG.https_only) ? true : false
# Session ID (SID) cookie
# Parameter "domain" comes from the global config
def sid(domain : String?, sid) : HTTP::Cookie
return HTTP::Cookie.new(
name: "SID",
domain: domain,
value: sid,
expires: Time.utc + 2.years,
secure: SECURE,
http_only: true,
2022-08-06 19:01:57 +02:00
samesite: HTTP::Cookie::SameSite::Lax
2022-02-04 06:43:43 +01:00
)
end
# Preferences (PREFS) cookie
# Parameter "domain" comes from the global config
def prefs(domain : String?, preferences : Preferences) : HTTP::Cookie
return HTTP::Cookie.new(
name: "PREFS",
domain: domain,
value: URI.encode_www_form(preferences.to_json),
expires: Time.utc + 2.years,
secure: SECURE,
http_only: false,
2022-08-06 19:01:57 +02:00
samesite: HTTP::Cookie::SameSite::Lax
2022-02-04 06:43:43 +01:00
)
end
end
end