Completed
Push — master ( dd0a26...902192 )
by Risan Bagja
02:42
created

AccessTokenFlow   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 1
cbo 4
dl 0
loc 105
ccs 18
cts 18
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A accessTokenUrl() 0 4 1
A isValidToken() 0 4 1
A accessTokenHeaders() 0 12 1
httpClient() 0 1 ?
config() 0 1 ?
signer() 0 1 ?
baseProtocolParameters() 0 1 ?
authorizationHeaders() 0 1 ?
A accessToken() 0 12 2
1
<?php
2
3
namespace OAuth1\Flows;
4
5
use InvalidArgumentException;
6
use OAuth1\Contracts\Tokens\RequestTokenInterface;
7
use OAuth1\Tokens\AccessToken;
8
9
trait AccessTokenFlow
10
{
11
    /**
12
     * Access token url.
13
     *
14
     * @return string
15
     */
16 3
    public function accessTokenUrl()
17
    {
18 3
        return $this->config()->accessTokenUrl();
19
    }
20
21
    /**
22
     * Get access token.
23
     *
24
     * @param \OAuth1\Contracts\Tokens\RequestTokenInterface $requestToken
25
     * @param string                                         $tokenKey
26
     * @param string                                         $verifier
27
     *
28
     * @return \OAuth1\Contracts\Tokens\AccessTokenInterface
29
     */
30 2
    public function accessToken(RequestTokenInterface $requestToken, $tokenKey, $verifier)
31
    {
32 2
        if (!$this->isValidToken($requestToken, $tokenKey)) {
33 1
            throw new InvalidArgumentException('The received oauth token does not match.');
34
        }
35
36 1
        $response = $this->httpClient()->post($this->accessTokenUrl(), [
37 1
            'headers' => $this->accessTokenHeaders($requestToken, $verifier),
38 1
        ]);
39
40 1
        return AccessToken::fromHttpResponse($response);
41
    }
42
43
    /**
44
     * Is valid token?
45
     *
46
     * @param \OAuth1\Contracts\Token\RequestTokenInterface $requestToken
47
     * @param string                                        $tokenKey
48
     *
49
     * @return bool
50
     */
51 3
    public function isValidToken(RequestTokenInterface $requestToken, $tokenKey)
52
    {
53 3
        return $requestToken->key() === $tokenKey;
54
    }
55
56
    /**
57
     * Access token header.
58
     *
59
     * @param \OAuth1\Contracts\Token\RequestTokenInterface $requestToken
60
     * @param string                                        $verifier
61
     *
62
     * @return array
63
     */
64 2
    public function accessTokenHeaders(RequestTokenInterface $requestToken, $verifier)
65
    {
66 2
        $parameters = $this->baseProtocolParameters();
67
68 2
        $parameters['oauth_token'] = $requestToken->key();
69 2
        $parameters['oauth_verifier'] = $verifier;
70 2
        $parameters['oauth_signature'] = $this->signer()->setTokenSecret($requestToken->secret())->sign($this->accessTokenUrl(), $parameters);
71
72
        return [
73 2
            'Authorization' => $this->authorizationHeaders($parameters),
74 2
        ];
75
    }
76
77
    /**
78
     * Get http client instance.
79
     *
80
     * @return \OAuth1\Contracts\HttpClientInterface
81
     */
82
    abstract public function httpClient();
83
84
    /**
85
     * Get client configuration.
86
     *
87
     * @return \OAuth1\Contracts\ConfigInterface
88
     */
89
    abstract public function config();
90
91
    /**
92
     * Get signer.
93
     *
94
     * @return \OAuth1\Contracts\Signers\SignerInterface
95
     */
96
    abstract public function signer();
97
98
    /**
99
     * Get OAuth base protocol parameters.
100
     *
101
     * @return array
102
     */
103
    abstract public function baseProtocolParameters();
104
105
    /**
106
     * Build authorization headers.
107
     *
108
     * @param array $parameters
109
     *
110
     * @return string
111
     */
112
    abstract public function authorizationHeaders(array $parameters);
113
}
114