halorgium-auth-hmac 1.1.1.2010090301 → 1.1.1.2010090302

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/auth-hmac.rb +35 -19
  2. metadata +3 -3
data/lib/auth-hmac.rb CHANGED
@@ -74,13 +74,20 @@ class AuthHMAC
74
74
  include Headers
75
75
 
76
76
  def initialize(request, authenticate_referrer=false)
77
- self << request_method(request) + "\n"
78
- self << header_values(request) + "\n"
79
- self << request_path(request, authenticate_referrer)
77
+ @request = request
78
+ @authenticate_referrer = authenticate_referrer
79
+ self << request_method + "\n"
80
+ self << header_values + "\n"
81
+ self << request_path
80
82
  end
81
-
83
+ attr_reader :request, :authenticate_referrer
84
+
82
85
  private
83
- def request_method(request)
86
+ def headers
87
+ super(@request)
88
+ end
89
+
90
+ def request_method
84
91
  if request.respond_to?(:request_method) && request.request_method.is_a?(String)
85
92
  request.request_method
86
93
  elsif request.respond_to?(:method) && request.method.is_a?(String)
@@ -92,42 +99,49 @@ class AuthHMAC
92
99
  end
93
100
  end
94
101
 
95
- def header_values(request)
96
- headers = headers(request)
97
- [ content_type(headers),
98
- (content_md5(headers) or (read_body(request).nil? || read_body(request).empty? ? '' : headers['Content-MD5'] = generate_content_md5(request))),
99
- (date(headers) or headers['Date'] = Time.now.utc.httpdate)
102
+ def header_values
103
+ [ content_type,
104
+ (content_md5 or generated_md5),
105
+ (date or headers['Date'] = Time.now.utc.httpdate)
100
106
  ].join("\n")
101
107
  end
102
108
 
103
- def read_body(request)
109
+ def read_body
104
110
  if request.body.respond_to?(:read)
105
- request.body.rewind
106
111
  request.body.read
112
+ request.body.rewind
107
113
  else
108
114
  request.body
109
115
  end
110
116
  end
111
117
 
112
- def content_type(headers)
118
+ def content_type
113
119
  find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE), headers)
114
120
  end
115
121
 
116
- def date(headers)
122
+ def date
117
123
  find_header(%w(DATE HTTP_DATE), headers)
118
124
  end
119
125
 
120
- def content_md5(headers)
126
+ def content_md5
121
127
  find_header(%w(CONTENT-MD5 CONTENT_MD5 HTTP_CONTENT_MD5), headers)
122
128
  end
123
129
 
124
- def generate_content_md5(request)
125
- OpenSSL::Digest::MD5.hexdigest(read_body(request))
130
+ def generated_md5
131
+ if read_body.nil? || read_body.empty?
132
+ ''
133
+ else
134
+ headers['Content-MD5'] = generate_content_md5
135
+ end
136
+ end
137
+
138
+ def generate_content_md5
139
+ OpenSSL::Digest::MD5.hexdigest(read_body)
126
140
  end
127
141
 
128
- def request_path(request, authenticate_referrer)
142
+ def request_path
129
143
  if authenticate_referrer
130
- headers(request)['Referer'] =~ /^(?:http:\/\/)?[^\/]*(\/.*)$/
144
+ headers['Referer'] =~ /^(?:http:\/\/)?[^\/]*(\/.*)$/
131
145
  path = $1
132
146
  else
133
147
  # Try unparsed_uri in case it is a Webrick request
@@ -178,6 +192,8 @@ class AuthHMAC
178
192
  @signature_method = lambda { |r,ar| @signature_class.send(:new, r, ar) }
179
193
  end
180
194
 
195
+ attr_reader :service_id
196
+
181
197
  # Generates canonical signing string for given request
182
198
  #
183
199
  # Supports same options as AuthHMAC.initialize for overriding service_id and
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: halorgium-auth-hmac
3
3
  version: !ruby/object:Gem::Version
4
- hash: 4020180521
4
+ hash: 4020180527
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
9
  - 1
10
- - 2010090301
11
- version: 1.1.1.2010090301
10
+ - 2010090302
11
+ version: 1.1.1.2010090302
12
12
  platform: ruby
13
13
  authors:
14
14
  - Sean Geoghegan