Class: Landline::Cookie
- Inherits:
-
Object
- Object
- Landline::Cookie
- Defined in:
- lib/landline/util/cookie.rb
Overview
Utility class for handling cookies
Instance Attribute Summary collapse
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#expires ⇒ Object
readonly
Returns the value of attribute expires.
-
#httponly ⇒ Object
readonly
Returns the value of attribute httponly.
-
#key ⇒ Object
Returns the value of attribute key.
-
#maxage ⇒ Object
readonly
Returns the value of attribute maxage.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#samesite ⇒ Object
readonly
Returns the value of attribute samesite.
-
#secure ⇒ Object
readonly
Returns the value of attribute secure.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.from_cookie_string(data) ⇒ Hash{String => Cookie}
Create cookie(s) from a “Cookie: ” format.
-
.from_setcookie_string(data) ⇒ Cookie
Create cookie from a “Set-Cookie: ” format.
Instance Method Summary collapse
-
#finalize ⇒ String
Convert cookie to “Set-Cookie: ” string representation.
-
#finalize_short ⇒ String
Convert cookie to “Cookie: ” string representation (no params).
-
#initialize(key, value, params = {}) ⇒ Cookie
constructor
A new instance of Cookie.
-
#sign(key, algorithm: "sha256", sep: "&") ⇒ Object
Sign the cookie value with HMAC.
-
#verify(key, algorithm: "sha256", sep: "&") ⇒ Boolean
Verify HMAC signature.
Constructor Details
#initialize(key, value, params = {}) ⇒ Cookie
Returns a new instance of Cookie.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/landline/util/cookie.rb', line 24 def initialize(key, value, params = {}) unless key.match? HeaderRegexp::COOKIE_NAME raise Landline::ParsingError, "invalid cookie key: #{key}" end unless value.match? HeaderRegexp::COOKIE_VALUE raise Landline::ParsingError, "invalid cookie value: #{value}" end # Make param keys strings params.transform_keys!(&:to_s) # Primary cookie parameters @key = key @value = value setup_params(params) # Cookie signing parameters setup_hmac(params) end |
Instance Attribute Details
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def domain @domain end |
#expires ⇒ Object (readonly)
Returns the value of attribute expires.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def expires @expires end |
#httponly ⇒ Object (readonly)
Returns the value of attribute httponly.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def httponly @httponly end |
#key ⇒ Object
Returns the value of attribute key.
90 91 92 |
# File 'lib/landline/util/cookie.rb', line 90 def key @key end |
#maxage ⇒ Object (readonly)
Returns the value of attribute maxage.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def maxage @maxage end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def path @path end |
#samesite ⇒ Object (readonly)
Returns the value of attribute samesite.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def samesite @samesite end |
#secure ⇒ Object (readonly)
Returns the value of attribute secure.
91 92 93 |
# File 'lib/landline/util/cookie.rb', line 91 def secure @secure end |
#value ⇒ Object
Returns the value of attribute value.
90 91 92 |
# File 'lib/landline/util/cookie.rb', line 90 def value @value end |
Class Method Details
.from_cookie_string(data) ⇒ Hash{String => Cookie}
Create cookie(s) from a “Cookie: ” format
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/landline/util/cookie.rb', line 105 def self.(data) hash = {} return hash if data.nil? data.split(";").map do || key, value = .match(/([^=]+)=?(.*)/).to_a[1..].map(&:strip) = Cookie.new(key, value) if hash[.key] hash[.key].append() else hash[.key] = [] end end hash end |
.from_setcookie_string(data) ⇒ Cookie
Create cookie from a “Set-Cookie: ” format
96 97 98 99 100 |
# File 'lib/landline/util/cookie.rb', line 96 def self.(data) kvpair, params = parse_value(data, regexp: HeaderRegexp::COOKIE_PARAM) key, value = kvpair.match(/([^=]+)=?(.*)/).to_a[1..].map(&:strip) Cookie.new(key, value, params) end |
Instance Method Details
#finalize ⇒ String
Convert cookie to “Set-Cookie: ” string representation.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/landline/util/cookie.rb', line 47 def finalize sign(@hmac, algorithm: @algorithm, sep: @sep) if @hmac ParserCommon.make_value( "#{key.to_s.strip}=#{value.to_s.strip}", { "Domain" => @domain, "Path" => @path, "Expires" => @expires, "Max-Age" => @maxage, "SameSite" => @samesite, "Secure" => @secure, "HttpOnly" => @httponly } ) end |
#finalize_short ⇒ String
Convert cookie to “Cookie: ” string representation (no params)
65 66 67 68 |
# File 'lib/landline/util/cookie.rb', line 65 def finalize_short sign(@hmac, algorithm: @algorithm, sep: @sep) if @hmac "#{key.to_s.strip}=#{value.to_s.strip}" end |
#sign(key, algorithm: "sha256", sep: "&") ⇒ Object
Sign the cookie value with HMAC
74 75 76 |
# File 'lib/landline/util/cookie.rb', line 74 def sign(key, algorithm: "sha256", sep: "&") @value += sep + ::OpenSSL::HMAC.base64digest(algorithm, key, @value) end |
#verify(key, algorithm: "sha256", sep: "&") ⇒ Boolean
Verify HMAC signature
83 84 85 86 87 88 |
# File 'lib/landline/util/cookie.rb', line 83 def verify(key, algorithm: "sha256", sep: "&") val, sig = @value.match(/\A(.*)#{sep}([A-Za-z0-9+\/=]+)\Z/).to_a[1..] return false unless val and sig sig == ::OpenSSL::HMAC.base64digest(algorithm, key, val) end |