Completed
Push — master ( 9b4aa4...587cf8 )
by Дмитрий
03:12
created

AbstractSignatureMethod   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 51
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getName() 0 1 ?
buildSignature() 0 1 ?
B checkSignature() 0 19 5
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Auth\Provider\OAuth1\Signature;
8
9
use SocialConnect\Auth\Provider\Consumer;
10
use SocialConnect\Auth\Provider\OAuth1\Request;
11
use SocialConnect\Auth\Provider\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