-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.php
138 lines (119 loc) · 3.42 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace SocialiteProvidersMini\WeixinMini;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
/**
* Unique Provider Identifier.
*/
public const IDENTIFIER = 'WEIXIN_MINI';
/**
* @var string
*/
protected $openId;
/**
* {@inheritdoc}.
*/
protected $scopes = ['snsapi_userinfo'];
/**
* set Open Id.
*
* @param string $openId
*/
public function setOpenId($openId)
{
$this->openId = $openId;
}
/**
* {@inheritdoc}.
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/oauth2/authorize', $state);
}
/**
* {@inheritdoc}.
*/
protected function buildAuthUrlFromBase($url, $state)
{
$query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType);
return $url.'?'.$query.'#wechat_redirect';
}
/**
* {@inheritdoc}.
*/
protected function getCodeFields($state = null)
{
return [
'appid' => $this->clientId, 'redirect_uri' => $this->redirectUrl,
'response_type' => 'code',
'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
'state' => $state,
];
}
/**
* {@inheritdoc}.
*/
protected function getTokenUrl()
{
return 'https://api.weixin.qq.com/sns/jscode2session';
}
/**
* {@inheritdoc}.
*/
protected function getUserByToken($token)
{
if (in_array('snsapi_base', $this->scopes, true)) {
$user = ['openid' => $this->openId];
} else {
$response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [
'query' => [
'access_token' => $token,
'openid' => $this->openId,
'lang' => 'zh_CN',
],
]);
$user = json_decode($response->getBody(), true);
}
return $user;
}
/**
* {@inheritdoc}.
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['openid'],
'unionid' => isset($user['unionid']) ? $user['unionid'] : null,
'nickname' => isset($user['nickname']) ? $user['nickname'] : null,
'avatar' => isset($user['headimgurl']) ? $user['headimgurl'] : null,
'name' => null,
'email' => null,
]);
}
/**
* {@inheritdoc}.
*/
protected function getTokenFields($code)
{
return [
'appid' => $this->clientId, 'secret' => $this->clientSecret,
'js_code' => $code, 'grant_type' => 'authorization_code',
];
}
/**
* {@inheritdoc}.
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->get($this->getTokenUrl(), [
'query' => $this->getTokenFields($code),
]);
$this->credentialsResponseBody = json_decode($response->getBody(), true);
if (isset($this->credentialsResponseBody['openid'])) {
$this->openId = $this->credentialsResponseBody['openid'];
}
return $this->credentialsResponseBody;
}
}