Completed
Pull Request — master (#498)
by Dragonqos
04:53 queued 02:07
created

Signature::prepareParameters()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 14
nc 6
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 = null;
25
26
    /**
27
     * @param CredentialsInterface $credentials
28
     */
29
    public function __construct(CredentialsInterface $credentials)
30
    {
31
        $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...
32
    }
33
34
    /**
35
     * @param string $algorithm
36
     */
37
    public function setHashingAlgorithm($algorithm)
38
    {
39
        $this->algorithm = $algorithm;
40
    }
41
42
    /**
43
     * @param string $token
44
     */
45
    public function setTokenSecret($token)
46
    {
47
        $this->tokenSecret = $token;
48
    }
49
50
    /**
51
     * @param UriInterface $uri
52
     * @param array        $params
53
     * @param string       $method
54
     *
55
     * @return string
56
     */
57
    public function getSignature(UriInterface $uri, array $params, $method = 'POST')
58
    {
59
        parse_str($uri->getQuery(), $queryStringData);
60
61
        $params = array_merge($queryStringData, $params);
62
        uksort($params, 'strcmp');
63
64
        $signatureData = $this->prepareParameters($params);
65
66
        // determine base uri
67
        $baseUri = $uri->getScheme() . '://' . $uri->getRawAuthority();
68
69
        if ('/' === $uri->getPath()) {
70
            $baseUri .= $uri->hasExplicitTrailingHostSlash() ? '/' : '';
71
        } else {
72
            $baseUri .= $uri->getPath();
73
        }
74
75
        $baseString = strtoupper($method) . '&';
76
        $baseString .= rawurlencode($baseUri) . '&';
77
        $baseString .= rawurlencode($this->buildSignatureDataString($signatureData));
78
79
        return base64_encode($this->hash($baseString));
80
    }
81
82
    /**
83
     * @param array $signatureData
84
     *
85
     * @return string
86
     */
87
    protected function buildSignatureDataString(array $signatureData)
88
    {
89
        return http_build_query($signatureData, null, '&', PHP_QUERY_RFC3986);
90
    }
91
92
    /**
93
     * Convert booleans to strings, removed unset parameters, and sorts the array
94
     *
95
     * @param array $data Data array
96
     *
97
     * @return array
98
     */
99
    public function prepareParameters($data)
100
    {
101
        ksort($data);
102
        foreach ($data as $key => &$value) {
103
            switch (gettype($value)) {
104
                case 'NULL':
105
                    unset($data[$key]);
106
                    break;
107
                case 'array':
108
                    $data[$key] = self::prepareParameters($value);
109
                    break;
110
                case 'boolean':
111
                    $data[$key] = $value ? 'true' : 'false';
112
                    break;
113
            }
114
        }
115
        return $data;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    protected function getSigningKey()
122
    {
123
        $signingKey = rawurlencode($this->credentials->getConsumerSecret()) . '&';
124
        if ($this->tokenSecret !== null) {
125
            $signingKey .= rawurlencode($this->tokenSecret);
126
        }
127
128
        return $signingKey;
129
    }
130
131
    /**
132
     * @param string $data
133
     *
134
     * @return string
135
     *
136
     * @throws UnsupportedHashAlgorithmException
137
     */
138
    protected function hash($data)
139
    {
140
        switch (strtoupper($this->algorithm)) {
141
            case 'HMAC-SHA1':
142
                return hash_hmac('sha1', $data, $this->getSigningKey(), true);
143
            default:
144
                throw new UnsupportedHashAlgorithmException(
145
                    'Unsupported hashing algorithm (' . $this->algorithm . ') used.'
146
                );
147
        }
148
    }
149
}
150