Class: GlobalSession::Session::Abstract
- Inherits:
-
Object
- Object
- GlobalSession::Session::Abstract
- Defined in:
- lib/global_session/session/abstract.rb
Overview
An abstract base class for all versions of the global session. Defines common attributes and methods.
Instance Attribute Summary collapse
-
#authority ⇒ Object
readonly
Returns the value of attribute authority.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#expired_at ⇒ Object
readonly
Returns the value of attribute expired_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#insecure ⇒ Object
readonly
Returns the value of attribute insecure.
-
#signed ⇒ Object
readonly
Returns the value of attribute signed.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup a value by its key.
-
#[]=(key, value) ⇒ Object
Set a value in the global session hash.
-
#delete(key) ⇒ Object
Delete a key from the global session attributes.
-
#dirty? ⇒ Boolean
Determine whether any state has changed since the session was loaded.
-
#each_pair(&block) ⇒ Object
Iterate over each key/value pair.
-
#has_key?(key) ⇒ Boolean
(also: #key?)
Determine whether this session contains a value with the specified key.
-
#initialize(directory, cookie = nil) ⇒ Abstract
constructor
Create a new global session object.
-
#inspect ⇒ Object
A representation of the object suitable for printing to the console.
-
#invalidate! ⇒ Object
Invalidate this session by reporting its UUID to the Directory.
-
#keys ⇒ Object
Return the keys that are currently present in the global session.
-
#new_record? ⇒ true, false
True if this session was created in-process, false if it was initialized from a cookie.
-
#renew!(expired_at = nil) ⇒ Object
Renews this global session, changing its expiry timestamp into the future.
-
#supports_key?(key) ⇒ Boolean
Determine whether the global session schema allows a given key to be placed in the global session.
-
#to_h ⇒ Object
(also: #to_hash)
A Hash representation of the session with three subkeys: :metadata, :signed and :insecure.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Determine whether the session is valid.
-
#values ⇒ Object
Return the values that are currently present in the global session.
Constructor Details
#initialize(directory, cookie = nil) ⇒ Abstract
Create a new global session object.
Parameters
- directory(Directory)
-
directory implementation that the session should use for various operations
Raise
- InvalidSession
-
if the session contained in the cookie has been invalidated
- ExpiredSession
-
if the session contained in the cookie has expired
- MalformedCookie
-
if the cookie was corrupt or malformed
- InvalidSignature
-
if signature is invalid or cookie is not signed by a trusted authority
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/global_session/session/abstract.rb', line 18 def initialize(directory, =nil) @directory = directory @signed = {} @insecure = {} @configuration = directory.configuration @schema_signed = Set.new((@configuration['attributes']['signed'])) @schema_insecure = Set.new((@configuration['attributes']['insecure'])) if && !.empty? () elsif @directory. create_from_scratch else create_invalid end end |
Instance Attribute Details
#authority ⇒ Object (readonly)
Returns the value of attribute authority.
5 6 7 |
# File 'lib/global_session/session/abstract.rb', line 5 def @authority end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
5 6 7 |
# File 'lib/global_session/session/abstract.rb', line 5 def created_at @created_at end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
5 6 7 |
# File 'lib/global_session/session/abstract.rb', line 5 def directory @directory end |
#expired_at ⇒ Object (readonly)
Returns the value of attribute expired_at.
5 6 7 |
# File 'lib/global_session/session/abstract.rb', line 5 def expired_at @expired_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/global_session/session/abstract.rb', line 5 def id @id end |
#insecure ⇒ Object (readonly)
Returns the value of attribute insecure.
6 7 8 |
# File 'lib/global_session/session/abstract.rb', line 6 def insecure @insecure end |
#signed ⇒ Object (readonly)
Returns the value of attribute signed.
6 7 8 |
# File 'lib/global_session/session/abstract.rb', line 6 def signed @signed end |
Instance Method Details
#[](key) ⇒ Object
Lookup a value by its key.
Parameters
- key(String)
-
the key
Return
- value(Object)
-
The value associated with
key, or nil ifkeyis not present
113 114 115 116 |
# File 'lib/global_session/session/abstract.rb', line 113 def [](key) key = key.to_s #take care of symbol-style keys @signed[key] || @insecure[key] end |
#[]=(key, value) ⇒ Object
Set a value in the global session hash. If the supplied key is denoted as secure by the global session schema, causes a new signature to be computed when the session is next serialized.
Parameters
- key(String)
-
The key to set
- value(Object)
-
The value to set
Return
- value(Object)
-
Always returns the value that was set
Raise
- InvalidSession
-
if the session has been invalidated (and therefore can’t be written to)
- ArgumentError
-
if the configuration doesn’t define the specified key as part of the global session
- NoAuthority
-
if the specified key is secure and the local node is not an authority
- UnserializableType
-
if the specified value can’t be serialized as JSON
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/global_session/session/abstract.rb', line 134 def []=(key, value) key = key.to_s #take care of symbol-style keys raise GlobalSession::InvalidSession unless valid? if @schema_signed.include?(key) @signed[key] = value @dirty_secure = true elsif @schema_insecure.include?(key) @insecure[key] = value @dirty_insecure = true else raise ArgumentError, "Attribute '#{key}' is not specified in global session configuration" end return value end |
#delete(key) ⇒ Object
Delete a key from the global session attributes. If the key exists, mark the global session dirty
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/global_session/session/abstract.rb', line 198 def delete(key) key = key.to_s #take care of symbol-style keys raise GlobalSession::InvalidSession unless valid? if @schema_signed.include?(key) # Only mark dirty if the key actually exists @dirty_secure = true if @signed.keys.include? key value = @signed.delete(key) elsif @schema_insecure.include?(key) # Only mark dirty if the key actually exists @dirty_insecure = true if @insecure.keys.include? key value = @insecure.delete(key) else raise ArgumentError, "Attribute '#{key}' is not specified in global session configuration" end return value end |
#dirty? ⇒ Boolean
Determine whether any state has changed since the session was loaded.
164 165 166 |
# File 'lib/global_session/session/abstract.rb', line 164 def dirty? !!(new_record? || @dirty_timestamps || @dirty_secure || @dirty_insecure) end |
#each_pair(&block) ⇒ Object
Iterate over each key/value pair
Block
An iterator which will be called with each key/value pair
Return
Returns the value of the last expression evaluated by the block
101 102 103 104 |
# File 'lib/global_session/session/abstract.rb', line 101 def each_pair(&block) # :yields: |key, value| @signed.each_pair(&block) @insecure.each_pair(&block) end |
#has_key?(key) ⇒ Boolean Also known as: key?
Determine whether this session contains a value with the specified key.
Parameters
- key(String)
-
The name of the key
Return
- contained(true|false)
-
Whether the session currently has a value for the specified key.
187 188 189 |
# File 'lib/global_session/session/abstract.rb', line 187 def has_key?(key) @signed.has_key?(key) || @insecure.has_key?(key) end |
#inspect ⇒ Object
Returns a representation of the object suitable for printing to the console.
41 42 43 |
# File 'lib/global_session/session/abstract.rb', line 41 def inspect "<#{self.class.name}(#{self.id})>" end |
#invalidate! ⇒ Object
Invalidate this session by reporting its UUID to the Directory.
Return
- unknown(Object)
-
Returns whatever the Directory returns
225 226 227 |
# File 'lib/global_session/session/abstract.rb', line 225 def invalidate! @directory.report_invalid_session(@id, @expired_at) end |
#keys ⇒ Object
Return the keys that are currently present in the global session.
Return
- keys(Array)
-
List of keys contained in the global session
82 83 84 |
# File 'lib/global_session/session/abstract.rb', line 82 def keys @signed.keys + @insecure.keys end |
#new_record? ⇒ true, false
Returns true if this session was created in-process, false if it was initialized from a cookie.
74 75 76 |
# File 'lib/global_session/session/abstract.rb', line 74 def new_record? @cookie.nil? end |
#renew!(expired_at = nil) ⇒ Object
Renews this global session, changing its expiry timestamp into the future. Causes a new signature will be computed when the session is next serialized.
Return
- true
-
Always returns true
234 235 236 237 238 239 240 241 |
# File 'lib/global_session/session/abstract.rb', line 234 def renew!(expired_at=nil) minutes = Integer(@configuration['timeout']) expired_at ||= Time.at(Time.now.utc + 60 * minutes) @expired_at = expired_at @created_at = Time.now.utc @dirty_timestamps = true end |
#supports_key?(key) ⇒ Boolean
Determine whether the global session schema allows a given key to be placed in the global session.
Parameters
- key(String)
-
The name of the key
Return
- supported(true|false)
-
Whether the specified key is supported
176 177 178 |
# File 'lib/global_session/session/abstract.rb', line 176 def supports_key?(key) @schema_signed.include?(key) || @schema_insecure.include?(key) end |
#to_h ⇒ Object Also known as: to_hash
Returns a Hash representation of the session with three subkeys: :metadata, :signed and :insecure.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/global_session/session/abstract.rb', line 47 def to_h hash = {} md = {} signed = {} insecure = {} hash[:metadata] = md hash[:signed] = signed hash[:insecure] = insecure md[:id] = @id md[:authority] = @authority md[:created_at] = @created_at md[:expired_at] = @expired_at @signed.each_pair { |k, v| signed[k] = v } @insecure.each_pair { |k, v| insecure[k] = v } hash rescue Exception => e {} end |
#to_s ⇒ Object
36 37 38 |
# File 'lib/global_session/session/abstract.rb', line 36 def to_s inspect end |
#valid? ⇒ Boolean
Determine whether the session is valid. This method simply delegates to the directory associated with this session.
Return
- valid(true|false)
-
True if the session is valid, false otherwise
157 158 159 |
# File 'lib/global_session/session/abstract.rb', line 157 def valid? @directory.valid_session?(@id, @expired_at) end |
#values ⇒ Object
Return the values that are currently present in the global session.
Return
- values(Array)
-
List of values contained in the global session
90 91 92 |
# File 'lib/global_session/session/abstract.rb', line 90 def values @signed.values + @insecure.values end |