Skip to content

Commit

Permalink
Fixed AWS authentication handler to be a simple helper rather than a …
Browse files Browse the repository at this point in the history
…sinatra rewrite. Looks like it plays much nicer.
  • Loading branch information
Randall B committed Jul 17, 2010
1 parent e0e9974 commit a944872
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
32 changes: 0 additions & 32 deletions lib/boardwalk.rb
@@ -1,40 +1,8 @@
module Sinatra
class Request
module AWSHandler
def aws_authenticate
@amz = {}
@env.each do |k, v|
k = k.downcase.gsub('_', '-')
@amz[$1] = v.strip if k =~ /^http-x-amz-([-\w]+)$/
end
date = (@env['HTTP_X_AMZ_DATE'] || @env['HTTP_DATE'])
auth, key, secret = *@env['HTTP_AUTHORIZATION'].to_s.match(/^AWS (\w+):(.+)$/)
uri = @env['PATH_INFO']
uri += "?" + @env['QUERY_STRING'] if RESOURCE_TYPES.include?(@env['QUERY_STRING'])
canonical = [@env['REQUEST_METHOD'], @env['HTTP_CONTENT_MD5'], @env['HTTP_CONTENT_TYPE'], date, uri]
@amz.sort.each do |k, v|
canonical[-1,0] = "x-amz-#{k}:#{v}"
end
@user = User.first(:conditions => {:s3key => key})
if @user and secret != hmac_sha1(@user.s3secret, canonical.map{|v|v.to_s.strip} * "\n")
raise BadAuthentication
end
end
private
def hmac_sha1(key, s)
return Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha1"), key, s)).strip
end
end
end
helpers Request::AWSHandler
end

class Boardwalk < Sinatra::Base
use Rack::FiberPool
load 'lib/boardwalk/mimetypes.rb'
load 'lib/boardwalk/control_routes.rb'
load 'lib/boardwalk/helpers.rb'
load 'lib/boardwalk/errors.rb'
helpers Sinatra::Request::AWSHandler
load 'lib/boardwalk/s3_routes.rb'
end
20 changes: 20 additions & 0 deletions lib/boardwalk/helpers.rb
@@ -1,4 +1,24 @@
helpers do
def aws_authenticate
@amz = {}
@env.each do |k, v|
k = k.downcase.gsub('_', '-')
@amz[$1] = v.strip if k =~ /^http-x-amz-([-\w]+)$/
end
date = (@env['HTTP_X_AMZ_DATE'] || @env['HTTP_DATE'])
auth, key, secret = *@env['HTTP_AUTHORIZATION'].to_s.match(/^AWS (\w+):(.+)$/)
uri = @env['PATH_INFO']
uri += "?" + @env['QUERY_STRING'] if RESOURCE_TYPES.include?(@env['QUERY_STRING'])
canonical = [@env['REQUEST_METHOD'], @env['HTTP_CONTENT_MD5'], @env['HTTP_CONTENT_TYPE'], date, uri]
@amz.sort.each do |k, v|
canonical[-1,0] = "x-amz-#{k}:#{v}"
end
@user = User.first(:conditions => {:s3key => key})
if @user and secret != hmac_sha1(@user.s3secret, canonical.map{|v|v.to_s.strip} * "\n")
raise BadAuthentication
end
end

def generate_secret
abc = %{ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz}
(1..40).map { abc[rand(abc.size),1] }.join
Expand Down

0 comments on commit a944872

Please sign in to comment.