fluke/devops-service/auth/devops_auth.rb

34 lines
811 B
Ruby
Raw Normal View History

2015-02-25 16:01:22 +03:00
module Sinatra
2015-03-06 12:20:30 +03:00
module DevopsAuth
2015-02-25 16:01:22 +03:00
module Helpers
def protect!
return if auth_with_basic?
2015-03-06 12:20:30 +03:00
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
2015-02-25 16:01:22 +03:00
halt 401, "Not authorized\n"
end
def auth_with_basic?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if @auth.provided? and @auth.basic? and @auth.credentials
c = @auth.credentials
begin
2015-07-16 17:18:55 +03:00
u = Devops::Db.connector.user_auth(c[0], c[1])
2015-03-11 15:10:05 +03:00
request.env['REMOTE_USER'] = c[0]
2015-07-16 17:18:55 +03:00
request.env['USER'] = u
2015-02-25 16:01:22 +03:00
true
rescue RecordNotFound => e
false
end
else
false
end
end
end
def self.registered(app)
2015-03-06 12:20:30 +03:00
app.helpers Sinatra::DevopsAuth::Helpers
2015-02-25 16:01:22 +03:00
end
end
end