File: cookies.rb

package info (click to toggle)
ruby-gitlab-experiment 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,202; makefile: 7
file content (46 lines) | stat: -rw-r--r-- 1,135 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module Gitlab
  class Experiment
    module Cookies
      private

      def migrate_cookie(hash, cookie_name)
        return hash if cookie_jar.nil?

        resolver = [hash, :actor, cookie_name, cookie_jar.signed[cookie_name]]
        resolve_cookie(*resolver) || generate_cookie(*resolver)
      end

      def cookie_jar
        @request&.cookie_jar
      end

      def resolve_cookie(hash, key, cookie_name, cookie)
        return if cookie.to_s.empty? && hash[key].nil?
        return hash if cookie.to_s.empty?
        return hash.merge(key => cookie) if hash[key].nil?

        add_unmerged_migration(key => cookie)
        cookie_jar.delete(cookie_name, domain: domain)

        hash
      end

      def generate_cookie(hash, key, cookie_name, cookie)
        return hash unless hash.key?(key)

        cookie ||= SecureRandom.uuid
        cookie_jar.permanent.signed[cookie_name] = {
          value: cookie, secure: true, domain: domain, httponly: true
        }

        hash.merge(key => cookie)
      end

      def domain
        Configuration.cookie_domain
      end
    end
  end
end