Skip to content

Commit

Permalink
Merge pull request #2307 from lascyb/master
Browse files Browse the repository at this point in the history
深层次获取username
  • Loading branch information
star7th authored Dec 23, 2024
2 parents 8c83776 + 390a00a commit 020dfae
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions server/Application/Api/Controller/ExtLoginController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function bySecretKey()
$time = I("time");
$token = I("token");
$redirect = I("redirect");
$name = I("name");
$name = I("name");

if ($time < (time() - 60)) {
$this->sendError(10101, "已过期");
Expand All @@ -25,7 +25,7 @@ public function bySecretKey()
$login_secret_key = D("Options")->get("login_secret_key");
if (!$login_secret_key) return false;
$new_token = md5($username . $login_secret_key . $time);
if (!($token === $new_token)) {
if (!($token === $new_token)) {
$this->sendError(10101, "token不正确");
return;
}
Expand All @@ -34,19 +34,19 @@ public function bySecretKey()
if (!$res) {
$new_uid = D("User")->register($username, md5("savsnyjh" . time() . rand()));
$res = D("User")->where("( username='%s' ) ", array($username))->find();
if($name){
if ($name) {
D("User")->where(" uid = '$new_uid' ")->save(array("name" => $name));
}

}
if ($res) {
// var_dump($res); return ;
if ($res['groupid'] == 1) {
$this->sendError(10101, "为了安全,禁止管理员通过这种方式登录");
return;
}
$uid = $res['uid'] ;
if($name){
$uid = $res['uid'];
if ($name) {

D("User")->where(" uid = '$uid' ")->save(array("name" => $name));
}
Expand All @@ -65,6 +65,29 @@ public function bySecretKey()
}
}

private function getUserNameFromOAuth2($array)
{
$keysToCheck = ["preferred_username", "name", "username", "login"];

foreach ($array as $key => $value) {
if (!is_array($value) && in_array($key, $keysToCheck, true)) {
return $value; // 找到匹配的键,直接返回值
}
}

foreach ($array as $value) {
if (is_array($value)) {
$username = $this->getUserNameFromOAuth2($value); // 递归检查子数组
if ($username) {
return $username; // 如果找到,返回结果
}
}
}

return false; // 如果没有找到,返回 false
}


public function oauth2()
{
$this->checkComposerPHPVersion();
Expand Down Expand Up @@ -95,11 +118,11 @@ public function oauth2()


$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => $clientId, // The client ID assigned to you by the provider
'clientSecret' => $clientSecret, // The client password assigned to you by the provider
'redirectUri' => $redirectUri,
'urlAuthorize' => $urlAuthorize,
'urlAccessToken' => $urlAccessToken,
'clientId' => $clientId, // The client ID assigned to you by the provider
'clientSecret' => $clientSecret, // The client password assigned to you by the provider
'redirectUri' => $redirectUri,
'urlAuthorize' => $urlAuthorize,
'urlAccessToken' => $urlAccessToken,
'urlResourceOwnerDetails' => $urlResourceOwnerDetails,
], [
'httpClient' => new \GuzzleHttp\Client(['verify' => false]),
Expand Down Expand Up @@ -152,24 +175,12 @@ public function oauth2()
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_HEADER, 0); //是否输出返回头信息
curl_setopt($oCurl, CURLOPT_HTTPHEADER, array("Authorization: bearer {$access_token_string}", "user-agent: showdoc","accept:application/json"));
curl_setopt($oCurl, CURLOPT_HTTPHEADER, array("Authorization: bearer {$access_token_string}", "user-agent: showdoc", "accept:application/json"));
$res = curl_exec($oCurl); //执行
curl_close($oCurl); //关闭会话
$res_array = json_decode($res, true);
if ($res_array) {
$username = '';
if ($res_array['preferred_username']) {
$username = $res_array['preferred_username'];
}
if ($res_array['name']) {
$username = $res_array['name'];
}
if ($res_array['username']) {
$username = $res_array['username'];
}
if ($res_array['login']) {
$username = $res_array['login'];
}
$username = $this->getUserNameFromOAuth2($res_array);
if (!$username) {
echo "返回信息中无法获取用户名。返回的内容如下:" . $res;
return;
Expand All @@ -178,10 +189,10 @@ public function oauth2()
if (!$info) {
D("User")->register($username, md5($username . time() . rand()));
$info = D("User")->where("username='%s'", array($username))->find();
if($res_array['name']){
D("User")->where("username='%s'", array($username))->save(array("name" =>$res_array['name']));
if ($res_array['name']) {
D("User")->where("username='%s'", array($username))->save(array("name" => $res_array['name']));
}

}

D("User")->setLastTime($info['uid']);
Expand Down

0 comments on commit 020dfae

Please sign in to comment.