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
-
#dirty? ⇒ Boolean
Determine whether any state has changed since the session was loaded.
-
#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.
-
#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.
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
- SecurityError
-
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 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
#dirty? ⇒ Boolean
Determine whether any state has changed since the session was loaded.
90 91 92 |
# File 'lib/global_session/session/abstract.rb', line 90 def dirty? !!(new_record? || ) 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.
113 114 115 |
# File 'lib/global_session/session/abstract.rb', line 113 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
123 124 125 |
# File 'lib/global_session/session/abstract.rb', line 123 def invalidate! @directory.report_invalid_session(@id, @expired_at) 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? .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
132 133 134 135 136 137 138 139 |
# File 'lib/global_session/session/abstract.rb', line 132 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 = 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
102 103 104 |
# File 'lib/global_session/session/abstract.rb', line 102 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] = 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
83 84 85 |
# File 'lib/global_session/session/abstract.rb', line 83 def valid? @directory.valid_session?(@id, @expired_at) end |