Passed
Pull Request — master (#18)
by
unknown
09:46
created

shouldSignWithClientCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Risan\OAuth1\Request;
4
5
use DateTime;
6
use Risan\OAuth1\Config\ConfigInterface;
7
use Risan\OAuth1\Signature\SignerInterface;
8
use Risan\OAuth1\Credentials\TokenCredentials;
9
use Risan\OAuth1\Credentials\TemporaryCredentials;
10
use Risan\OAuth1\Credentials\ServerIssuedCredentials;
11
12
class ProtocolParameter implements ProtocolParameterInterface
13
{
14
    /**
15
     * The ConfigInterface instance.
16
     *
17
     * @var \Risan\OAuth1\Config\ConfigInterface
18
     */
19
    protected $config;
20
21
    /**
22
     * The SignerInterface instance.
23
     *
24
     * @var \Risan\OAuth1\Signature\SignerInterface
25
     */
26
    protected $signer;
27
28
    /**
29
     * The NonceGeneratorInterface instance.
30
     *
31
     * @var \Risan\OAuth1\Request\NonceGeneratorInterface
32
     */
33
    protected $nonceGenerator;
34
35
    /**
36
     * Create ProtocolParameter instance.
37
     *
38
     * @param \Risan\OAuth1\Config\ConfigInterface          $config
39
     * @param \Risan\OAuth1\Signature\SignerInterface       $signer
40
     * @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator
41
     */
42 21
    public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator)
43
    {
44 21
        $this->config = $config;
45 21
        $this->signer = $signer;
46 21
        $this->nonceGenerator = $nonceGenerator;
47 21
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function getConfig()
53
    {
54 1
        return $this->config;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    public function getSigner()
61
    {
62 3
        return $this->signer;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function getNonceGenerator()
69
    {
70 1
        return $this->nonceGenerator;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function getCurrentTimestamp()
77
    {
78 1
        return (new DateTime())->getTimestamp();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 2
    public function getVersion()
85
    {
86 2
        return '1.0';
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function getBase()
93
    {
94
        return [
95 1
            'oauth_consumer_key' => $this->config->getClientCredentialsIdentifier(),
96 1
            'oauth_nonce' => $this->nonceGenerator->generate(),
97 1
            'oauth_signature_method' => $this->signer->getMethod(),
98 1
            'oauth_timestamp' => "{$this->getCurrentTimestamp()}",
99 1
            'oauth_version' => $this->getVersion(),
100
        ];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function forTemporaryCredentials()
107
    {
108 1
        $parameters = $this->getBase();
109
110 1
        if ($this->config->hasCallbackUri()) {
111 1
            $parameters['oauth_callback'] = $this->config->getCallbackUri();
112
        }
113
114 1
        $parameters['oauth_signature'] = $this->getSignature($parameters, $this->config->getTemporaryCredentialsUri());
115
116 1
        return $parameters;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function forTokenCredentials(TemporaryCredentials $temporaryCredentials, $verificationCode)
123
    {
124
        $parameters = $this->getBase();
125
126
        $parameters['oauth_token'] = $temporaryCredentials->getIdentifier();
127
        $parameters['oauth_verifier'] = $verificationCode;
128
129
        $requestOptions = [
130
            'form_params' => ['oauth_verifier' => $verificationCode],
131
        ];
132
133
        $parameters['oauth_signature'] = $this->getSignature(
134
            $parameters,
135
            $this->config->getTokenCredentialsUri(),
136
            $temporaryCredentials,
137
            $requestOptions
138
        );
139
140
        return $parameters;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function forProtectedResource(TokenCredentials $tokenCredentials, $httpMethod, $uri, array $requestOptions = [])
147
    {
148 1
        $parameters = $this->getBase();
149
150 1
        $parameters['oauth_token'] = $tokenCredentials->getIdentifier();
151
152 1
        $parameters['oauth_signature'] = $this->getSignature(
153 1
            $parameters,
154 1
            $this->config->buildUri($uri),
155 1
            $tokenCredentials,
156 1
            $requestOptions,
157 1
            $httpMethod
158
        );
159
160 1
        return $parameters;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 1
    public function getSignature(array $protocolParameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST')
167
    {
168 1
        $signatureParameters = $this->signatureParameters($protocolParameters, $requestOptions);
169
170 1
        return $this->setupSigner($serverIssuedCredentials)
171 1
            ->sign($uri, $signatureParameters, $httpMethod);
172
    }
173
174
    /**
175
     * Build the signature parameters to be signed.
176
     *
177
     * @param array $protocolParameters
178
     * @param array $requestOptions
179
     *
180
     * @return array
181
     */
182 1
    public function signatureParameters(array $protocolParameters, array $requestOptions = [])
183
    {
184 1
        $parameters = $protocolParameters;
185
186 1
        if ($this->requestOptionsHas($requestOptions, 'query')) {
187 1
            $parameters = array_merge($parameters, $requestOptions['query']);
188
        }
189
190 1
        if ($this->requestOptionsHas($requestOptions, 'form_params')) {
191 1
            $parameters = array_merge($parameters, $requestOptions['form_params']);
192
        }
193
194 1
        return $parameters;
195
    }
196
197
    /**
198
     * Setup the signer.
199
     *
200
     * @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials
201
     *
202
     * @return \Risan\OAuth1\Signature\SignerInterface
203
     */
204 1
    public function setupSigner(ServerIssuedCredentials $serverIssuedCredentials = null)
205
    {
206 1
        if ($this->shouldSignWithClientCredentials()) {
207 1
            $this->signer->setClientCredentials($this->config->getClientCredentials());
208
        }
209
210 1
        if ($this->shouldSignWithServerIssuedCredentials($serverIssuedCredentials)) {
211 1
            $this->signer->setServerIssuedCredentials($serverIssuedCredentials);
212
        }
213
214 1
        return $this->signer;
215
    }
216
217
    /**
218
     * Should sign with the client credentials.
219
     *
220
     * @return bool
221
     */
222 2
    public function shouldSignWithClientCredentials()
223
    {
224 2
        return $this->signer->isKeyBased();
225
    }
226
227
    /**
228
     * Should sign with the server issued credentials.
229
     *
230
     * @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials
231
     *
232
     * @return bool
233
     */
234 2
    public function shouldSignWithServerIssuedCredentials(ServerIssuedCredentials $serverIssuedCredentials = null)
235
    {
236 2
        return $this->signer->isKeyBased() && null !== $serverIssuedCredentials;
237
    }
238
239
    /**
240
     * Check if request options has the given key option.
241
     *
242
     * @param array  $requestOptions
243
     * @param string $key
244
     *
245
     * @return bool
246
     */
247 2
    public function requestOptionsHas(array $requestOptions, $key)
248
    {
249 2
        return isset($requestOptions[$key]) &&
250 2
            is_array($requestOptions[$key]) &&
251 2
            count($requestOptions[$key]) > 0;
252
    }
253
}
254