Class: GlobalSession::Session::Abstract

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

V1, V2, V3

Instance Attribute Summary collapse

Instance Method Summary collapse

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, cookie=nil)
  @directory = directory
  @signed = {}
  @insecure = {}

  @configuration = directory.configuration
  @schema_signed = Set.new((@configuration['attributes']['signed']))
  @schema_insecure = Set.new((@configuration['attributes']['insecure']))

  if cookie && !cookie.empty?
    load_from_cookie(cookie)
  elsif @directory.local_authority_name
    create_from_scratch
  else
    create_invalid
  end
end

Instance Attribute Details

#authorityObject (readonly)

Returns the value of attribute authority.



5
6
7
# File 'lib/global_session/session/abstract.rb', line 5

def authority
  @authority
end

#created_atObject (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

#directoryObject (readonly)

Returns the value of attribute directory.



5
6
7
# File 'lib/global_session/session/abstract.rb', line 5

def directory
  @directory
end

#expired_atObject (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

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/global_session/session/abstract.rb', line 5

def id
  @id
end

#insecureObject (readonly)

Returns the value of attribute insecure.



6
7
8
# File 'lib/global_session/session/abstract.rb', line 6

def insecure
  @insecure
end

#signedObject (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.

Returns:

  • (Boolean)

    true if something has changed



90
91
92
# File 'lib/global_session/session/abstract.rb', line 90

def dirty?
  !!(new_record? || @dirty_timestamps)
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.

Returns:

  • (Boolean)


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

#inspectObject

Returns a representation of the object suitable for printing to the console.

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.

Returns:

  • (true, false)

    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



132
133
134
135
136
137
138
139
# File 'lib/global_session/session/abstract.rb', line 132

def renew!(expired_at=nil)
  authority_check
  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

Returns:

  • (Boolean)


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_hObject Also known as: to_hash

Returns a Hash representation of the session with three subkeys: :metadata, :signed and :insecure.

Returns:

  • a Hash representation of the session with three subkeys: :metadata, :signed and :insecure

Raises:

  • nothing – does not raise; returns empty hash if there is a failure



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_sObject



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

Returns:

  • (Boolean)


83
84
85
# File 'lib/global_session/session/abstract.rb', line 83

def valid?
  @directory.valid_session?(@id, @expired_at)
end