summaryrefslogtreecommitdiff
path: root/lib/authorization.rb
blob: 9a1760aa08c3911e7ae57f0fcadd88e0b4c4b735 (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="Opentox Webservice Authentication") 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