Completed
Push — develop ( 470834...262225 )
by Risan Bagja
03:34
created

ProtocolParameter::forProtectedResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 4
crap 1
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\ConfigInterface $config
39
     * @param \Risan\OAuth1\Signature\SignerInterface $signer
40
     * @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator
41
     */
42 19
    public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator)
43
    {
44 19
        $this->config = $config;
45 19
        $this->signer = $signer;
46 19
        $this->nonceGenerator = $nonceGenerator;
47 19
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function getConfig()
53
    {
54 1
        return $this->config;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 1
    public function getSigner()
61
    {
62 1
        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 1
    public function forTokenCredentials(TemporaryCredentials $temporaryCredentials, $verificationCode)
123
    {
124 1
        $parameters = $this->getBase();
125
126 1
        $parameters['oauth_token'] = $temporaryCredentials->getIdentifier();
127
128
        $requestOptions = [
129 1
            'form_params' => ['oauth_verifier' => $verificationCode],
130
        ];
131
132 1
        $parameters['oauth_signature'] = $this->getSignature(
133 1
            $parameters,
134 1
            $this->config->getTokenCredentialsUri(),
135 1
            $temporaryCredentials,
136 1
            $requestOptions
137
        );
138
139 1
        return $parameters;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145 1
    public function forProtectedResource(TokenCredentials $tokenCredentials, $httpMethod, $uri, array $requestOptions = [])
146
    {
147 1
        $parameters = $this->getBase();
148
149 1
        $parameters['oauth_token'] = $tokenCredentials->getIdentifier();
150
151 1
        $parameters['oauth_signature'] = $this->getSignature(
152 1
            $parameters,
153 1
            $this->config->buildUri($uri),
154 1
            $tokenCredentials,
155 1
            $requestOptions,
156 1
            $httpMethod
157
        );
158
159 1
        return $parameters;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 1
    public function getSignature(array $protocolParameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST')
166
    {
167 1
        $signatureParameters = $this->signatureParameters($protocolParameters, $requestOptions);
168
169 1
        return $this->setupSigner($serverIssuedCredentials)
170 1
            ->sign($uri, $signatureParameters, $httpMethod);
171
    }
172
173
    /**
174
     * Build the signature parameters to be signed.
175
     *
176
     * @param  array  $protocolParameters
177
     * @param  array  $requestOptions
178
     * @return array
179
     */
180 1
    public function signatureParameters(array $protocolParameters, array $requestOptions = [])
181
    {
182 1
        $parameters = $protocolParameters;
183
184 1
        if ($this->requestOptionsHas($requestOptions, 'query')) {
185 1
            $parameters = array_merge($parameters, $requestOptions['query']);
186
        }
187
188 1
        if ($this->requestOptionsHas($requestOptions, 'form_params')) {
189 1
            $parameters = array_merge($parameters, $requestOptions['form_params']);
190
        }
191
192 1
        return $parameters;
193
    }
194
195
    /**
196
     * Setup the signer.
197
     *
198
     * @param  \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials
199
     * @return \Risan\OAuth1\Signature\SignerInterface
200
     */
201 1
    public function setupSigner(ServerIssuedCredentials $serverIssuedCredentials = null)
202
    {
203 1
        if ($this->shouldSignWithClientCredentials()) {
204 1
            $this->signer->setClientCredentials($this->config->getClientCredentials());
205
        }
206
207 1
        if ($this->shouldSignWithServerIssuedCredentials($serverIssuedCredentials)) {
208 1
            $this->signer->setServerIssuedCredentials($serverIssuedCredentials);
209
        }
210
211 1
        return $this->signer;
212
    }
213
214
    /**
215
     * Should sign with the client credentials.
216
     *
217
     * @return boolean
218
     */
219 2
    public function shouldSignWithClientCredentials()
220
    {
221 2
        return $this->signer->isKeyBased();
222
    }
223
224
    /**
225
     * Should sign with the server issued credentials.
226
     *
227
     * @param  \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials
228
     * @return boolean
229
     */
230 2
    public function shouldSignWithServerIssuedCredentials(ServerIssuedCredentials $serverIssuedCredentials = null)
231
    {
232 2
        return $this->signer->isKeyBased() && $serverIssuedCredentials !== null;
233
    }
234
235
    /**
236
     * Check if request options has the given key option.
237
     *
238
     * @param  array  $requestOptions
239
     * @param  string $key
240
     * @return boolean
241
     */
242 2
    public function requestOptionsHas(array $requestOptions, $key)
243
    {
244 2
        return isset($requestOptions[$key]) &&
245 2
            is_array($requestOptions[$key]) &&
246 2
            count($requestOptions[$key]) > 0;
247
    }
248
}
249