Delicious::getAuthorizationEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Delicious service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
7
 *
8
 * @see    https://github.com/SciDevs/delicious-api/blob/master/api/oauth.md
9
 */
10
11
namespace OAuth\OAuth2\Service;
12
13
use OAuth\Common\Consumer\CredentialsInterface;
14
use OAuth\Common\Http\Client\ClientInterface;
15
use OAuth\Common\Http\Exception\TokenResponseException;
16
use OAuth\Common\Http\Uri\Uri;
17
use OAuth\Common\Http\Uri\UriInterface;
18
use OAuth\Common\Storage\TokenStorageInterface;
19
use OAuth\OAuth2\Token\StdOAuth2Token;
20
21
/**
22
 * Delicious service.
23
 *
24
 * @author  Pedro Amorim <[email protected]>
25
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
26
 *
27
 * @see    https://github.com/SciDevs/delicious-api/blob/master/api/oauth.md
28
 */
29
class Delicious extends AbstractService
30
{
31
    public function __construct(
32
        CredentialsInterface $credentials,
33
        ClientInterface $httpClient,
34
        TokenStorageInterface $storage,
35
        $scopes = [],
36
        ?UriInterface $baseApiUri = null
37
    ) {
38
        parent::__construct(
39
            $credentials,
40
            $httpClient,
41
            $storage,
42
            $scopes,
43
            $baseApiUri,
44
            true
45
        );
46
47
        if (null === $baseApiUri) {
48
            $this->baseApiUri = new Uri('https://api.del.icio.us/v1/');
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAuthorizationEndpoint()
56
    {
57
        return new Uri('https://delicious.com/auth/authorize');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAccessTokenEndpoint()
64
    {
65
        return new Uri('https://avosapi.delicious.com/api/v1/oauth/token');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getAuthorizationMethod()
72
    {
73
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function parseAccessTokenResponse($responseBody)
80
    {
81
        $data = json_decode($responseBody, true);
82
83
        if (null === $data || !is_array($data)) {
84
            throw new TokenResponseException('Unable to parse response.');
85
        } elseif (isset($data['error'])) {
86
            throw new TokenResponseException(
87
                'Error in retrieving token: "' . $data['error'] . '"'
88
            );
89
        }
90
91
        $token = new StdOAuth2Token();
92
        $token->setAccessToken($data['access_token']);
93
94
        if (isset($data['expires_in'])) {
95
            $token->setLifetime($data['expires_in']);
96
            unset($data['expires_in']);
97
        }
98
        if (isset($data['refresh_token'])) {
99
            $token->setRefreshToken($data['refresh_token']);
100
            unset($data['refresh_token']);
101
        }
102
103
        unset($data['access_token']);
104
105
        $token->setExtraParams($data);
106
107
        return $token;
108
    }
109
110
    // Special, delicious didn't respect the oauth2 RFC and need a grant_type='code'
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function requestAccessToken($code, $state = null)
116
    {
117
        if (null !== $state) {
118
            $this->validateAuthorizationState($state);
119
        }
120
121
        $bodyParams = [
122
            'code' => $code,
123
            'client_id' => $this->credentials->getConsumerId(),
124
            'client_secret' => $this->credentials->getConsumerSecret(),
125
            'redirect_uri' => $this->credentials->getCallbackUrl(),
126
            'grant_type' => 'code',
127
        ];
128
129
        $responseBody = $this->httpClient->retrieveResponse(
130
            $this->getAccessTokenEndpoint(),
131
            $bodyParams,
132
            $this->getExtraOAuthHeaders()
133
        );
134
135
        $token = $this->parseAccessTokenResponse($responseBody);
136
        $this->storage->storeAccessToken($this->service(), $token);
137
138
        return $token;
139
    }
140
}
141