This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Wechat\API; |
||
3 | |||
4 | use Wechat\Utils\Url; |
||
5 | use Wechat\Api; |
||
6 | |||
7 | /** |
||
8 | * 微信Auth相关接口. |
||
9 | * |
||
10 | * @author Tian. |
||
11 | */ |
||
12 | class AuthApi extends BaseApi |
||
13 | { |
||
14 | const API_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize'; |
||
15 | |||
16 | protected static $authorizedUser; |
||
17 | |||
18 | /** |
||
19 | * 生成outh URL |
||
20 | * |
||
21 | * @param string $to |
||
22 | * @param string $scope |
||
23 | * @param string $state |
||
24 | * |
||
25 | * @return string |
||
26 | */ |
||
27 | public function url($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
||
28 | { |
||
29 | $to !== null || $to = Url::current(); |
||
30 | |||
31 | $queryStr = [ |
||
32 | 'appid' => $this->getAppId(), |
||
33 | 'redirect_uri' => $to, |
||
34 | 'response_type' => 'code', |
||
35 | 'scope' => $scope, |
||
36 | 'state' => $state, |
||
37 | ]; |
||
38 | |||
39 | return self::API_URL . '?' . http_build_query($queryStr) . '#wechat_redirect'; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * 直接跳转 |
||
44 | * |
||
45 | * @param string $to |
||
46 | * @param string $scope |
||
47 | * @param string $state |
||
48 | */ |
||
49 | public function redirect($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
||
50 | { |
||
51 | header('Location:' . $this->url($to, $scope, $state)); |
||
52 | |||
53 | exit; |
||
0 ignored issues
–
show
|
|||
54 | } |
||
55 | |||
56 | /** |
||
57 | * 获取用户信息 |
||
58 | * |
||
59 | * @param string $openId |
||
60 | * @param string $accessToken |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | View Code Duplication | public function getUser($openId, $accessToken) |
|
0 ignored issues
–
show
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. ![]() |
|||
65 | { |
||
66 | $queryStr = [ |
||
67 | 'access_token' => $accessToken, |
||
68 | 'openid' => $openId, |
||
69 | 'lang' => 'zh_CN', |
||
70 | ]; |
||
71 | |||
72 | $this->apitype = 'sns'; |
||
73 | $this->module = 'userinfo'; |
||
74 | $res = $this->_get('', $queryStr); |
||
75 | |||
76 | return $res; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * 获取已授权用户 |
||
81 | * |
||
82 | * @return array $user |
||
83 | */ |
||
84 | public function user() |
||
0 ignored issues
–
show
user uses the super-global variable $_GET which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
85 | { |
||
86 | if (self::$authorizedUser || !$_GET['state'] || (!$code = $_GET['code']) && $_GET['state']) { |
||
87 | return self::$authorizedUser; |
||
88 | } |
||
89 | |||
90 | $permission = $this->getAccessPermission($code); |
||
91 | |||
92 | if ($permission['scope'] !== 'snsapi_userinfo') { |
||
93 | $user = ['openid' => $permission['openid']]; |
||
94 | } else { |
||
95 | $user = $this->getUser($permission['openid'], $permission['access_token']); |
||
96 | } |
||
97 | |||
98 | return $this->authorizedUser = $user; |
||
0 ignored issues
–
show
|
|||
99 | } |
||
100 | |||
101 | /** |
||
102 | * 通过授权获取用户 |
||
103 | * |
||
104 | * @param null $to |
||
105 | * @param string $scope |
||
106 | * @param string $state |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
||
0 ignored issues
–
show
authorize uses the super-global variable $_GET which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
111 | { |
||
112 | if (!$_GET['state'] && !$code = $_GET['code']) { |
||
113 | $this->redirect($to, $scope, $state); |
||
114 | } |
||
115 | |||
116 | return $this->user(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * 检查 Access Token 是否有效 |
||
121 | * |
||
122 | * @param string $accessToken |
||
123 | * @param string $openId |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | View Code Duplication | public function accessTokenIsValid($accessToken, $openId) |
|
0 ignored issues
–
show
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. ![]() |
|||
128 | { |
||
129 | $params = [ |
||
130 | 'openid' => $openId, |
||
131 | 'access_token' => $accessToken, |
||
132 | ]; |
||
133 | |||
134 | $this->apitype = 'sns'; |
||
135 | $this->module = 'auth'; |
||
136 | |||
137 | $res = $this->_get('', $params); |
||
138 | |||
139 | return $res; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * 刷新 access_token |
||
144 | * |
||
145 | * @param $refreshToken |
||
146 | * |
||
147 | * @return bool|array |
||
148 | */ |
||
149 | View Code Duplication | public function refresh($refreshToken) |
|
0 ignored issues
–
show
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. ![]() |
|||
150 | { |
||
151 | $queryStr = [ |
||
152 | 'appid' => $this->getAppId(), |
||
153 | 'grant_type' => 'refresh_token', |
||
154 | 'refresh_token' => $refreshToken, |
||
155 | ]; |
||
156 | |||
157 | $this->apitype = 'sns'; |
||
158 | $this->module = 'oauth2'; |
||
159 | $res = $this->_get('refresh_token', $queryStr); |
||
160 | |||
161 | return $res; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * 获取access token |
||
166 | * |
||
167 | * @param string $code |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | View Code Duplication | public function getAccessPermission($code) |
|
0 ignored issues
–
show
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. ![]() |
|||
172 | { |
||
173 | $queryStr = [ |
||
174 | 'appid' => $this->getAppId(), |
||
175 | 'secret' => $this->getAppSecret(), |
||
176 | 'code' => $code, |
||
177 | 'grant_type' => 'authorization_code', |
||
178 | ]; |
||
179 | |||
180 | $this->apitype = 'sns'; |
||
181 | $this->module = 'oauth2'; |
||
182 | $res = $this->_get('access_token', $queryStr); |
||
183 | |||
184 | return $res; |
||
185 | } |
||
186 | } |
||
187 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.