Skip to content

Commit c6add53

Browse files
committed
Added support to sign with hmac_sha512
1 parent 836ce73 commit c6add53

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Signature/HmacSha512Signature.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\Psr7\Oauth1\Signature;
4+
5+
class HmacSha512Signature extends HmacSignature
6+
{
7+
protected function getHashingAlgorithm()
8+
{
9+
return 'sha512';
10+
}
11+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ApiClients\Tests\Tools\Psr7\Oauth1\Signature;
4+
5+
use ApiClients\Tools\Psr7\Oauth1\Definition\ConsumerSecret;
6+
use ApiClients\Tools\Psr7\Oauth1\Definition\TokenSecret;
7+
use ApiClients\Tools\Psr7\Oauth1\Signature\HmacSha512Signature;
8+
use GuzzleHttp\Psr7\Uri;
9+
use Psr\Http\Message\UriInterface;
10+
11+
class HmacSha512SignatureTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testGetMethod()
14+
{
15+
$this->assertSame('HMAC-SHA512', (new HmacSha512Signature(new ConsumerSecret('secret')))->getMethod());
16+
}
17+
18+
public function provideSign()
19+
{
20+
return [
21+
[
22+
new Uri('https://example.com/'),
23+
'kWHPexc3goH9/jVh2MTYuAqMg6FLTKwZX63C9WmhuoCEoz1Ye5+kew3uSEg/JuQOWxbtN4jm05eSRuvIWmF0ZA==',
24+
'laMFvK2JMbORQp79wtajx0ESMG+U6DFlE0LBOacmsXK80dfTscLljseRi4pHeEUn1V+fDTNUIzTYjCK5DgQmRQ==',
25+
],
26+
[
27+
new Uri('https://example.com/thea.pot',[], 'THEAPOT'),
28+
'/I2kMebX4xOddQTfJevI6wzFvltybVUTsHlL7h5LcsgySv2Me1+6XC35e79sKFWpw5WTYaOCM7D2MNInA5NKUw==',
29+
'kG+XGV4utRgXWt3tbJ5361sWHn86plmm1FcLYfjjRL0l1Ogv0D5lMOMWBrCt1pRGhO05he+DPUqZffyBh4M5GA==',
30+
],
31+
[
32+
new Uri('https://example.com/?foo=bar', ['foo' => 'bar',]),
33+
'buNZpqvSzvJI1G1NLVGuiJnm/ZmZ59yfjGhUCXp8E8J98oEQHTMNua3Kawv41ejoj+KPgqw9JMZ/8HKeO7zvgg==',
34+
'xAllQX+xlZ6bzmthcEDZ9pyC5nyARMPaTuCe34YC9XeFIvAN+QRM1MattDvssurZN8UMPYHeyvbge1rPxygL8A==',
35+
],
36+
[
37+
new Uri('https://example.com/',['foo' => 'bar',], 'HEAD'),
38+
'kWHPexc3goH9/jVh2MTYuAqMg6FLTKwZX63C9WmhuoCEoz1Ye5+kew3uSEg/JuQOWxbtN4jm05eSRuvIWmF0ZA==',
39+
'laMFvK2JMbORQp79wtajx0ESMG+U6DFlE0LBOacmsXK80dfTscLljseRi4pHeEUn1V+fDTNUIzTYjCK5DgQmRQ==',
40+
],
41+
];
42+
}
43+
44+
/**
45+
* @param UriInterface $uri
46+
* @param $signedSignature
47+
* @dataProvider provideSign
48+
*/
49+
public function testSign(UriInterface $uri, $signedSignature, $signedTokenSecretSignature)
50+
{
51+
$secret = new ConsumerSecret('consumerSecret');
52+
$signature = new HmacSha512Signature($secret);
53+
$tokenSecret = new TokenSecret('tokenSecret');
54+
$this->assertSame($signedSignature, $signature->sign($uri));
55+
$signature = $signature->withTokenSecret($tokenSecret);
56+
$this->assertSame($signedTokenSecretSignature, $signature->sign($uri));
57+
$signature = $signature->withoutTokenSecret();
58+
$this->assertSame($signedSignature, $signature->sign($uri));
59+
}
60+
}

0 commit comments

Comments
 (0)