-
Notifications
You must be signed in to change notification settings - Fork 106
/
authenticator.rb
48 lines (37 loc) · 952 Bytes
/
authenticator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
=begin rdoc
A rack middleware for handling authentication.
=end
class Authenticator
UnAuthenticated = /^(\/home\/login|\/home\/logout|\/password_resets.*|\/assets.*)$/
######
class SessionAuth < Rack::Auth::AbstractHandler
def initialize(app, realm=nil, &authenticator)
@app = app
end
def call(env)
return unauthorized(env) unless User.find_by_id(env['rack.session'][:user_id])
@app.call(env)
end
private
def unauthorized(env)
[ 302, {'Content-type' => 'text/plain', 'Location' => env['SCRIPT_NAME'] + '/home/login',
'Content-length' => '0'}, []]
end
end
#####
def initialize(app, realm=nil)
@app = app
@realm = realm
@auths = [SessionAuth.new(app, realm){}]
end
def select_auth(env)
@auths.first
end
def call(env)
if env["PATH_INFO"] =~ UnAuthenticated
@app.call(env)
else
select_auth(env).call(env)
end
end
end