link-share/helpers/application_helper.rb

24 lines
515 B
Ruby
Raw Normal View History

2015-07-21 10:33:31 -04:00
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
2016-10-11 00:33:44 -04:00
return unless @auth.provided? && @auth.basic?
2015-07-21 10:33:31 -04:00
2016-10-11 00:33:44 -04:00
authenticate?(@auth.credentials)
end
2015-07-21 10:33:31 -04:00
2016-10-11 00:33:44 -04:00
def authenticate?(credentials)
username, password = credentials
users.keys.include?(username) && users[username] == password
2015-07-21 10:33:31 -04:00
end
2016-10-11 00:33:44 -04:00
def users
settings.users
2015-07-21 10:33:31 -04:00
end
end