summaryrefslogtreecommitdiff
path: root/lib/authorization.rb
blob: 372d52b580469629ab045870368931b9e075c0d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
helpers do
 
  def protected!
    response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth") and \
    throw(:halt, [401, "Not authorized\n"]) and \
    return unless authorized?
  end
 
  def authorized?
    @auth ||= Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? && @auth.basic? && @auth.credentials && valid_user?
  end
  
  def valid_user?
    users = @@users[:users]
    return @auth.credentials == [@auth.username, users.fetch(@auth.username)] if users.has_key?(@auth.username)
    return false
  end
 
end
 
before do
  protected! unless env['REQUEST_METHOD'] == "GET"
end