114 行
3.0 KiB
PHP
114 行
3.0 KiB
PHP
<?php
|
|
/**
|
|
* GitHub登录回调处理文件
|
|
* 处理GitHub OAuth授权回调,获取用户信息并重定向到绑定页面
|
|
*/
|
|
|
|
session_start();
|
|
|
|
// 加载配置文件
|
|
$config = require_once __DIR__ . '/../config.php';
|
|
$githubConfig = $config['github'];
|
|
|
|
// 检查必要参数
|
|
if (!isset($_GET['code'])) {
|
|
die('授权失败:缺少code参数');
|
|
}
|
|
|
|
$code = $_GET['code'];
|
|
|
|
// 1. 通过code获取access_token
|
|
$tokenUrl = 'https://github.com/login/oauth/access_token';
|
|
$tokenParams = [
|
|
'client_id' => $githubConfig['client_id'],
|
|
'client_secret' => $githubConfig['client_secret'],
|
|
'code' => $code,
|
|
'redirect_uri' => $githubConfig['callback']
|
|
];
|
|
|
|
// 发送POST请求获取access_token
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $tokenUrl);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($tokenParams));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Accept: application/json'
|
|
]);
|
|
|
|
$tokenResponse = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$tokenData = json_decode($tokenResponse, true);
|
|
|
|
if (!isset($tokenData['access_token'])) {
|
|
die('获取access_token失败:' . $tokenResponse);
|
|
}
|
|
|
|
$accessToken = $tokenData['access_token'];
|
|
|
|
// 2. 通过access_token获取用户信息
|
|
$userInfoUrl = 'https://api.github.com/user';
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $userInfoUrl);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Authorization: token ' . $accessToken,
|
|
'Accept: application/vnd.github.v3+json',
|
|
'User-Agent: ChatApp'
|
|
]);
|
|
|
|
$userInfoResponse = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$userInfo = json_decode($userInfoResponse, true);
|
|
|
|
if (!isset($userInfo['id'])) {
|
|
die('获取用户信息失败:' . $userInfoResponse);
|
|
}
|
|
|
|
$githubId = $userInfo['id'];
|
|
$githubUsername = $userInfo['login'];
|
|
$githubName = isset($userInfo['name']) ? $userInfo['name'] : $githubUsername;
|
|
$githubAvatar = isset($userInfo['avatar_url']) ? $userInfo['avatar_url'] : '';
|
|
|
|
// 3. 获取用户邮箱(如果有)
|
|
$emailUrl = 'https://api.github.com/user/emails';
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $emailUrl);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Authorization: token ' . $accessToken,
|
|
'Accept: application/vnd.github.v3+json',
|
|
'User-Agent: ChatApp'
|
|
]);
|
|
|
|
$emailResponse = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$emailInfo = json_decode($emailResponse, true);
|
|
$githubEmail = '';
|
|
|
|
if (is_array($emailInfo)) {
|
|
foreach ($emailInfo as $email) {
|
|
if (isset($email['primary']) && $email['primary'] && isset($email['verified']) && $email['verified']) {
|
|
$githubEmail = $email['email'];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. 保存第三方用户信息到Session,跳转到绑定页面
|
|
$_SESSION['oauth_provider'] = 'github';
|
|
$_SESSION['oauth_user'] = [
|
|
'id' => $githubId,
|
|
'username' => $githubUsername,
|
|
'name' => $githubName,
|
|
'avatar' => $githubAvatar,
|
|
'email' => $githubEmail
|
|
];
|
|
|
|
// 5. 跳转回绑定页面
|
|
header('Location: ../oauth_bind.php');
|
|
exit;
|
|
?>
|