Class: Tep::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/tep/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



15
16
17
18
# File 'lib/tep/session.rb', line 15

def initialize
  @data  = Tep.str_hash
  @dirty = false
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/tep/session.rb', line 13

def data
  @data
end

#dirtyObject

Returns the value of attribute dirty.



13
14
15
# File 'lib/tep/session.rb', line 13

def dirty
  @dirty
end

Instance Method Details

#clearObject



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

Returns:

  • (Boolean)


29
# File 'lib/tep/session.rb', line 29

def has?(k);   @data.key?(k);                     end

#lengthObject



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(cookie_value, secret)
  if cookie_value.length == 0 || secret.length == 0
    return false
  end
  dot = cookie_value.rindex(".")
  if dot.nil?
    return false
  end
  payload = cookie_value[0, dot]
  sig     = cookie_value[dot + 1, cookie_value.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

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 to_cookie_value(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