From 0e7f3c196b123acd9d311f02f09f8b03992cea5f Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 19 Jan 2010 10:02:09 +0100 Subject: move authorization to branch test --- lib/authorization.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/authorization.rb (limited to 'lib/authorization.rb') diff --git a/lib/authorization.rb b/lib/authorization.rb new file mode 100644 index 0000000..372d52b --- /dev/null +++ b/lib/authorization.rb @@ -0,0 +1,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 \ No newline at end of file -- cgit v1.2.3