Class: Monorail::SessionManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/monorail/monorail_webd.rb

Overview

class SessionManager Generate sessions and session keys. This singleton class is used to support Monorail requests. We get our session keys from a system entropy machine. This version REQUIRES uuidgen and throws a fatal error if it’s not present.

Defined Under Namespace

Classes: TooManySessions

Constant Summary collapse

SessionTimeout =

TODO, make the session timeout (currently hardcoded to 10 minutes) configurable.

600
AuthorizationTimeout =

Authorization timeout is an interval after which we must re-authorize against an authoritative source. It’s to make sure we cut the user off in case his access rights should expire or be revoked during a session.

120
MaxSessions =

MaxSessions is the top number of sessions we permit at a time. May need to become configurable

100

Instance Method Summary collapse

Constructor Details

#initializeSessionManager

initialize



89
90
91
92
93
94
95
# File 'lib/monorail/monorail_webd.rb', line 89

def initialize
  # generate one key and throw it away. That way if there is a problem
  # with the entropy generator, it'll generate an exception at the top
  # of the run.
  generate_key
  @sessions = {}
end

Instance Method Details

#create_sessionObject

create_session This is as a good a place as any to purge expired sessions. We also throttle the total number of sessions open at any given time.



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/monorail/monorail_webd.rb', line 145

def create_session
  purge_expired_sessions

  unless @sessions.size < MaxSessions
    raise TooManySessions
  end

  sc = Session.new
  @sessions [sc.session_name.dup] = sc
  sc
end

#generate_keyObject

generate_key



110
111
112
113
114
115
116
117
118
# File 'lib/monorail/monorail_webd.rb', line 110

def generate_key
  if !@key_suffix or @key_suffix > 1000000
    @key_seed = get_key_seed
    raise "Invalid session-key seed" unless @key_seed.length > 8
    @key_suffix = 0
  end
  @key_suffix += 1
  format( "%s%08x", @key_seed, @key_suffix )
end

#get_key_seedObject

get_key_seed This method uses uuidgen, which must be present on the system. Override here to use some other mechanism.



103
104
105
# File 'lib/monorail/monorail_webd.rb', line 103

def get_key_seed
  `uuidgen -r`.chomp.gsub(/[\-]/, "")
end

#purge_expired_sessionsObject

purge_expired_sessions



161
162
163
164
165
# File 'lib/monorail/monorail_webd.rb', line 161

def purge_expired_sessions
  @sessions.delete_if {|k,v|
    v.is_expired?
  }
end

#retrieve_or_create_session(session_name) ⇒ Object

retrieve_or_create_session



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/monorail/monorail_webd.rb', line 123

def retrieve_or_create_session session_name
  sc = @sessions[session_name]

  if sc
    if sc.is_expired?
      @sessions.delete sc.session_name
      sc = nil
    else
      sc.touch
    end
  end

  sc or create_session

end