rack-contrib-sign 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e063e40cf6ff43aa7ccd9f89e905d4f87a733baf
4
- data.tar.gz: 7a20e29567951a4d2908bb5a64aea76820fb4c87
3
+ metadata.gz: 0f04f49f937e4e268755732df3b62e1fa36a0646
4
+ data.tar.gz: f93c5ed88d1dd26885d43014f1b25818a77e0617
5
5
  SHA512:
6
- metadata.gz: 7d2255f91a0a46d4f65a26ab6589a1c480ef9caa5fa0a2b27f55900cb90c367f4f1689e06ea836df5552a074f7c3256aa939b0e0a6bd9f41f64d3092461321a2
7
- data.tar.gz: e4013973326b23f2f14101a725b775f908409ccefaa313208257460b76e2fafddad8166c4014de3a671d51f50ea73df4947343e83d229f25029f06da19e373ea
6
+ metadata.gz: 5152592a6cbcae4aa188c8fdd81cbe647a74dcd0bdc84898a8693e1624579edbb8fe2d7003d93c9c3442eccafd2f23f25a0fe7d64f25676e6a895a7625464cec
7
+ data.tar.gz: b6b67f84cadd35298807bb9136ef20c43cdb802b8061e72eb05102dfc39d93674e25fb34c405ab4ecf738d309d0fc8c6fc126fe014ab8d9a42fde35f39f7bc91
@@ -1,4 +1,5 @@
1
1
 
2
+ require 'digest/md5'
2
3
  require 'logger'
3
4
 
4
5
  require "rack/contrib/sign/version"
@@ -47,9 +47,11 @@ module Rack
47
47
  # Extract environmental data into a Receipt
48
48
  def build_receipt env, credentials
49
49
  receipt = Rack::Contrib::Sign::Receipt.new
50
+ receipt.host = env['rack.url_scheme'] + '://' + env['HTTP_HOST']
50
51
  receipt.uri = env['REQUEST_URI']
51
52
  receipt.request_method = env['REQUEST_METHOD']
52
53
  receipt.body = extract_body env
54
+ receipt.content_type = env['HTTP_CONTENT_TYPE'] || ''
53
55
 
54
56
  extract_headers(env).each { |h,v| receipt.headers[h] = v }
55
57
 
@@ -8,7 +8,9 @@ module Rack
8
8
  attr_accessor :api_key
9
9
  attr_accessor :api_secret
10
10
  attr_accessor :body
11
+ attr_accessor :content_type
11
12
  attr_accessor :uri
13
+ attr_accessor :host
12
14
 
13
15
  def initialize
14
16
  @headers = {}
@@ -18,17 +20,27 @@ module Rack
18
20
  @request_method = s.upcase
19
21
  end
20
22
 
23
+ def body_md5
24
+ Digest::MD5.hexdigest(body)
25
+ end
26
+
27
+ def body_length
28
+ body.length
29
+ end
30
+
21
31
  def to_s
22
32
  preamble + header_text
23
33
  end
24
34
 
25
35
  def preamble
26
36
  s = ""
27
- s << "%s %s\n" % [request_method, uri]
37
+ s << "%s\n" % request_method
38
+ s << "%s\n" % host
39
+ s << "%s\n" % uri
28
40
  s << "%s\n" % api_key
29
- s << "%s\n" % api_secret
30
- s << "%s\n" % body
31
- s << "--\n"
41
+ s << "%s\n" % content_type
42
+ s << "%s\n" % body_length
43
+ s << "%s\n" % body_md5
32
44
  s
33
45
  end
34
46
 
@@ -1,7 +1,7 @@
1
1
  module Rack
2
2
  module Contrib
3
3
  module Sign
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -29,7 +29,10 @@ describe Rack::Contrib::Sign::Middleware do
29
29
  it "sets various options" do
30
30
  env = {
31
31
  'REQUEST_METHOD' => 'POST',
32
- 'REQUEST_URI' => 'foo/bar',
32
+ 'HTTP_HOST' => '127.0.0.1:9292',
33
+ 'rack.url_scheme' => 'http',
34
+ 'HTTP_CONTENT_TYPE' => 'text/plain',
35
+ 'REQUEST_URI' => '/foo/bar',
33
36
  'HTTP_HI_FOOO' => 'YIPEE',
34
37
  'rack.input' => StringIO.new('foo=bar')
35
38
  }
@@ -38,7 +41,9 @@ describe Rack::Contrib::Sign::Middleware do
38
41
 
39
42
  receipt = ware.build_receipt(env, creds)
40
43
 
41
- receipt.uri.should eq('foo/bar')
44
+ receipt.host.should eq('http://127.0.0.1:9292')
45
+ receipt.content_type.should eq('text/plain')
46
+ receipt.uri.should eq('/foo/bar')
42
47
  receipt.request_method.should eq('POST')
43
48
  receipt.body.should eq('foo=bar')
44
49
  receipt.api_key.should eq('123')
@@ -130,6 +135,8 @@ describe Rack::Contrib::Sign::Middleware do
130
135
  env = {
131
136
  'HTTP_AUTHORIZATION' => 'foo-bar 123:foo',
132
137
  'REQUEST_METHOD' => '?',
138
+ 'HTTP_HOST' => '127.0.0.1:9292',
139
+ 'rack.url_scheme' => 'http',
133
140
  'rack.input' => StringIO.new()
134
141
  }
135
142
 
@@ -143,7 +150,9 @@ describe Rack::Contrib::Sign::Middleware do
143
150
  env = {
144
151
  'HTTP_AUTHORIZATION' => 'foo-bar abc:YABBA DABBA DOOO',
145
152
  'REQUEST_METHOD' => 'POST',
146
- 'REQUEST_URI' => 'http://foo/bar/baz',
153
+ 'HTTP_HOST' => '127.0.0.1:9292',
154
+ 'rack.url_scheme' => 'http',
155
+ 'REQUEST_URI' => '/foo/bar/baz',
147
156
  'rack.input' => StringIO.new('foo=bar'),
148
157
  }
149
158
 
@@ -154,9 +163,12 @@ describe Rack::Contrib::Sign::Middleware do
154
163
  it "works when I sign it right" do
155
164
  cred_provider['123'] = 'abc'
156
165
  env = {
157
- 'HTTP_AUTHORIZATION' => 'foo-bar 123:0d501b6934dc0ec5f1452947a7afd108e41c91af',
166
+ 'HTTP_AUTHORIZATION' => 'foo-bar 123:75e8d7d1c4eeb07a049f9f7c1395aa2e61e5a879',
158
167
  'REQUEST_METHOD' => 'POST',
159
- 'REQUEST_URI' => 'http://foo/bar/baz',
168
+ 'HTTP_HOST' => '127.0.0.1:9292',
169
+ 'HTTP_CONTENT_TYPE' => 'text/plain',
170
+ 'rack.url_scheme' => 'http',
171
+ 'REQUEST_URI' => '/foo/bar/baz',
160
172
  'rack.input' => StringIO.new('foo=bar'),
161
173
  'HTTP_HI_FOOOO' => 'aoenuoneuh',
162
174
  'HTTP_BYE_FOOO' => 'oeucorgcgc'
@@ -9,8 +9,12 @@ describe Rack::Contrib::Sign::Receipt do
9
9
  it { should respond_to(:api_secret=) }
10
10
  it { should respond_to(:body) }
11
11
  it { should respond_to(:body=) }
12
+ it { should respond_to(:content_type) }
13
+ it { should respond_to(:content_type=) }
12
14
  it { should respond_to(:request_method) }
13
15
  it { should respond_to(:request_method=) }
16
+ it { should respond_to(:host) }
17
+ it { should respond_to(:host=) }
14
18
  it { should respond_to(:uri) }
15
19
  it { should respond_to(:uri=) }
16
20
  it { should respond_to(:headers) }
@@ -22,6 +26,22 @@ describe Rack::Contrib::Sign::Receipt do
22
26
  end
23
27
  end
24
28
 
29
+ describe "#body_md5" do
30
+ it "returns the md5 of the body" do
31
+ receipt.body = 'herro'
32
+
33
+ receipt.body_md5.should eq("18f1072de45420e57fd22ee5bd59df9e")
34
+ end
35
+ end
36
+
37
+ describe "#body_length" do
38
+ it "returns the length of the body" do
39
+ receipt.body = 'herro'
40
+
41
+ receipt.body_length.should eq(5)
42
+ end
43
+ end
44
+
25
45
  describe "#headers" do
26
46
  it "defaults to an empty hash" do
27
47
  receipt.headers.should eq({})
@@ -43,18 +63,22 @@ describe Rack::Contrib::Sign::Receipt do
43
63
  describe "#preamble" do
44
64
  it "incorporates all the preamble elements in a string block" do
45
65
  receipt.api_key = 'abc'
46
- receipt.api_secret = '123'
47
66
  receipt.body = 'foo=bar'
48
67
  receipt.request_method = 'post'
49
- receipt.uri = 'http://example.com/123'
68
+ receipt.host = 'http://example.com'
69
+ receipt.uri = '/123?foo=bar'
70
+ receipt.content_type = "text/json"
50
71
 
51
72
  returned = receipt.preamble
52
73
 
53
- r = "POST http://example.com/123\n"
74
+ r = ""
75
+ r << "POST\n"
76
+ r << "http://example.com\n"
77
+ r << "/123?foo=bar\n"
54
78
  r << "abc\n"
55
- r << "123\n"
56
- r << "foo=bar\n"
57
- r << "--\n"
79
+ r << "text/json\n"
80
+ r << "7\n"
81
+ r << "06ad47d8e64bd28de537b62ff85357c4\n"
58
82
 
59
83
  returned.should eq(r)
60
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-contrib-sign
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Graham Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-23 00:00:00.000000000 Z
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler