Class: OMF::Web::Rack::SessionAuthenticator
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
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
Constructor Details
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
10
11
12
|
# File 'lib/omf-web/rack/session_authenticator.rb', line 10
def self.active?
@@active
end
|
.authenticate ⇒ Object
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
14
15
16
|
# File 'lib/omf-web/rack/session_authenticator.rb', line 14
def self.authenticated?
self[:authenticated]
end
|
.logout ⇒ Object
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)
req = ::Rack::Request.new(env)
sid = nil
path_info = req.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
login_url = @opts[:login_url]
if login_url && login_url != req.path_info
if authenticated = self.class[:authenticated]
if self.class[:valid_until] < Time.now
debug "Session '#{sid}' expired"
authenticated = false
end
end
unless authenticated
return [301, {'Location' => login_url, "Content-Type" => ""}, ['Login first']]
end
end
self.class[:valid_until] = Time.now + @@expire_after
end
status, , body = @app.call(env)
if sid
['Set-Cookie'] = "sid=#{sid}" end
[status, , body]
end
|