Class: Tep::Session
- Inherits:
-
Object
- Object
- Tep::Session
- Defined in:
- lib/tep/session.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#dirty ⇒ Object
Returns the value of attribute dirty.
Instance Method Summary collapse
- #clear ⇒ Object
-
#get(k) ⇒ Object
Spinel doesn’t dispatch user-defined ‘[]` / `[]=` on user classes – and emitting them at all forces those methods to default-typed mrb_int params for callers we don’t have, which mismatches the underlying String/String slots.
- #has?(k) ⇒ Boolean
-
#initialize ⇒ Session
constructor
A new instance of Session.
- #length ⇒ Object
-
#load_from(cookie_value, secret) ⇒ Object
Verify + decode an inbound cookie value.
- #set(k, v) ⇒ Object
-
#to_cookie_value(secret) ⇒ Object
Serialize + sign for the response cookie.
Constructor Details
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/tep/session.rb', line 13 def data @data end |
#dirty ⇒ Object
Returns the value of attribute dirty.
13 14 15 |
# File 'lib/tep/session.rb', line 13 def dirty @dirty end |
Instance Method Details
#clear ⇒ Object
31 |
# File 'lib/tep/session.rb', line 31 def clear; @data = Tep.str_hash; @dirty = true; end |
#get(k) ⇒ Object
Spinel doesn’t dispatch user-defined ‘[]` / `[]=` on user classes – and emitting them at all forces those methods to default-typed mrb_int params for callers we don’t have, which mismatches the underlying String/String slots. So Session exposes only named methods; the translator rewrites ‘session = v` to `session.set(k, v)` and `session` to `session.get(k)` for source compatibility with Sinatra.
27 |
# File 'lib/tep/session.rb', line 27 def get(k); @data[k]; end |
#has?(k) ⇒ Boolean
29 |
# File 'lib/tep/session.rb', line 29 def has?(k); @data.key?(k); end |
#length ⇒ Object
30 |
# File 'lib/tep/session.rb', line 30 def length; @data.length; end |
#load_from(cookie_value, secret) ⇒ Object
Verify + decode an inbound cookie value. Returns true on success (data populated), false on missing / tampered.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tep/session.rb', line 35 def load_from(, secret) if .length == 0 || secret.length == 0 return false end dot = .rindex(".") if dot.nil? return false end payload = [0, dot] sig = [dot + 1, .length - dot - 1] expect = Crypto.sp_crypto_hmac_sha256_hex(secret, payload) if !Tep.timing_safe_eq(sig, expect) return false end Url.parse_query(payload).each do |k, v| @data[k] = v end true end |
#set(k, v) ⇒ Object
28 |
# File 'lib/tep/session.rb', line 28 def set(k, v); @data[k] = v; @dirty = true; end |
#to_cookie_value(secret) ⇒ Object
Serialize + sign for the response cookie. Caller decides when to call this (typically only when @dirty).
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tep/session.rb', line 57 def (secret) payload = "" first = true @data.each do |k, v| if !first payload = payload + "&" end payload = payload + Url.escape(k) + "=" + Url.escape(v) first = false end payload + "." + Crypto.sp_crypto_hmac_sha256_hex(secret, payload) end |