Permalink
Browse files

Proper error handling when no API key is given.

Which, with the git version I have, is always.
  • Loading branch information...
1 parent d133016 commit f71f556f68422ba1ecdae3471ee8362473fd1869 @technomancy committed Nov 22, 2012
Showing with 17 additions and 3 deletions.
  1. +17 −3 lib/sokoban.rb
View
20 lib/sokoban.rb
@@ -4,6 +4,10 @@
require "uuid"
module Sokoban
+ class Error < StandardError
+ attr_accessor :status
+ end
+
class Proxy
def initialize
@uuid = UUID.new
@@ -28,12 +32,13 @@ def proxy(env, base_url)
end
def call(env)
- api_key = Rack::Auth::Basic::Request.new(env).credentials[1]
app_name = env["REQUEST_PATH"][/^(.+?)\.git/, 1]
- receiver_url = ensure_receiver(app_name, api_key)
+ receiver_url = ensure_receiver(app_name, api_key(env))
- puts "call app_name=#{app_name} api_key=#{api_key} receiver=#{receiver_url}"
+ puts "call app_name=#{app_name} receiver=#{receiver_url}"
proxy(env, receiver_url)
+ rescue Error => e
+ [e.status, {"Content-Type" => "text/plain"}, [e.message]]
end
def ensure_receiver(app_name, api_key)
@@ -53,5 +58,14 @@ def launch(app_name, api_key)
heroku.post_ps(app_name, command, { :ps_env => receiver_config })
@redis.blpop.tap {|receiver| @redis.hset(app_name, receiver) }
end
+
+ def api_key(env)
+ auth = Rack::Auth::Basic::Request.new(env)
+ if auth.provided? && auth.basic? && auth.credentials
+ auth.credentials[1]
+ else
+ raise Error.new("Not authorized\n").tap{|e| e.status = 401 }
+ end
+ end
end
end

0 comments on commit f71f556

Please sign in to comment.