15m44z 续(19:59):修 GitHub 授权误导 —— 刚授权却提示「token 可能已失效」

gh_cfg.php:加 gh_token_fresh 判定(OAuth 回调 updated_at <5min → 免 /user 验活,必有效);
根治 CF 边缘 /user 验活偶发 401/卡住把刚授权的有效 token 误判成失效。
token 保留兜底只对「老 token 验活 401」生效,刚授权(fresh)不进入该分支。
player.php:按 gh_token_fresh 区分文案 —— 刚授权显示「已授权 token 有效」,
老 token 失效才显示「授权已保留,token 可能已失效」。
This commit is contained in:
ypsdf1
2026-11-26 20:04:19 +08:00
parent b32b6ed9ef
commit 8c9b8a10d9
2 changed files with 41 additions and 11 deletions
+23 -4
View File
@@ -23,6 +23,11 @@ require_once __DIR__ . '/../gh_api.php'; // 15m44v: ghApiRequest(验活 toke
$cfg = loadImgJson(); $cfg = loadImgJson();
$ghAuthorized = false; $ghAuthorized = false;
// ★ 15m44z 续(19:59 修):gh_token_fresh 区分「刚授权(免验活必有效)」vs「老 token(验活判)」。
// 刚走完 gh_oauth 回调的玩家 updated_at 很近(<5min),token 必有效 —— 不再发 /user 验活
// (CF 边缘偶发卡住/401 会把有效 token 误判成失效 → 前端显示「token 可能已失效」误导)。
$ghTokenFresh = false;
$ghTokenUpdatedAt = 0;
// ★ 15m44v: 带 token 时查该玩家是否已有【有效】GitHub token(验活,非只看存没存)。 // ★ 15m44v: 带 token 时查该玩家是否已有【有效】GitHub token(验活,非只看存没存)。
// 前端 loadGhCfg 改成 fetch('gh_cfg.php?token=' + TOKEN) 后,据此显示「已授权」态, // 前端 loadGhCfg 改成 fetch('gh_cfg.php?token=' + TOKEN) 后,据此显示「已授权」态,
@@ -60,16 +65,27 @@ if ($cfg['gh_enabled'] && $probeTk !== '') {
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens ( $db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
player_name VARCHAR(500) PRIMARY KEY, gh_login VARCHAR(500) DEFAULT '', player_name VARCHAR(500) PRIMARY KEY, gh_login VARCHAR(500) DEFAULT '',
access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)"); access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)");
$st = $db->prepare("SELECT access_token, gh_login FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); $st = $db->prepare("SELECT access_token, gh_login, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1");
$st->bindValue(':p', $probePlayer, SQLITE3_TEXT); $st->bindValue(':p', $probePlayer, SQLITE3_TEXT);
$trow = $st->execute()->fetchArray(SQLITE3_ASSOC); $trow = $st->execute()->fetchArray(SQLITE3_ASSOC);
if ($trow && !empty($trow['access_token'])) { if ($trow && !empty($trow['access_token'])) {
// ★ 15m44z 续(19:59 修):刚授权(updated_at 距今 < 5min)→ token 必有效,免验活。
// 根治「刚走完 gh_oauth 回调回来,gh_cfg 却显示 token 可能已失效」的误导。
// 老 token(>5min)才走 /user 验活判真失效。
$ghTokenUpdatedAt = (int)($trow['updated_at'] ?? 0);
$ghTokenFresh = ($ghTokenUpdatedAt > 0 && (time() - $ghTokenUpdatedAt) < 300);
if ($ghTokenFresh) {
$ghAuthorized = true; // 刚授权 = 有效,不再发 /user(防 CF 卡住误判 401)
ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8)
. ' fresh=y updated_at=' . $ghTokenUpdatedAt . ' (免验活, 刚授权必有效)');
} else {
// 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法) // 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法)
$v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']); $v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']);
$ghAuthorized = !empty($v['ok']); $ghAuthorized = !empty($v['ok']);
// ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀) // ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀)
ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8)
. ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n')); . ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n'));
}
} else { } else {
ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens'); ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens');
} }
@@ -99,11 +115,13 @@ if ($ghAppbot && $ghAnonMode) {
// 只要 token 行还在,gh_authorized 也应为 true(「token 已保留,可重新授权」), // 只要 token 行还在,gh_authorized 也应为 true(「token 已保留,可重新授权」),
// 避免前端误判"未授权"让玩家以为切匿名后授权被清掉了。 // 避免前端误判"未授权"让玩家以为切匿名后授权被清掉了。
// 判法:$ghAnonAvailable=true 且 $ghAuthorized 验活 401 失败 + gh_auth_tokens 有 token 行 // 判法:$ghAnonAvailable=true 且 $ghAuthorized 验活 401 失败 + gh_auth_tokens 有 token 行
// → 改 gh_authorized=true,前端文案提示"实名通道失效,已保留授权记录,可重新授权"。 // → 改 gh_authorized=true(但 gh_token_fresh 保持 false,前端显示「已保留」而非「已授权」)。
if ($ghAnonAvailable && $probePlayer !== '' && !$ghAuthorized) { // ★ 15m44z 续(19:59):兜底只对「老 token 验活 401」生效;刚授权(fresh)已在上面直接判有效,
// 不会进这个分支,所以刚授权不会误显示「token 可能已失效」。
if ($ghAnonAvailable && $probePlayer !== '' && !$ghAuthorized && !$ghTokenFresh) {
try { try {
$dbTok = getDB(); $dbTok = getDB();
$stTok = $dbTok->prepare("SELECT access_token FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); $stTok = $dbTok->prepare("SELECT access_token, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1");
$stTok->bindValue(':p', $probePlayer, SQLITE3_TEXT); $stTok->bindValue(':p', $probePlayer, SQLITE3_TEXT);
$tokRow = $stTok->execute()->fetchArray(SQLITE3_ASSOC); $tokRow = $stTok->execute()->fetchArray(SQLITE3_ASSOC);
if ($tokRow && !empty($tokRow['access_token'])) { if ($tokRow && !empty($tokRow['access_token'])) {
@@ -121,6 +139,7 @@ success([
'gh_enabled' => !empty($cfg['gh_enabled']), 'gh_enabled' => !empty($cfg['gh_enabled']),
'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '', 'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '',
'gh_authorized' => $ghAuthorized, 'gh_authorized' => $ghAuthorized,
'gh_token_fresh' => $ghTokenFresh, // ★ 15m44z 续(19:59):刚授权免验活=必有效
'gh_appbot' => $ghAppbot, 'gh_appbot' => $ghAppbot,
'gh_anon_available' => $ghAnonAvailable, 'gh_anon_available' => $ghAnonAvailable,
], 'ok'); ], 'ok');
+12 -1
View File
@@ -4002,11 +4002,22 @@ if ($currentVersion !== $BUILD_VERSION) {
// - 匿名模式可用 + gh_authorized=true → "授权已保留(可重新授权)" // - 匿名模式可用 + gh_authorized=true → "授权已保留(可重新授权)"
// - 匿名模式不可用 + gh_authorized=true → "已授权,自动同步"(老态) // - 匿名模式不可用 + gh_authorized=true → "已授权,自动同步"(老态)
if (isAuthed) { if (isAuthed) {
if (d.gh_anon_available === true) { // ★ 15m44z 续(19:59):按 gh_token_fresh 区分「刚授权(必有效)」vs「老 token(可能失效)」。
// 刚走完 gh_oauth 回调(updated_at <5min)→ gh_token_fresh=true → 显示「已授权」,
// 根治「刚授权却提示 token 可能已失效」的误导(CF 验活偶发 401 把有效 token 误判)。
const tokenFresh = d.gh_token_fresh === true;
if (tokenFresh) {
// 刚授权 = 必有效,不显示「可能失效」
if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue';
if (sub) sub.textContent = '刚完成授权,token 有效,工单提交将自动同步到 GitHub issue';
if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; }
} else if (d.gh_anon_available === true) {
// 老 token 失效 + 匿名通道可用 → 已保留授权记录(可重新授权或走匿名)
if (txt) txt.textContent = '🔗 GitHub 授权已保留,切匿名模式不影响授权记录'; if (txt) txt.textContent = '🔗 GitHub 授权已保留,切匿名模式不影响授权记录';
if (sub) sub.textContent = '实名通道 token 可能已失效,点重新授权可刷新;切匿名模式走系统机器人,无需玩家授权'; if (sub) sub.textContent = '实名通道 token 可能已失效,点重新授权可刷新;切匿名模式走系统机器人,无需玩家授权';
if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; }
} else { } else {
// 老 token 失效 + 无匿名通道
if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue'; if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue';
if (sub) sub.textContent = '当前账号 token 有效,工单提交将自动同步到 GitHub issue'; if (sub) sub.textContent = '当前账号 token 有效,工单提交将自动同步到 GitHub issue';
if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; }