fix: 使用飞书官方 passport 授权端点
后端修改: - 授权 URL 从 open-apis 改为 passport.feishu.cn - 使用 passport 端点获取 user_access_token - 添加 httpGet 方法 前端修改: - 使用官方 tt.requestAuthCode() API(飞书内免登) - 保留轮询方式实现扫码登录 参考文档: https://open.feishu.cn/document/sso/web-application-sso/qr-sdk-documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -140,10 +140,10 @@ class AuthController extends BaseController
|
|||||||
'expires_at' => date('Y-m-d H:i:s', time() + 300), // 5分钟过期
|
'expires_at' => date('Y-m-d H:i:s', time() + 300), // 5分钟过期
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 构建扫码登录 URL
|
// 构建扫码登录 URL(使用官方 passport 端点)
|
||||||
$appId = 'cli_a8033d607cba500d';
|
$appId = 'cli_a8033d607cba500d';
|
||||||
$redirectUri = urlencode("https://xsm.dev.hnxinyouhe.com/login/qrcode/{$loginSession->id}");
|
$redirectUri = urlencode("https://xsm.dev.hnxinyouhe.com/login/qrcode/{$loginSession->id}");
|
||||||
$authUrl = "https://open.feishu.cn/open-apis/authen/v1/oidc/authorize?app_id={$appId}&redirect_uri={$redirectUri}&state={$loginSession->id}";
|
$authUrl = "https://passport.feishu.cn/suite/passport/oauth/authorize?client_id={$appId}&redirect_uri={$redirectUri}&response_type=code&state={$loginSession->id}";
|
||||||
|
|
||||||
// 生成二维码内容(返回 URL,前端生成二维码)
|
// 生成二维码内容(返回 URL,前端生成二维码)
|
||||||
return $this->success([
|
return $this->success([
|
||||||
|
|||||||
@@ -134,28 +134,36 @@ class FeishuService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 auth_code 获取用户信息
|
* 根据 auth_code 获取用户信息(扫码登录)
|
||||||
*/
|
*/
|
||||||
public function getUserInfo(string $authCode): array
|
public function getUserInfo(string $authCode): array
|
||||||
{
|
{
|
||||||
$app = $this->getAppConfig();
|
$app = $this->getAppConfig();
|
||||||
$token = $this->getAccessToken();
|
|
||||||
|
|
||||||
// 获取用户 access_token
|
// 使用 passport 端点获取 user_access_token
|
||||||
$response = $this->httpPost('https://open.feishu.cn/open-apis/authen/v1/oidc/access_token', [
|
$response = $this->httpPost('https://passport.feishu.cn/suite/passport/oauth/token', [
|
||||||
'grant_type' => 'authorization_code',
|
'grant_type' => 'authorization_code',
|
||||||
'client_id' => $app['app_id'],
|
'client_id' => $app['app_id'],
|
||||||
'client_secret' => $app['app_secret'],
|
'client_secret' => $app['app_secret'],
|
||||||
'code' => $authCode,
|
'code' => $authCode,
|
||||||
], [
|
|
||||||
'Authorization: Bearer ' . $token,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($response['code'] !== 0) {
|
if (!isset($response['access_token'])) {
|
||||||
throw new \Exception('获取用户信息失败: ' . ($response['msg'] ?? '未知错误'));
|
throw new \Exception('获取 user_access_token 失败: ' . ($response['error_description'] ?? '未知错误'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response['data'] ?? [];
|
$userAccessToken = $response['access_token'];
|
||||||
|
|
||||||
|
// 使用 user_access_token 获取用户信息
|
||||||
|
$userInfo = $this->httpGet('https://passport.feishu.cn/suite/passport/oauth/userinfo', [
|
||||||
|
'access_token' => $userAccessToken,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!isset($userInfo['data'])) {
|
||||||
|
throw new \Exception('获取用户信息失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $userInfo['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -306,4 +314,36 @@ class FeishuService
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP GET 请求
|
||||||
|
*/
|
||||||
|
private function httpGet(string $url, array $params = []): array
|
||||||
|
{
|
||||||
|
if (!empty($params)) {
|
||||||
|
$url .= '?' . http_build_query($params);
|
||||||
|
}
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
if ($httpCode !== 200) {
|
||||||
|
throw new \Exception("HTTP 请求失败: {$httpCode}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = json_decode($response, true);
|
||||||
|
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new \Exception('JSON 解析失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,6 +175,10 @@ export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
<p className={styles.tip}>
|
||||||
|
💡 打开飞书 APP,扫一扫登录
|
||||||
|
</p>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user