Skip to content

Instantly share code, notes, and snippets.

@crohr
Created June 15, 2011 20:45
Show Gist options
  • Save crohr/1028064 to your computer and use it in GitHub Desktop.
Save crohr/1028064 to your computer and use it in GitHub Desktop.
Trying to abort upload if no authentication provided
require 'goliath'
require 'tempfile'
class Authn < Goliath::API
def protected!(env)
raise Goliath::Validation::UnauthorizedError.new unless authorized?(env)
end
def authorized?(env)
auth = Rack::Auth::Basic::Request.new(env)
auth.provided? && auth.basic? && auth.credentials && auth.credentials == ['admin', 'admin']
end
def on_headers(env, headers)
protected!(env)
env.logger.info 'received headers: ' + headers.inspect
env['async-headers'] = headers
end
def on_body(env, data)
env.logger.info "Received data!"
end
def on_close(env)
env.logger.info 'closing connection'
end
def response(env)
env.logger.info "In #response!"
[200, {'Content-Type' => 'text/plain'}, "Authenticated!"]
end
end
ruby-1.9.2 ~ $ curl http://localhost:9000/ -T IMG_0223.JPG
{"error":"Unauthorized"}
[email protected] ~/dev/flag.io(master) $ bundle exec ruby authn.rb -sv
[6283:INFO] 2011-06-15 22:42:31 :: Starting server on 0.0.0.0:9000 in development mode. Watch out for stones.
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Received data!
[6283:INFO] 2011-06-15 22:42:34 :: Status: 401, Content-Length: 24, Response Time: 2012.06ms
[6283:INFO] 2011-06-15 22:42:34 :: closing connection
[6283:INFO] 2011-06-15 22:42:34 :: In #response!
[6283:INFO] 2011-06-15 22:42:34 :: Status: 200, Content-Length: 24, Response Time: 2044.00ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment