Signature::setTokenSecret()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace OAuth\OAuth1\Signature;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Uri\UriInterface;
7
use OAuth\OAuth1\Signature\Exception\UnsupportedHashAlgorithmException;
8
9
class Signature implements SignatureInterface
10
{
11
    /**
12
     * @var Credentials
13
     */
14
    protected $credentials;
15
16
    /**
17
     * @var string
18
     */
19
    protected $algorithm;
20
21
    /**
22
     * @var string
23
     */
24
    protected $tokenSecret;
25
26
    public function __construct(CredentialsInterface $credentials)
27
    {
28
        $this->credentials = $credentials;
0 ignored issues
show
Documentation Bug introduced by
It seems like $credentials of type object<OAuth\Common\Cons...r\CredentialsInterface> is incompatible with the declared type object<OAuth\OAuth1\Signature\Credentials> of property $credentials.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
    }
30
31
    /**
32
     * @param string $algorithm
33
     */
34
    public function setHashingAlgorithm($algorithm): void
35
    {
36
        $this->algorithm = $algorithm;
37
    }
38
39
    /**
40
     * @param string $token
41
     */
42
    public function setTokenSecret($token): void
43
    {
44
        $this->tokenSecret = $token;
45
    }
46
47
    /**
48
     * @param string       $method
49
     *
50
     * @return string
51
     */
52
    public function getSignature(UriInterface $uri, array $params, $method = 'POST')
53
    {
54
        parse_str($uri->getQuery(), $queryStringData);
55
56
        foreach (array_merge($queryStringData, $params) as $key => $value) {
57
            $signatureData[rawurlencode($key)] = rawurlencode($value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$signatureData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $signatureData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
58
        }
59
60
        ksort($signatureData);
61
62
        // determine base uri
63
        $baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority();
64
65
        if ('/' === $uri->getPath()) {
66
            $baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : '';
67
        } else {
68
            $baseUri .= $uri->getPath();
69
        }
70
71
        $baseString = strtoupper($method) . '&';
72
        $baseString .= rawurlencode($baseUri) . '&';
73
        $baseString .= rawurlencode($this->buildSignatureDataString($signatureData));
74
75
        return base64_encode($this->hash($baseString));
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function buildSignatureDataString(array $signatureData)
82
    {
83
        $signatureString = '';
84
        $delimiter = '';
85
        foreach ($signatureData as $key => $value) {
86
            $signatureString .= $delimiter . $key . '=' . $value;
87
88
            $delimiter = '&';
89
        }
90
91
        return $signatureString;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    protected function getSigningKey()
98
    {
99
        $signingKey = rawurlencode($this->credentials->getConsumerSecret()) . '&';
100
        if ($this->tokenSecret !== null) {
101
            $signingKey .= rawurlencode($this->tokenSecret);
102
        }
103
104
        return $signingKey;
105
    }
106
107
    /**
108
     * @param string $data
109
     *
110
     * @return string
111
     */
112
    protected function hash($data)
113
    {
114
        switch (strtoupper($this->algorithm)) {
115
            case 'HMAC-SHA1':
116
                return hash_hmac('sha1', $data, $this->getSigningKey(), true);
117
            default:
118
                throw new UnsupportedHashAlgorithmException(
119
                    'Unsupported hashing algorithm (' . $this->algorithm . ') used.'
120
                );
121
        }
122
    }
123
}
124