simple-hmac 0.3.2 → 0.4.0

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: 9163d06bbb21ad88fecc7593c9b7c852bbbd0531
4
- data.tar.gz: ed7590ce93a7fd060f40deb5558a27868421688d
3
+ metadata.gz: 8df9b536189c550e347e8957860c825671827340
4
+ data.tar.gz: b212b86247013ebfa722bacebf794e933bb02cac
5
5
  SHA512:
6
- metadata.gz: 20f18e0693252889a7b475d87d67169fce203de500a6996dccb4b64e97f4b6b1e90bfc7d528a91e5c7808e5c29fe7b6488e64cc38887718b6a304998225d8900
7
- data.tar.gz: 9b35d75510fadb91a227b7dd535aada1d006efe0ebd01db007a0d6d914e5c60d71ceeae6cf1d512aecde6e1afb3fabb317db80f9cfd40975330d4717aec4693b
6
+ metadata.gz: 50544713054a17897ca7bc22e390485adf670acec661bf774adc9fd64ad07e34a1f5c43ed3318d2ddaaa5b3e6012b6db5e3a5253ad90690371837ef809e28589
7
+ data.tar.gz: 54677f8d988498a8fbac9b92dde9724ba26c43c9715440f2adfae7bed991363052c73936fb127eaf65d816857fcb3e2e83443a06d2165275952acb3de2ac4db4
@@ -1,27 +1,9 @@
1
- require 'time'
2
- require 'digest'
3
-
4
1
  module RestClient
5
2
  class Request
6
3
  include SimpleHmac::Helper
7
4
 
8
5
  def sign!(api_id, api_secret, options={})
9
- sign_headers(processed_headers, method, api_id, api_secret, options)
10
- end
11
-
12
- private
13
-
14
- def set_md5_header
15
- return '' unless [:post, :put, :patch].include?(method)
16
- if payload
17
- body = payload.read
18
- payload.instance_variable_get(:@stream).seek(0)
19
- else
20
- body = ''
21
- end
22
- content_md5 = Digest::MD5.base64digest(body)
23
- processed_headers.merge! 'Content-MD5' => content_md5
24
- content_md5
6
+ processed_headers.merge! authentication_headers(method, processed_headers['Content-Type'], payload, url, api_id, api_secret, options={})
25
7
  end
26
8
  end
27
- end
9
+ end
@@ -1,6 +1,7 @@
1
1
  require 'base64'
2
2
  require 'openssl'
3
3
  require 'time'
4
+ require 'digest'
4
5
 
5
6
  module SimpleHmac
6
7
  module Helper
@@ -13,14 +14,32 @@ module SimpleHmac
13
14
  Base64.strict_encode64(OpenSSL::HMAC.digest(options[:algorithm], api_secret, string_to_sign))
14
15
  end
15
16
 
16
- def sign_headers(headers, verb, api_key, api_secret, options={})
17
+ def authentication_headers(verb, content_type, payload, url, api_key, api_secret, options={})
17
18
  options = { auth_prefix: 'WIZYPAY' }.merge(options)
18
19
  auth_prefix = options.delete :auth_prefix
19
20
  date = Time.now.httpdate
20
- headers.merge! 'Date' => date
21
- content_type = (headers['Content-Type'] ||= 'text/plain')
22
- hmac_token = hmac_token(verb, content_type, set_md5_header, url, date, api_secret, options)
23
- headers.merge! 'Authorization' => "#{auth_prefix} #{api_key}:#{hmac_token}"
21
+ content_type ||= 'text/plain'
22
+ content_md5 = digest_md5(verb, payload)
23
+ hmac_token = hmac_token(verb, content_type, content_md5, url, date, api_secret, options)
24
+ {
25
+ 'Date' => date,
26
+ 'Content-Type' => content_type,
27
+ 'Content-MD5' => content_md5,
28
+ 'Authorization' => "#{auth_prefix} #{api_key}:#{hmac_token}"
29
+ }
30
+ end
31
+
32
+ def digest_md5(verb, payload)
33
+ return '' unless [:post, :put, :patch].include?(verb.downcase.to_sym)
34
+ if payload.nil?
35
+ body = ''
36
+ elsif payload.respond_to? :read
37
+ body = payload.read
38
+ payload.instance_variable_get(:@stream).seek(0)
39
+ else
40
+ body = payload
41
+ end
42
+ Digest::MD5.base64digest(body)
24
43
  end
25
44
  end
26
- end
45
+ end
@@ -1,4 +1,3 @@
1
1
  module SimpleHmac
2
- VERSION = '0.3.2'
2
+ VERSION = '0.4.0'
3
3
  end
4
-
@@ -0,0 +1,34 @@
1
+ RSpec.describe SimpleHmac::Helper do
2
+ context '#authentication_headers' do
3
+ before do
4
+ @signer = Object.new
5
+ @signer.extend SimpleHmac::Helper
6
+ end
7
+
8
+ it 'returns the correct GET headers' do
9
+ Timecop.freeze do
10
+ expect(@signer).to receive(:hmac_token).and_return 'hmac_token'
11
+ h = @signer.authentication_headers('GET', nil, nil, '/something', 'my_key', 'my_secret', auth_prefix: 'TEST')
12
+ expect(h).to match({
13
+ 'Date' => Time.now.httpdate,
14
+ 'Content-Type' => 'text/plain',
15
+ 'Content-MD5' => '',
16
+ 'Authorization' => "TEST my_key:hmac_token"
17
+ })
18
+ end
19
+ end
20
+
21
+ it 'returns the correct POST headers' do
22
+ Timecop.freeze do
23
+ expect(@signer).to receive(:hmac_token).and_return 'hmac_token'
24
+ h = @signer.authentication_headers('POST', 'application/json', '{a: 1}', '/something', 'my_key', 'my_secret', auth_prefix: 'TEST')
25
+ expect(h).to match({
26
+ 'Date' => Time.now.httpdate,
27
+ 'Content-Type' => 'application/json',
28
+ 'Content-MD5' => 'B9JGFlnJCn1YCMEwtd2eFA==',
29
+ 'Authorization' => "TEST my_key:hmac_token"
30
+ })
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ require 'rest-client'
2
+
3
+ RSpec.describe RestClient::Request do
4
+ context '#sign!' do
5
+ let!(:req) do
6
+ RestClient::Request.new({
7
+ url: 'http://example.com/something',
8
+ method: :post,
9
+ headers: { accept: :json, content_type: :json },
10
+ payload: '{a: 1}'
11
+ })
12
+ end
13
+
14
+ it 'is signed' do
15
+ Timecop.freeze do
16
+ expect(req).to receive(:hmac_token).and_return 'hmac_token'
17
+ req.sign!('my_key', 'my_secret')
18
+ expect(req.processed_headers).to match({
19
+ 'Date' => Time.now.httpdate,
20
+ 'Accept' => 'application/json',
21
+ 'Accept-Encoding' => 'gzip, deflate',
22
+ 'Authorization' => 'WIZYPAY my_key:hmac_token',
23
+ 'Content-Length' => '6',
24
+ 'Content-MD5' => 'B9JGFlnJCn1YCMEwtd2eFA==',
25
+ 'Content-Type' => 'application/json'
26
+ })
27
+ end
28
+ end
29
+ end
30
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rspec'
2
2
  require 'simple-hmac'
3
3
  require 'webmock/rspec'
4
+ require 'timecop'
4
5
 
5
6
  # Conventionally, all specs live under a `spec` directory, which RSpec adds to
6
7
  # the `$LOAD_PATH`. The generated `.rspec` file contains `--require spec_helper`
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-hmac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chaker Nakhli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-29 00:00:00.000000000 Z
11
+ date: 2016-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.20'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.0
83
97
  description:
84
98
  email:
85
99
  - chaker@wizypay.com
@@ -94,7 +108,9 @@ files:
94
108
  - lib/simple-hmac.rb
95
109
  - lib/simple-hmac/helper.rb
96
110
  - lib/simple-hmac/version.rb
111
+ - spec/authentication_headers_spec.rb
97
112
  - spec/hmac_token_spec.rb
113
+ - spec/sign_request_spec.rb
98
114
  - spec/spec_helper.rb
99
115
  homepage: http://www.wizypay.com
100
116
  licenses:
@@ -122,5 +138,7 @@ signing_key:
122
138
  specification_version: 4
123
139
  summary: Lightweight HMAC implementation for Rails + Restclient.
124
140
  test_files:
141
+ - spec/authentication_headers_spec.rb
125
142
  - spec/hmac_token_spec.rb
143
+ - spec/sign_request_spec.rb
126
144
  - spec/spec_helper.rb