1 | <?php |
||||
2 | |||||
3 | namespace tinymeng\OAuth2\Gateways; |
||||
4 | |||||
5 | use tinymeng\OAuth2\Connector\Gateway; |
||||
6 | use tinymeng\OAuth2\Exception\OAuthException; |
||||
7 | use tinymeng\OAuth2\Helper\ConstCode; |
||||
8 | use tinymeng\OAuth2\Helper\Str; |
||||
9 | |||||
10 | /** |
||||
11 | * Class Line |
||||
12 | * 开发平台: https://developers.line.biz/en/ |
||||
13 | * 接口文档: https://developers.line.biz/en/reference/line-login/#issue-access-token |
||||
14 | * @package tinymeng\OAuth2\Gateways |
||||
15 | * @Author: TinyMeng <[email protected]> |
||||
16 | * @Created: 2018/11/9 |
||||
17 | */ |
||||
18 | class Line extends Gateway |
||||
19 | { |
||||
20 | const API_BASE = 'https://api.line.me/v2/'; |
||||
21 | protected $AuthorizeURL = 'https://access.line.me/oauth2/v2.1/authorize'; |
||||
22 | protected $AccessTokenURL = 'https://api.line.me/oauth2/v2.1/token'; |
||||
23 | protected $headers = [ |
||||
24 | 'Content-Type: application/x-www-form-urlencoded' |
||||
25 | ]; |
||||
26 | |||||
27 | /** |
||||
28 | * 得到跳转地址 |
||||
29 | */ |
||||
30 | public function getRedirectUrl() |
||||
31 | { |
||||
32 | //存储state |
||||
33 | $this->saveState(); |
||||
34 | //登录参数 |
||||
35 | $params = [ |
||||
36 | 'response_type' => $this->config['response_type'], |
||||
37 | 'client_id' => $this->config['app_id'], |
||||
38 | 'redirect_uri' => $this->config['callback'], |
||||
39 | 'scope' => $this->config['scope'], |
||||
40 | 'state' => $this->config['state'] ?: Str::random(), |
||||
41 | ]; |
||||
42 | return $this->AuthorizeURL . '?' . http_build_query($params); |
||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * 获取当前授权用户的openid标识 |
||||
47 | */ |
||||
48 | public function openid() |
||||
49 | { |
||||
50 | $result = $this->getUserInfo(); |
||||
51 | return $result['userId']; |
||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * 获取格式化后的用户信息 |
||||
56 | */ |
||||
57 | public function userInfo() |
||||
58 | { |
||||
59 | $result = $this->getUserInfo(); |
||||
60 | |||||
61 | $userInfo = [ |
||||
62 | 'open_id' => $result['userId'], |
||||
63 | 'access_token'=> $this->token['access_token'] ?? '', |
||||
64 | 'union_id'=> $result['userId'], |
||||
65 | 'channel' => ConstCode::TYPE_LINE, |
||||
66 | 'nickname' => $result['displayName'], |
||||
67 | 'gender' => ConstCode::GENDER, //line不返回性别信息 |
||||
68 | 'avatar' => isset($result['pictureUrl']) ? $result['pictureUrl'] . '/large' : '', |
||||
69 | 'native' => $result, |
||||
70 | ]; |
||||
71 | return $userInfo; |
||||
72 | } |
||||
73 | |||||
74 | /** |
||||
75 | * 获取原始接口返回的用户信息 |
||||
76 | */ |
||||
77 | public function getUserInfo() |
||||
78 | { |
||||
79 | $this->getToken(); |
||||
80 | |||||
81 | $data = $this->call('profile', $this->token, 'GET'); |
||||
82 | |||||
83 | if (isset($data['error'])) { |
||||
84 | throw new OAuthException($data['error_description']); |
||||
85 | } |
||||
86 | return $data; |
||||
87 | } |
||||
88 | |||||
89 | /** |
||||
90 | * 发起请求 |
||||
91 | * |
||||
92 | * @param string $api |
||||
93 | * @param array $params |
||||
94 | * @param string $method |
||||
95 | * @return array |
||||
96 | */ |
||||
97 | private function call($api, $params = [], $method = 'GET') |
||||
98 | { |
||||
99 | $method = strtoupper($method); |
||||
100 | $request = [ |
||||
101 | 'method' => $method, |
||||
102 | 'uri' => self::API_BASE . $api, |
||||
103 | ]; |
||||
104 | |||||
105 | $headers = ['Authorization: ' . $this->token['token_type'] . ' ' . $this->token['access_token']]; |
||||
106 | |||||
107 | $data = $this->$method($request['uri'], $params, $headers); |
||||
108 | |||||
109 | return json_decode($data, true); |
||||
110 | } |
||||
111 | |||||
112 | |||||
113 | /** |
||||
114 | * Description: 获取AccessToken |
||||
115 | * @author: JiaMeng <[email protected]> |
||||
116 | * Updater: |
||||
117 | */ |
||||
118 | protected function getToken(){ |
||||
119 | if (empty($this->token)) { |
||||
120 | /** 验证state参数 */ |
||||
121 | $this->checkState(); |
||||
122 | |||||
123 | /** 获取参数 */ |
||||
124 | $params = $this->accessTokenParams(); |
||||
125 | $params = http_build_query($params); |
||||
126 | |||||
127 | /** 获取access_token */ |
||||
128 | $token = $this->post($this->AccessTokenURL, $params,$this->getHeaders()); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
129 | |||||
130 | /** 解析token值 */ |
||||
131 | $this->token = $this->parseToken($token); |
||||
0 ignored issues
–
show
It seems like
$token can also be of type true ; however, parameter $token of tinymeng\OAuth2\Gateways\Line::parseToken() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
132 | }else{ |
||||
133 | return $this->token; |
||||
134 | } |
||||
135 | } |
||||
136 | |||||
137 | /** |
||||
138 | * 解析access_token方法请求后的返回值 |
||||
139 | * @param string $token 获取access_token的方法的返回值 |
||||
140 | */ |
||||
141 | protected function parseToken($token) |
||||
142 | { |
||||
143 | $token = json_decode($token, true); |
||||
144 | if (isset($token['error'])) { |
||||
145 | throw new OAuthException($token['error_description']); |
||||
146 | } |
||||
147 | return $token; |
||||
148 | } |
||||
149 | } |
||||
150 |