Class: OMF::Web::Rack::SessionAuthenticator

Inherits:
Common::LObject show all
Defined in:
lib/omf-web/rack/session_authenticator.rb

Constant Summary collapse

@@active =
false
@@expire_after =

Expire authenticated session after being idle for that many seconds

2592000

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Loggable

#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn

Constructor Details

#initialize(app, opts = {}) ⇒ SessionAuthenticator

opts -

:no_session - Array of regexp to ignore


43
44
45
46
47
48
49
50
51
# File 'lib/omf-web/rack/session_authenticator.rb', line 43

def initialize(app, opts = {})
  @app = app
  @opts = opts
  @opts[:no_session] = (@opts[:no_session] || []).map { |s| Regexp.new(s) }
  if @opts[:expire_after]
    @@expire_after = @opts[:expire_after]
  end
  @@active = true
end

Class Method Details

.[](key) ⇒ Object



27
28
29
# File 'lib/omf-web/rack/session_authenticator.rb', line 27

def self.[](key)
  OMF::Web::SessionStore[key, :authenticator]
end

.[]=(key, value) ⇒ Object



31
32
33
# File 'lib/omf-web/rack/session_authenticator.rb', line 31

def self.[]=(key, value)
  OMF::Web::SessionStore[key, :authenticator] = value
end

.active?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/omf-web/rack/session_authenticator.rb', line 10

def self.active?
  @@active
end

.authenticateObject



18
19
20
21
# File 'lib/omf-web/rack/session_authenticator.rb', line 18

def self.authenticate
  self[:authenticated] = true
  self[:valid_until] = Time.now + @@expire_after
end

.authenticated?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/omf-web/rack/session_authenticator.rb', line 14

def self.authenticated?
  self[:authenticated]
end

.logoutObject



23
24
25
# File 'lib/omf-web/rack/session_authenticator.rb', line 23

def self.logout
  self[:authenticated] = false
end

Instance Method Details

#call(env) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/omf-web/rack/session_authenticator.rb', line 54

def call(env)
  #puts env.keys.inspect
  req = ::Rack::Request.new(env)
  sid = nil
  path_info = req.path_info
  #puts "REQUEST: #{path_info}"
  unless @opts[:no_session].find {|rx| rx.match(path_info) }
    sid = req.cookies['sid'] || "s#{(rand * 10000000).to_i}_#{(rand * 10000000).to_i}"
    debug "Setting session for '#{req.path_info}' to '#{sid}'"
    Thread.current["sessionID"] = sid
    # If 'login_url' is defined, check if this session is authenticated
     = @opts[:login_url] 
    if  &&  != req.path_info
      if authenticated = self.class[:authenticated]
        # Check if it hasn't imed out
        if self.class[:valid_until] < Time.now
          debug "Session '#{sid}' expired"
          authenticated = false
        end    
      end
      unless authenticated
        return [301, {'Location' => , "Content-Type" => ""}, ['Login first']]
      end
    end
    self.class[:valid_until] = Time.now + @@expire_after
  end
        
  status, headers, body = @app.call(env)
  if sid
    headers['Set-Cookie'] = "sid=#{sid}"  ##: name2=value2; Expires=Wed, 09-Jun-2021 ]
  end
  [status, headers, body]      
end