Skip to content

Commit 8aa97d0

Browse files
JeremySiouich1ago
authored andcommitted
Add OAuth::Signature::HMAC::SHA256 and associated tests
1 parent cd4cab0 commit 8aa97d0

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

lib/oauth.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
require 'oauth/client/helper'
99
require 'oauth/signature/hmac/sha1'
10+
require 'oauth/signature/hmac/sha256'
1011
require 'oauth/signature/rsa/sha1'
1112
require 'oauth/request_proxy/mock_request'

lib/oauth/signature/hmac/sha256.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'oauth/signature/base'
2+
3+
module OAuth::Signature::HMAC
4+
class SHA256 < OAuth::Signature::Base
5+
implements 'hmac-sha256'
6+
7+
def body_hash
8+
Base64.encode64(OpenSSL::Digest::SHA256.digest(request.body || '')).chomp.gsub(/\n/,'')
9+
end
10+
11+
private
12+
13+
def digest
14+
OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret, signature_base_string)
15+
end
16+
end
17+
end

test/units/test_hmac_sha256.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require File.expand_path('../../test_helper', __FILE__)
2+
3+
class TestSignatureHmacSha256 < Minitest::Test
4+
def test_that_hmac_sha256_implements_hmac_sha256
5+
assert OAuth::Signature.available_methods.include?('hmac-sha256')
6+
end
7+
8+
def test_that_get_request_from_oauth_test_cases_produces_matching_signature
9+
request = Net::HTTP::Get.new('/photos?file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA256')
10+
11+
consumer = OAuth::Consumer.new('dpf43f3p2l4k3l03', 'kd94hf93k423kf44')
12+
token = OAuth::Token.new('nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00')
13+
14+
signature = OAuth::Signature.sign(request, { :consumer => consumer,
15+
:token => token,
16+
:uri => 'http://photos.example.net/photos',
17+
:signature_method => 'HMAC-SHA256' } )
18+
19+
assert_equal 'WVPzl1j6ZsnkIjWr7e3OZ3jkenL57KwaLFhYsroX1hg=', signature
20+
end
21+
end
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require File.expand_path('../../test_helper', __FILE__)
2+
3+
require 'oauth/signature/hmac/sha256'
4+
5+
class SignatureHMACSHA256Test < Minitest::Test
6+
def test_that_verify_returns_true_when_the_request_signature_is_right
7+
request = OAuth::RequestProxy::MockRequest.new(
8+
'method' => 'POST',
9+
'uri' => 'https://photos.example.net/initialize',
10+
'parameters' => {
11+
'oauth_consumer_key' => 'dpf43f3p2l4k3l03',
12+
'oauth_signature_method' => 'HMAC-SHA256',
13+
'oauth_timestamp' => '137131200',
14+
'oauth_nonce' => 'wIjqoS',
15+
'oauth_callback' => 'http://printer.example.com/ready',
16+
'oauth_version' => '1.0',
17+
'oauth_signature' => 'tkpCGNHi3laWBHQ9+Ka5IOeixEuhxg12LTMlLJxQxKc='
18+
}
19+
)
20+
assert OAuth::Signature::HMAC::SHA256.new(request, :consumer_secret => 'kd94hf93k423kf44').verify
21+
end
22+
23+
def test_that_verify_returns_false_when_the_request_signature_is_wrong
24+
# Test a bug in the OAuth::Signature::Base#== method: when the Base64.decode64 method is
25+
# used on the "self" and "other" signature (as in version 0.4.7), the result may be incorrectly "true".
26+
request = OAuth::RequestProxy::MockRequest.new(
27+
'method' => 'POST',
28+
'uri' => 'https://photos.example.net/initialize',
29+
'parameters' => {
30+
'oauth_consumer_key' => 'dpf43f3p2l4k3l03',
31+
'oauth_signature_method' => 'HMAC-SHA256',
32+
'oauth_timestamp' => '137131200',
33+
'oauth_nonce' => 'wIjqoS',
34+
'oauth_callback' => 'http://printer.example.com/ready',
35+
'oauth_version' => '1.0',
36+
'oauth_signature' => 'tkpCGNHi3laWBHQ9+Ka5IOeixEuhxg12LTMlLJxQxKZ='
37+
}
38+
)
39+
assert !OAuth::Signature::HMAC::SHA256.new(request, :consumer_secret => 'kd94hf93k423kf44').verify
40+
end
41+
end

0 commit comments

Comments
 (0)