Completed
Push — master ( bbbbeb...30b88d )
by Дмитрий
03:09
created

Provider::getRequestTokenUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Odnoklassniki;
8
9
use SocialConnect\Auth\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Exception\InvalidResponse;
11
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Http\Client\Client;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
/**
17
 * @link https://apiok.ru/dev
18
 */
19
class Provider extends \SocialConnect\Auth\Provider\OAuth2\AbstractProvider
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getBaseUri()
25
    {
26
        return 'https://api.ok.ru/api/';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getAuthorizeUri()
33
    {
34
        return 'http://connect.ok.ru/oauth/authorize';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getRequestTokenUri()
41
    {
42
        return 'http://api.odnoklassniki.ru/oauth/token.do';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getName()
49
    {
50
        return 'odnoklassniki';
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function parseToken($body)
57
    {
58
        if (empty($body)) {
59
            throw new InvalidAccessToken('Provider response with empty body');
60
        }
61
62
        $result = json_decode($body);
63
        if ($result) {
64
            if (isset($result->access_token)) {
65
                return new AccessToken($result->access_token);
66
            }
67
68
            throw new InvalidAccessToken('Provider API returned without access_token field inside JSON');
69
        }
70
71
        throw new InvalidAccessToken('Provider response with not valid JSON');
72
    }
73
74
    /**
75
     * @link https://apiok.ru/dev/methods/
76
     *
77
     * @param array $requestParameters
78
     * @return string
79
     */
80
    protected function makeSecureSignature(array $requestParameters, AccessToken $accessToken)
81
    {
82
        ksort($requestParameters);
83
84
        $params = '';
85
86
        foreach ($requestParameters as $key => $value) {
87
            if ($key === 'access_token') {
88
                continue;
89
            }
90
91
            $params .= "$key=$value";
92
        }
93
94
        return strtolower(md5($params . md5($accessToken->getToken() . $this->consumer->getSecret())));
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getIdentity(AccessToken $accessToken)
101
    {
102
        $parameters = [
103
            'application_key' => $this->consumer->getPublic(),
104
            'access_token' => $accessToken->getToken(),
105
            'format' => 'json'
106
        ];
107
108
        $parameters['sig'] = $this->makeSecureSignature($parameters, $accessToken);
109
110
        $response = $this->service->getHttpClient()->request(
111
            $this->getBaseUri() . 'users/getCurrentUser',
112
            $parameters
113
        );
114
115
        $result = $response->json();
116
        if (!$result) {
117
            throw new InvalidResponse(
118
                'API response is not a valid JSON object',
119
                $response->getBody()
120
            );
121
        }
122
123
        $hydrator = new ObjectMap(array(
124
            'uid' => 'id',
125
            'first_name' => 'firstname',
126
            'last_name' => 'lastname',
127
            'name' => 'fullname'
128
        ));
129
130
        return $hydrator->hydrate(new User(), $result);
131
    }
132
}
133