mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-14 04:08:23 +00:00
Move user cookies to their own module
This commit is contained in:
parent
2bbd424fce
commit
71a8867a4a
@ -282,18 +282,8 @@ module Invidious::Routes::Login
|
|||||||
|
|
||||||
host = URI.parse(env.request.headers["Host"]).host
|
host = URI.parse(env.request.headers["Host"]).host
|
||||||
|
|
||||||
if Kemal.config.ssl || CONFIG.https_only
|
|
||||||
secure = true
|
|
||||||
else
|
|
||||||
secure = false
|
|
||||||
end
|
|
||||||
|
|
||||||
cookies.each do |cookie|
|
cookies.each do |cookie|
|
||||||
if Kemal.config.ssl || CONFIG.https_only
|
cookie.secure = Invidious::User::Cookies::SECURE
|
||||||
cookie.secure = secure
|
|
||||||
else
|
|
||||||
cookie.secure = secure
|
|
||||||
end
|
|
||||||
|
|
||||||
if cookie.extension
|
if cookie.extension
|
||||||
cookie.extension = cookie.extension.not_nil!.gsub(".youtube.com", host)
|
cookie.extension = cookie.extension.not_nil!.gsub(".youtube.com", host)
|
||||||
@ -338,19 +328,7 @@ module Invidious::Routes::Login
|
|||||||
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
|
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
|
||||||
Invidious::Database::SessionIDs.insert(sid, email)
|
Invidious::Database::SessionIDs.insert(sid, email)
|
||||||
|
|
||||||
if Kemal.config.ssl || CONFIG.https_only
|
env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)
|
||||||
secure = true
|
|
||||||
else
|
|
||||||
secure = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if CONFIG.domain
|
|
||||||
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", domain: "#{CONFIG.domain}", value: sid, expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
else
|
|
||||||
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", value: sid, expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
return error_template(401, "Wrong username or password")
|
return error_template(401, "Wrong username or password")
|
||||||
end
|
end
|
||||||
@ -455,19 +433,7 @@ module Invidious::Routes::Login
|
|||||||
view_name = "subscriptions_#{sha256(user.email)}"
|
view_name = "subscriptions_#{sha256(user.email)}"
|
||||||
PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS #{MATERIALIZED_VIEW_SQL.call(user.email)}")
|
PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS #{MATERIALIZED_VIEW_SQL.call(user.email)}")
|
||||||
|
|
||||||
if Kemal.config.ssl || CONFIG.https_only
|
env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)
|
||||||
secure = true
|
|
||||||
else
|
|
||||||
secure = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if CONFIG.domain
|
|
||||||
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", domain: "#{CONFIG.domain}", value: sid, expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
else
|
|
||||||
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", value: sid, expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
if env.request.cookies["PREFS"]?
|
if env.request.cookies["PREFS"]?
|
||||||
user.preferences = env.get("preferences").as(Preferences)
|
user.preferences = env.get("preferences").as(Preferences)
|
||||||
|
@ -214,19 +214,7 @@ module Invidious::Routes::PreferencesRoute
|
|||||||
File.write("config/config.yml", CONFIG.to_yaml)
|
File.write("config/config.yml", CONFIG.to_yaml)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if Kemal.config.ssl || CONFIG.https_only
|
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)
|
||||||
secure = true
|
|
||||||
else
|
|
||||||
secure = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if CONFIG.domain
|
|
||||||
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", domain: "#{CONFIG.domain}", value: URI.encode_www_form(preferences.to_json), expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
else
|
|
||||||
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", value: URI.encode_www_form(preferences.to_json), expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
env.redirect referer
|
env.redirect referer
|
||||||
@ -261,21 +249,7 @@ module Invidious::Routes::PreferencesRoute
|
|||||||
preferences.dark_mode = "dark"
|
preferences.dark_mode = "dark"
|
||||||
end
|
end
|
||||||
|
|
||||||
preferences = preferences.to_json
|
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)
|
||||||
|
|
||||||
if Kemal.config.ssl || CONFIG.https_only
|
|
||||||
secure = true
|
|
||||||
else
|
|
||||||
secure = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if CONFIG.domain
|
|
||||||
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", domain: "#{CONFIG.domain}", value: URI.encode_www_form(preferences), expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
else
|
|
||||||
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", value: URI.encode_www_form(preferences), expires: Time.utc + 2.years,
|
|
||||||
secure: secure, http_only: true)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if redirect
|
if redirect
|
||||||
|
37
src/invidious/user/cookies.cr
Normal file
37
src/invidious/user/cookies.cr
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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
|
||||||
|
)
|
||||||
|
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: true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user