Vkontakte::getAccessTokenEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
class Vkontakte extends AbstractService
14
{
15
    /**
16
     * Defined scopes.
17
     *
18
     * @see http://vk.com/dev/permissions
19
     */
20
    const SCOPE_EMAIL = 'email';
21
    const SCOPE_NOTIFY = 'notify';
22
    const SCOPE_FRIENDS = 'friends';
23
    const SCOPE_PHOTOS = 'photos';
24
    const SCOPE_AUDIO = 'audio';
25
    const SCOPE_VIDEO = 'video';
26
    const SCOPE_DOCS = 'docs';
27
    const SCOPE_NOTES = 'notes';
28
    const SCOPE_PAGES = 'pages';
29
    const SCOPE_APP_LINK = '';
30
    const SCOPE_STATUS = 'status';
31
    const SCOPE_OFFERS = 'offers';
32
    const SCOPE_QUESTIONS = 'questions';
33
    const SCOPE_WALL = 'wall';
34
    const SCOPE_GROUPS = 'groups';
35
    const SCOPE_MESSAGES = 'messages';
36
    const SCOPE_NOTIFICATIONS = 'notifications';
37
    const SCOPE_STATS = 'stats';
38
    const SCOPE_ADS = 'ads';
39
    const SCOPE_OFFLINE = 'offline';
40
    const SCOPE_NOHTTPS = 'nohttps';
41
42
    public function __construct(
43
        CredentialsInterface $credentials,
44
        ClientInterface $httpClient,
45
        TokenStorageInterface $storage,
46
        $scopes = [],
47
        ?UriInterface $baseApiUri = null
48
    ) {
49
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
50
51
        if (null === $baseApiUri) {
52
            $this->baseApiUri = new Uri('https://api.vk.com/method/');
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getAuthorizationEndpoint()
60
    {
61
        return new Uri('https://oauth.vk.com/authorize');
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getAccessTokenEndpoint()
68
    {
69
        return new Uri('https://oauth.vk.com/access_token');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function parseAccessTokenResponse($responseBody)
76
    {
77
        $data = json_decode($responseBody, true);
78
79
        if (null === $data || !is_array($data)) {
80
            throw new TokenResponseException('Unable to parse response.');
81
        } elseif (isset($data['error'])) {
82
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
83
        }
84
85
        $token = new StdOAuth2Token();
86
        $token->setAccessToken($data['access_token']);
87
        $token->setLifeTime($data['expires_in']);
88
89
        if (isset($data['refresh_token'])) {
90
            $token->setRefreshToken($data['refresh_token']);
91
            unset($data['refresh_token']);
92
        }
93
94
        unset($data['access_token'], $data['expires_in']);
95
96
        $token->setExtraParams($data);
97
98
        return $token;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    protected function getAuthorizationMethod()
105
    {
106
        return static::AUTHORIZATION_METHOD_QUERY_STRING;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function requestAccessToken($code, $state = null)
113
    {
114
        if (null !== $state) {
115
            $this->validateAuthorizationState($state);
116
        }
117
118
        $bodyParams = [
119
            'code' => $code,
120
            'client_id' => $this->credentials->getConsumerId(),
121
            'client_secret' => $this->credentials->getConsumerSecret(),
122
            'redirect_uri' => $this->credentials->getCallbackUrl(),
123
            'grant_type' => 'client_credentials',
124
125
        ];
126
127
        $responseBody = $this->httpClient->retrieveResponse(
128
            $this->getAccessTokenEndpoint(),
129
            $bodyParams,
130
            $this->getExtraOAuthHeaders()
131
        );
132
133
        $token = $this->parseAccessTokenResponse($responseBody);
134
        $this->storage->storeAccessToken($this->service(), $token);
135
136
        return $token;
137
    }
138
}
139