1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the mingyoung/dingtalk. |
5
|
|
|
* |
6
|
|
|
* (c) 张铭阳 <[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 EasyDingTalk\Auth; |
13
|
|
|
|
14
|
|
|
use EasyDingTalk\Kernel\Http\Client; |
15
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
16
|
|
|
|
17
|
|
|
class OAuthClient extends Client |
18
|
|
|
{ |
19
|
|
|
use HasStateParameter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $credential; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $withQrConnect = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
protected $redirect; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $name |
38
|
|
|
* |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function use($name) |
42
|
|
|
{ |
43
|
|
|
$this->credential = $this->app['config']->get('oauth')[$name]; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getRedirectUrl() |
52
|
|
|
{ |
53
|
|
|
return $this->redirect ?: $this->credential['redirect']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $url |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setRedirectUrl($url) |
62
|
|
|
{ |
63
|
|
|
$this->redirect = $url; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function withQrConnect() |
72
|
|
|
{ |
73
|
|
|
$this->withQrConnect = true; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Redirect to the authentication page. |
80
|
|
|
* |
81
|
|
|
* @param string|null $url |
82
|
|
|
* |
83
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
84
|
|
|
*/ |
85
|
|
|
public function redirect($url = null) |
86
|
|
|
{ |
87
|
|
|
$query = [ |
88
|
|
|
'appid' => $this->credential['client_id'], |
89
|
|
|
'response_type' => 'code', |
90
|
|
|
'scope' => $this->credential['scope'], |
91
|
|
|
'state' => $this->makeState(), |
92
|
|
|
'redirect_uri' => $url ?: $this->getRedirectUrl(), |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
return new RedirectResponse( |
96
|
|
|
sprintf('https://oapi.dingtalk.com/connect/%s?%s', $this->withQrConnect ? 'qrconnect' : 'oauth2/sns_authorize', http_build_query(array_filter($query))) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
* |
103
|
|
|
* @throws \EasyDingTalk\Auth\InvalidStateException |
104
|
|
|
*/ |
105
|
|
|
public function user() |
106
|
|
|
{ |
107
|
|
|
if (!$this->hasValidState($this->app['request']->get('state'))) { |
108
|
|
|
throw new InvalidStateException(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$data = [ |
112
|
|
|
'tmp_auth_code' => $this->app['request']->get('code'), |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
$query = [ |
116
|
|
|
'accessKey' => $this->credential['client_id'], |
117
|
|
|
'timestamp' => $timestamp = (int) microtime(true) * 1000, |
118
|
|
|
'signature' => $this->signature($timestamp), |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
return $this->postJson('sns/getuserinfo_bycode', $data, $query); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* 计算签名 |
126
|
|
|
* |
127
|
|
|
* @param int $timestamp |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function signature($timestamp) |
132
|
|
|
{ |
133
|
|
|
return base64_encode(hash_hmac('sha256', $timestamp, $this->credential['client_secret'], true)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|