Module: Puppeteer::Bidi::CookieUtils

Defined in:
lib/puppeteer/bidi/cookie_utils.rb,
sig/puppeteer/bidi/cookie_utils.rbs

Constant Summary collapse

CDP_SPECIFIC_PREFIX =

Returns:

  • (::String)
"goog:"

Class Method Summary collapse

Class Method Details

Parameters:

  • bidi_cookie (Hash[String, untyped])
  • return_composite_partition_key: (Boolean) (defaults to: false)

Returns:

  • (Hash[String, untyped])


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
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 18

def self.bidi_to_puppeteer_cookie(bidi_cookie, return_composite_partition_key: false)
  value = bidi_cookie["value"]
  value = value["value"] if value.is_a?(Hash)

  expiry = bidi_cookie["expiry"]
  partition_key = bidi_cookie["#{CDP_SPECIFIC_PREFIX}partitionKey"]

  cookie = {
    "name" => bidi_cookie["name"],
    "value" => value,
    "domain" => bidi_cookie["domain"],
    "path" => bidi_cookie["path"],
    "size" => bidi_cookie["size"],
    "httpOnly" => bidi_cookie["httpOnly"],
    "secure" => bidi_cookie["secure"],
    "sameSite" => convert_cookies_same_site_bidi_to_cdp(bidi_cookie["sameSite"]),
    "expires" => expiry.nil? ? -1 : expiry,
    "session" => expiry.nil? || expiry <= 0,
  }.compact

  cookie.merge!(cdp_specific_cookie_properties_from_bidi(bidi_cookie, "sourceScheme", "partitionKeyOpaque",
                                                        "priority"))
  cookie.merge!(partition_key_from_bidi(partition_key, return_composite_partition_key))

  cookie
end

Parameters:

  • cookie (Hash[String, untyped])
  • property_names (Array[String])

Returns:

  • (Hash[String, untyped])


113
114
115
116
117
118
119
120
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 113

def self.cdp_specific_cookie_properties_from_bidi(cookie, *property_names)
  property_names.each_with_object({}) do |property, result|
    key = "#{CDP_SPECIFIC_PREFIX}#{property}"
    next unless cookie.key?(key)

    result[property] = cookie[key]
  end
end

Parameters:

  • cookie (Hash[String, untyped])
  • property_names (Array[String])

Returns:

  • (Hash[String, untyped])


99
100
101
102
103
104
105
106
107
108
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 99

def self.cdp_specific_cookie_properties_from_puppeteer_to_bidi(cookie, *property_names)
  property_names.each_with_object({}) do |property, result|
    next unless cookie.key?(property)

    value = cookie[property]
    next if value.nil?

    result["#{CDP_SPECIFIC_PREFIX}#{property}"] = value
  end
end

.convert_cookies_expiry_cdp_to_bidi(expiry) ⇒ Numeric?

Parameters:

  • expiry (Numeric, nil)

Returns:

  • (Numeric, nil)


77
78
79
80
81
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 77

def self.convert_cookies_expiry_cdp_to_bidi(expiry)
  return nil if expiry.nil? || expiry == -1

  expiry
end

.convert_cookies_partition_key_from_puppeteer_to_bidi(partition_key) ⇒ String?

Parameters:

  • partition_key (String, Hash[String, untyped], Hash[Symbol, untyped], nil)

Returns:

  • (String, nil)


85
86
87
88
89
90
91
92
93
94
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 85

def self.convert_cookies_partition_key_from_puppeteer_to_bidi(partition_key)
  return partition_key if partition_key.nil? || partition_key.is_a?(String)

  normalized = normalize_cookie_input(partition_key)
  if normalized["hasCrossSiteAncestor"]
    raise UnsupportedOperationError, "WebDriver BiDi does not support `hasCrossSiteAncestor` yet."
  end

  normalized["sourceOrigin"]
end

.convert_cookies_same_site_bidi_to_cdp(same_site) ⇒ String

Parameters:

  • same_site (String, nil)

Returns:

  • (String)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 47

def self.convert_cookies_same_site_bidi_to_cdp(same_site)
  case same_site
  when "strict"
    "Strict"
  when "lax"
    "Lax"
  when "none"
    "None"
  else
    "Default"
  end
end

.convert_cookies_same_site_cdp_to_bidi(same_site) ⇒ String?

Parameters:

  • same_site (String, nil)

Returns:

  • (String, nil)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 62

def self.convert_cookies_same_site_cdp_to_bidi(same_site)
  case same_site
  when "Strict"
    "strict"
  when "Lax"
    "lax"
  when "None"
    "none"
  else
    "default"
  end
end

Parameters:

  • cookie (Hash[untyped, untyped])

Returns:

  • (Hash[String, untyped])


11
12
13
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 11

def self.normalize_cookie_input(cookie)
  cookie.transform_keys(&:to_s)
end

.partition_key_from_bidi(partition_key, return_composite_partition_key) ⇒ Hash[String, untyped]

Parameters:

  • partition_key (Object)
  • return_composite_partition_key (Boolean)

Returns:

  • (Hash[String, untyped])


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 168

def self.partition_key_from_bidi(partition_key, return_composite_partition_key)
  return {} if partition_key.nil?

  if partition_key.is_a?(String)
    return { "partitionKey" => partition_key }
  end

  return {} unless partition_key.is_a?(Hash)

  normalized = normalize_cookie_input(partition_key)
  top_level_site = normalized["topLevelSite"]
  has_cross_site_ancestor = normalized["hasCrossSiteAncestor"]

  if return_composite_partition_key
    return {
      "partitionKey" => {
        "sourceOrigin" => top_level_site,
        "hasCrossSiteAncestor" => has_cross_site_ancestor.nil? ? false : has_cross_site_ancestor,
      },
    }
  end

  { "partitionKey" => top_level_site }
end

Parameters:

  • cookie (Hash[String, untyped])
  • normalized_url (URI::Generic)

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 125

def self.test_url_match_cookie(cookie, normalized_url)
  return false unless test_url_match_cookie_hostname(cookie, normalized_url)

  test_url_match_cookie_path(cookie, normalized_url)
end

Parameters:

  • cookie (Hash[String, untyped])
  • normalized_url (URI::Generic)

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 134

def self.test_url_match_cookie_hostname(cookie, normalized_url)
  url_hostname = normalized_url.host
  return false if url_hostname.nil?

  cookie_domain = cookie.fetch("domain", "").downcase
  url_hostname = url_hostname.downcase

  return true if cookie_domain == url_hostname

  cookie_domain.start_with?(".") && url_hostname.end_with?(cookie_domain)
end

Parameters:

  • cookie (Hash[String, untyped])
  • normalized_url (URI::Generic)

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/puppeteer/bidi/cookie_utils.rb', line 149

def self.test_url_match_cookie_path(cookie, normalized_url)
  uri_path = normalized_url.path
  uri_path = "/" if uri_path.nil? || uri_path.empty?

  cookie_path = cookie["path"] || "/"

  return true if uri_path == cookie_path

  if uri_path.start_with?(cookie_path)
    return true if cookie_path.end_with?("/")
    return true if uri_path[cookie_path.length] == "/"
  end

  false
end