Passed
Push — discord-provider ( 0e4f76 )
by AD
03:05
created

MailRu::getAuthorizeUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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