34 lines
757 B
Ruby
34 lines
757 B
Ruby
module Sinatra
|
|
module DevopsAuth
|
|
module Helpers
|
|
def protect!
|
|
return if auth_with_basic?
|
|
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
|
|
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
|
|
Devops::Db.connector.user_auth(c[0], c[1])
|
|
true
|
|
rescue RecordNotFound => e
|
|
false
|
|
end
|
|
else
|
|
false
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.registered(app)
|
|
app.helpers Sinatra::DevopsAuth::Helpers
|
|
end
|
|
|
|
end
|
|
|
|
register Sinatra::DevopsAuth
|
|
end
|