Class: AWSAuth::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-auth.rb

Constant Summary collapse

VERSION =
"0.9.1"
ROOT_DIR =
ENV['AWS_ROOT_DIR'] || File.join(File.dirname(__FILE__),'..')
DEFAULT_PASSWORD =
"pass@word1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Base

Returns a new instance of Base.



24
25
26
27
# File 'lib/aws-auth.rb', line 24

def initialize(app)
  @app = app
  AWSAuth::User.establish_connection(AWSAuth::Base.config[:auth])
end

Class Method Details

.configObject



16
17
18
# File 'lib/aws-auth.rb', line 16

def self.config
  @@config ||= load_config()
end

.config_path=(val) ⇒ Object



20
21
22
# File 'lib/aws-auth.rb', line 20

def self.config_path=(val)
  @@config_path = val
end

.generate_keyObject



89
90
91
92
# File 'lib/aws-auth.rb', line 89

def self.generate_key
  abc = %{ABCDEF0123456789}
  (1..20).map { abc[rand(abc.size),1] }.join
end

.generate_secretObject



84
85
86
87
# File 'lib/aws-auth.rb', line 84

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

.hmac_sha1(key, s) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aws-auth.rb', line 94

def self.hmac_sha1(key, s)
  ipad = [].fill(0x36, 0, 64)
  opad = [].fill(0x5C, 0, 64)
  key = key.unpack("C*")
  if key.length < 64 then
    key += [].fill(0, 0, 64-key.length)
  end

  inner = []
  64.times { |i| inner.push(key[i] ^ ipad[i]) }
  inner += s.unpack("C*")

  outer = []
  64.times { |i| outer.push(key[i] ^ opad[i]) }
  outer = outer.pack("c*")
  outer += Digest::SHA1.digest(inner.pack("c*"))

  return Base64::encode64(Digest::SHA1.digest(outer)).chomp
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aws-auth.rb', line 29

def call(env)
  date_s = env['HTTP_X_AMZ_DATE'] || env['HTTP_DATE']
  request = Rack::Request.new(env)
  auth = Rack::Auth::Basic::Request.new(env)

  # Basic Auth
  if auth.provided? && auth.basic?
    user = AWSAuth::User.(auth.credentials[0])
    unless user.nil?
      if user.password == AWSAuth::Base.hmac_sha1( auth.credentials[1], user.secret )
        env['AWS_AUTH_USER'] = user
      end
    end
  # Route 53
  elsif env['HTTP_X_AMZN_AUTHORIZATION'] =~ /^AWS3-HTTPS AWSAccessKeyId=(.*),Algorithm=HmacSHA256,Signature=(.*)$/
    access_key = $1
    signature = $2
    user = AWSAuth::User.find_by_key(access_key)
    unless user.nil?
      hmac = HMAC::SHA256.new(user.secret)
      hmac.update(date_s)
      if Base64.encode64(hmac.digest).chomp == signature
        env['AWS_AUTH_USER'] = user
      end
    end
  # S3
  elsif env['HTTP_AUTHORIZATION'] =~ /^AWS (\w+):(.+)$/ || !request['Signature'].nil?
    meta, amz = {}, {}
    env.each do |k,v|
      k = k.downcase.gsub('_', '-')
      amz[$1] = v.strip if k =~ /^http-x-amz-([-\w]+)$/
      meta[$1] = v if k =~ /^http-x-amz-meta-([-\w]+)$/
    end

    auth, key_s, secret_s = *env['HTTP_AUTHORIZATION'].to_s.match(/^AWS (\w+):(.+)$/)
    if request.params.has_key?('Signature') and Time.at(request['Expires'].to_i) >= Time.now
      key_s, secret_s, date_s = request['AWSAccessKeyId'], request['Signature'], request['Expires']
    end
    uri = env['PATH_INFO']
    uri += "?" + env['QUERY_STRING'] if %w[acl versioning torrent].include?(env['QUERY_STRING'])
    canonical = [env['REQUEST_METHOD'], env['HTTP_CONTENT_MD5'], env['CONTENT_TYPE'],
	date_s, uri]
    amz.sort.each do |k, v|
      canonical[-1,0] = "x-amz-#{k}:#{v}"
    end

    user = AWSAuth::User.find_by_key key_s
    unless (user and secret_s != AWSAuth::Base.hmac_sha1(user.secret, canonical.map{|v|v.to_s.strip} * "\n")) || (user and user.deleted == 1)
      env['AWS_AUTH_USER'] = user
    end
  end

  @app.call(env)
end