Signature::getSignature()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 24
c 0
b 0
f 0
rs 9.8333
cc 4
nc 6
nop 3
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
0 ignored issues
show
Bug introduced by
The type OAuth\OAuth1\Signature\Credentials was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 OAuth\Common\Consumer\CredentialsInterface is incompatible with the declared type 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 ?? '');
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