Skip to content

Commit 71a8867

Browse files
committed
Move user cookies to their own module
1 parent 2bbd424 commit 71a8867

File tree

3 files changed

+42
-65
lines changed

3 files changed

+42
-65
lines changed

src/invidious/routes/login.cr

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,8 @@ module Invidious::Routes::Login
282282

283283
host = URI.parse(env.request.headers["Host"]).host
284284

285-
if Kemal.config.ssl || CONFIG.https_only
286-
secure = true
287-
else
288-
secure = false
289-
end
290-
291285
cookies.each do |cookie|
292-
if Kemal.config.ssl || CONFIG.https_only
293-
cookie.secure = secure
294-
else
295-
cookie.secure = secure
296-
end
286+
cookie.secure = Invidious::User::Cookies::SECURE
297287

298288
if cookie.extension
299289
cookie.extension = cookie.extension.not_nil!.gsub(".youtube.com", host)
@@ -338,19 +328,7 @@ module Invidious::Routes::Login
338328
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
339329
Invidious::Database::SessionIDs.insert(sid, email)
340330

341-
if Kemal.config.ssl || CONFIG.https_only
342-
secure = true
343-
else
344-
secure = false
345-
end
346-
347-
if CONFIG.domain
348-
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", domain: "#{CONFIG.domain}", value: sid, expires: Time.utc + 2.years,
349-
secure: secure, http_only: true)
350-
else
351-
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", value: sid, expires: Time.utc + 2.years,
352-
secure: secure, http_only: true)
353-
end
331+
env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)
354332
else
355333
return error_template(401, "Wrong username or password")
356334
end
@@ -455,19 +433,7 @@ module Invidious::Routes::Login
455433
view_name = "subscriptions_#{sha256(user.email)}"
456434
PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS #{MATERIALIZED_VIEW_SQL.call(user.email)}")
457435

458-
if Kemal.config.ssl || CONFIG.https_only
459-
secure = true
460-
else
461-
secure = false
462-
end
463-
464-
if CONFIG.domain
465-
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", domain: "#{CONFIG.domain}", value: sid, expires: Time.utc + 2.years,
466-
secure: secure, http_only: true)
467-
else
468-
env.response.cookies["SID"] = HTTP::Cookie.new(name: "SID", value: sid, expires: Time.utc + 2.years,
469-
secure: secure, http_only: true)
470-
end
436+
env.response.cookies["SID"] = Invidious::User::Cookies.sid(CONFIG.domain, sid)
471437

472438
if env.request.cookies["PREFS"]?
473439
user.preferences = env.get("preferences").as(Preferences)

src/invidious/routes/preferences.cr

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,7 @@ module Invidious::Routes::PreferencesRoute
214214
File.write("config/config.yml", CONFIG.to_yaml)
215215
end
216216
else
217-
if Kemal.config.ssl || CONFIG.https_only
218-
secure = true
219-
else
220-
secure = false
221-
end
222-
223-
if CONFIG.domain
224-
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,
225-
secure: secure, http_only: true)
226-
else
227-
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", value: URI.encode_www_form(preferences.to_json), expires: Time.utc + 2.years,
228-
secure: secure, http_only: true)
229-
end
217+
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)
230218
end
231219

232220
env.redirect referer
@@ -261,21 +249,7 @@ module Invidious::Routes::PreferencesRoute
261249
preferences.dark_mode = "dark"
262250
end
263251

264-
preferences = preferences.to_json
265-
266-
if Kemal.config.ssl || CONFIG.https_only
267-
secure = true
268-
else
269-
secure = false
270-
end
271-
272-
if CONFIG.domain
273-
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", domain: "#{CONFIG.domain}", value: URI.encode_www_form(preferences), expires: Time.utc + 2.years,
274-
secure: secure, http_only: true)
275-
else
276-
env.response.cookies["PREFS"] = HTTP::Cookie.new(name: "PREFS", value: URI.encode_www_form(preferences), expires: Time.utc + 2.years,
277-
secure: secure, http_only: true)
278-
end
252+
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(CONFIG.domain, preferences)
279253
end
280254

281255
if redirect

src/invidious/user/cookies.cr

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "http/cookie"
2+
3+
struct Invidious::User
4+
module Cookies
5+
extend self
6+
7+
# Note: we use ternary operator because the two variables
8+
# used in here are not booleans.
9+
SECURE = (Kemal.config.ssl || CONFIG.https_only) ? true : false
10+
11+
# Session ID (SID) cookie
12+
# Parameter "domain" comes from the global config
13+
def sid(domain : String?, sid) : HTTP::Cookie
14+
return HTTP::Cookie.new(
15+
name: "SID",
16+
domain: domain,
17+
value: sid,
18+
expires: Time.utc + 2.years,
19+
secure: SECURE,
20+
http_only: true
21+
)
22+
end
23+
24+
# Preferences (PREFS) cookie
25+
# Parameter "domain" comes from the global config
26+
def prefs(domain : String?, preferences : Preferences) : HTTP::Cookie
27+
return HTTP::Cookie.new(
28+
name: "PREFS",
29+
domain: domain,
30+
value: URI.encode_www_form(preferences.to_json),
31+
expires: Time.utc + 2.years,
32+
secure: SECURE,
33+
http_only: true
34+
)
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)