Completed
Push — master ( 8b22ae...a4fa8f )
by Carlos
01:39
created

TaobaoProvider::mapUserToObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Socialite\Providers;
13
14
use Overtrue\Socialite\AccessTokenInterface;
15
use Overtrue\Socialite\ProviderInterface;
16
use Overtrue\Socialite\User;
17
18
/**
19
 * Class TaobaoProvider
20
 *
21
 * @author mechono <[email protected]>
22
 *
23
 * @see    https://open.taobao.com/doc.htm?docId=102635&docType=1&source=search [Taobao - OAuth 2.0 授权登录]
24
 */
25
class TaobaoProvider extends AbstractProvider implements ProviderInterface
26
{
27
    /**
28
     * The base url of Taobao API.
29
     *
30
     * @var string
31
     */
32
    protected $baseUrl = 'https://oauth.taobao.com';
33
34
    /**
35
     * Taobao API service URL address
36
     *
37
     * @var string
38
     */
39
    protected $gatewayUrl = 'https://eco.taobao.com/router/rest';
40
41
    /**
42
     * The API version for the request.
43
     *
44
     * @var string
45
     */
46
    protected $version = '2.0';
47
48
    /**
49
     * @var string
50
     */
51
    protected $format = 'json';
52
53
    /**
54
     * @var string
55
     */
56
    protected $signMethod = 'md5';
57
58
    /**
59
     * Web 对应 PC 端(淘宝 logo )浏览器页面样式;Tmall 对应天猫的浏览器页面样式;Wap 对应无线端的浏览器页面样式。
60
     */
61
    protected $view = 'web';
62
63
    /**
64
     * The scopes being requested.
65
     *
66
     * @var array
67
     */
68
    protected $scopes = ['user_info'];
69
70
    /**
71
     * Get the authentication URL for the provider.
72
     *
73
     * @param string $state
74
     *
75
     * @return string
76
     */
77
    protected function getAuthUrl($state)
78
    {
79
        return $this->buildAuthUrlFromBase($this->baseUrl . '/authorize', $state);
80
    }
81
82
    /**
83
     * 获取授权码接口参数.
84
     *
85
     * @param string|null $state
86
     *
87
     * @return array
88
     */
89
    public function getCodeFields($state = null)
90
    {
91
        $fields = [
92
            'client_id' => $this->clientId,
93
            'redirect_uri' => $this->redirectUrl,
94
            'view' => $this->view,
95
            'response_type' => 'code',
96
        ];
97
98
        if ($this->usesState()) {
99
            $fields['state'] = $state;
100
        }
101
102
        return $fields;
103
    }
104
105
    /**
106
     * Get the token URL for the provider.
107
     *
108
     * @return string
109
     */
110
    protected function getTokenUrl()
111
    {
112
        return $this->baseUrl . '/token';
113
    }
114
115
    /**
116
     * Get the Post fields for the token request.
117
     *
118
     * @param string $code
119
     *
120
     * @return array
121
     */
122
    protected function getTokenFields($code)
123
    {
124
        return parent::getTokenFields($code) + ['grant_type' => 'authorization_code', 'view' => $this->view];
125
    }
126
127
    /**
128
     * Get the access token for the given code.
129
     *
130
     * @param string $code
131
     *
132
     * @return \Overtrue\Socialite\AccessToken
133
     */
134 View Code Duplication
    public function getAccessToken($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
137
            'query' => $this->getTokenFields($code),
138
        ]);
139
140
        return $this->parseAccessToken($response->getBody()->getContents());
141
    }
142
143
    /**
144
     * Get the access token from the token response body.
145
     *
146
     * @param string $body
147
     *
148
     * @return \Overtrue\Socialite\AccessToken
149
     */
150
    public function parseAccessToken($body)
151
    {
152
        return parent::parseAccessToken($body);
0 ignored issues
show
Documentation introduced by
$body is of type string, but the function expects a object<Psr\Http\Message\StreamInterface>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
    }
154
155
    /**
156
     * Get the raw user for the given access token.
157
     *
158
     * @param \Overtrue\Socialite\AccessTokenInterface $token
159
     *
160
     * @return array
161
     */
162
    protected function getUserByToken(AccessTokenInterface $token)
163
    {
164
        $response = $this->getHttpClient()->post($this->getUserInfoUrl($this->gatewayUrl, $token));
165
166
        return json_decode($response->getBody(), true);
167
    }
168
169
    /**
170
     * Map the raw user array to a Socialite User instance.
171
     *
172
     * @param array $user
173
     *
174
     * @return \Overtrue\Socialite\User
175
     */
176
    protected function mapUserToObject(array $user)
177
    {
178
        return new User([
179
            'id' => $this->arrayItem($user, 'open_id'),
180
            'nickname' => $this->arrayItem($user, 'nick'),
181
            'name' => $this->arrayItem($user, 'nick'),
182
            'avatar' => $this->arrayItem($user, 'avatar'),
183
        ]);
184
    }
185
186
    /**
187
     * @param $params
188
     *
189
     * @return string
190
     */
191
    protected function generateSign($params)
192
    {
193
        ksort($params);
194
195
        $stringToBeSigned = $this->clientSecret;
196
197
        foreach ($params as $k => $v) {
198
            if (!is_array($v) && '@' != substr($v, 0, 1)) {
199
                $stringToBeSigned .= "$k$v";
200
            }
201
        }
202
203
        $stringToBeSigned .= $this->clientSecret;
204
205
        return strtoupper(md5($stringToBeSigned));
206
    }
207
208
    /**
209
     * @param \Overtrue\Socialite\AccessTokenInterface $token
210
     * @param array                                    $apiFields
211
     *
212
     * @return array
213
     */
214
    protected function getPublicFields(AccessTokenInterface $token, array $apiFields = [])
215
    {
216
        $fields = [
217
            'app_key' => $this->clientId,
218
            'sign_method' => $this->signMethod,
219
            'session' => $token->getToken(),
220
            'timestamp' => date('Y-m-d H:i:s'),
221
            'v' => $this->version,
222
            'format' => $this->format,
223
        ];
224
225
        $fields = array_merge($apiFields, $fields);
226
        $fields['sign'] = $this->generateSign($fields);
227
228
        return $fields;
229
    }
230
231
    /**
232
     * {@inheritdoc}.
233
     */
234
    protected function getUserInfoUrl($url, AccessTokenInterface $token)
235
    {
236
        $apiFields = ['method' => 'taobao.miniapp.userInfo.get'];
237
238
        $query = http_build_query($this->getPublicFields($token, $apiFields), '', '&', $this->encodingType);
239
240
        return $url.'?'.$query;
241
    }
242
}
243