Completed
Push — master ( 0095fb...15ea44 )
by Дмитрий
04:21
created

MailRu::makeSecureSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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