|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SocialConnect project |
|
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace SocialConnect\OAuth1\Signature; |
|
8
|
|
|
|
|
9
|
|
|
use SocialConnect\Provider\Consumer; |
|
10
|
|
|
use SocialConnect\OAuth1\Request; |
|
11
|
|
|
use SocialConnect\OAuth1\Token; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractSignatureMethod |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Needs to return the name of the Signature Method (ie HMAC-SHA1) |
|
17
|
|
|
* |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract public function getName(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Build up the signature |
|
24
|
|
|
* NOTE: The output of this function MUST NOT be urlencoded. |
|
25
|
|
|
* the encoding is handled in OAuthRequest when the final |
|
26
|
|
|
* request is serialized |
|
27
|
|
|
* |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
* @param Consumer $consumer |
|
30
|
|
|
* @param Token $token |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract public function buildSignature(Request $request, Consumer $consumer, Token $token); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Verifies that a given signature is correct |
|
37
|
|
|
* |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* @param Consumer $consumer |
|
40
|
|
|
* @param Token $token |
|
41
|
|
|
* @param string $signature |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public function checkSignature(Request $request, Consumer $consumer, Token $token, $signature) |
|
45
|
|
|
{ |
|
46
|
|
|
$built = $this->buildSignature($request, $consumer, $token); |
|
47
|
|
|
if (strlen($built) == 0 || strlen($signature) == 0) { // Check for zero length, although unlikely here |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (strlen($built) != strlen($signature)) { |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Avoid a timing leak with a (hopefully) time insensitive compare |
|
56
|
|
|
$result = 0; |
|
57
|
|
|
for ($i = 0; $i < strlen($signature); $i ++) { |
|
58
|
|
|
$result |= ord($built {$i}) ^ ord($signature {$i}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $result == 0; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|