89 行
2.8 KiB
PHP
89 行
2.8 KiB
PHP
<?php
|
|
/**
|
|
* Steam登录回调处理
|
|
*/
|
|
|
|
session_start();
|
|
require_once '../config.php';
|
|
|
|
$config = require '../config.php';
|
|
$steamConfig = $config['steam'] ?? [];
|
|
$apiKey = $steamConfig['api_key'] ?? '';
|
|
|
|
// 获取Steam返回的参数
|
|
$openidMode = $_GET['openid_mode'] ?? '';
|
|
|
|
if ($openidMode === 'id_res') {
|
|
// 验证Steam登录
|
|
$params = [
|
|
'openid.assoc_handle' => $_GET['openid_assoc_handle'] ?? '',
|
|
'openid.signed' => $_GET['openid_signed'] ?? '',
|
|
'openid.sig' => $_GET['openid_sig'] ?? '',
|
|
'openid.ns' => 'http://specs.openid.net/auth/2.0',
|
|
'openid.mode' => 'check_authentication'
|
|
];
|
|
|
|
// 添加所有signed参数
|
|
$signed = explode(',', $params['openid.signed']);
|
|
foreach ($signed as $item) {
|
|
$params['openid.' . $item] = $_GET['openid_' . str_replace('.', '_', $item)] ?? '';
|
|
}
|
|
|
|
// 发送验证请求到Steam
|
|
$ch = curl_init('https://steamcommunity.com/openid/login');
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
if (strpos($response, 'is_valid:true') !== false) {
|
|
// 登录成功,提取Steam ID
|
|
$claimedId = $_GET['openid_claimed_id'] ?? '';
|
|
preg_match('/\/id\/(\d+)/', $claimedId, $matches);
|
|
$steamId = $matches[1] ?? '';
|
|
|
|
$username = "Steam_$steamId";
|
|
$avatar = '';
|
|
|
|
if ($steamId && $apiKey) {
|
|
// 使用Steam API获取用户信息
|
|
$apiUrl = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$apiKey&steamids=$steamId";
|
|
$ch = curl_init($apiUrl);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
$apiResponse = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$userData = json_decode($apiResponse, true);
|
|
$player = $userData['response']['players'][0] ?? [];
|
|
|
|
$username = $player['personaname'] ?? $username;
|
|
$avatar = $player['avatarfull'] ?? '';
|
|
}
|
|
|
|
// 保存第三方用户信息到Session,跳转到绑定页面
|
|
$_SESSION['oauth_provider'] = 'steam';
|
|
$_SESSION['oauth_user'] = [
|
|
'id' => $steamId,
|
|
'username' => $username,
|
|
'name' => $username,
|
|
'avatar' => $avatar,
|
|
'email' => ''
|
|
];
|
|
|
|
// 跳转回绑定页面
|
|
header('Location: ../oauth_bind.php');
|
|
exit;
|
|
} else {
|
|
// 登录失败
|
|
$_SESSION['error'] = 'Steam登录失败,请稍后重试';
|
|
header('Location: ../index.php');
|
|
exit;
|
|
}
|
|
} else {
|
|
// 不是有效的回调请求
|
|
$_SESSION['error'] = '无效的登录请求';
|
|
header('Location: ../index.php');
|
|
exit;
|
|
}
|
|
?>
|