Files
Sdf1_login/api/gh_cfg.php
T
ypsdf1 a199874c5c 15m44w 续5b 根治横条消失:身份反解不依赖 weblogin token 未过期
根因:gh_cfg/gh_oauth 走 validateTokenSilent 反解玩家名,weblogin token
是 600s 短命,过期后返回 false → probePlayer 空 → gh_authorized 恒 false
→ 授权横条消失(实测 youpaishidifu weblogin token age 2009s>600 已过期)。
但身份反解只需确认 web_token 属于哪个玩家,不需登录态仍有效。

修:gh_cfg + gh_oauth(继承/发起/授权 3 处)普通 token 未命中后,直接查
weblogin_tokens WHERE web_token 拿 player_name(不过期判定),保证过期
token 也能认玩家身份、继承已存 GitHub token、不把授权存到 anon 名下。
2026-11-25 19:58:42 +08:00

84 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 15m44m: 工单前端判断「GitHub issue 同步是否启用」的懒重载端点。
* 返回 {gh_enabled:bool, gh_repo:string, gh_authorized:bool}。前端进工单表单时 fetch:
* - gh_enabled=true 才显示「授权 GitHub 工单同步」按钮
* - gh_repo 用于按钮文案(caoyuanwiki)
* - 15m44v: gh_authorized=true 时前端显示「已授权」态(不再误判为未授权)
* 带 ?token= 时反解玩家名查 gh_auth_tokens 是否已有有效 token。
*/
while (ob_get_level() > 0) { @ob_end_clean(); }
ob_start();
header('Content-Type: application/json; charset=utf-8');
header('X-Content-Type-Options: nosniff');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
require_once __DIR__ . '/../core.php';
require_once __DIR__ . '/../lsky.php';
require_once __DIR__ . '/../gh_api.php'; // 15m44v: ghApiRequest(验活 token)
$cfg = loadImgJson();
$ghAuthorized = false;
// ★ 15m44v: 带 token 时查该玩家是否已有【有效】GitHub token(验活,非只看存没存)。
// 前端 loadGhCfg 改成 fetch('gh_cfg.php?token=' + TOKEN) 后,据此显示「已授权」态,
// 根治"重新进表单 URL 无 gh_ok 就误判为未授权"的老问题。
$probeTk = isset($_GET['token']) ? trim((string)$_GET['token']) : '';
if ($cfg['gh_enabled'] && $probeTk !== '') {
// ★ 15m44w 续5 根治横条消失:身份反解不依赖 weblogin token 是否未过期。
// 旧逻辑走 validateTokenSilent → weblogin token 已过期(600s 短命)则返回 false →
// probePlayer 空 → gh_authorized 恒 false → 授权横条消失。
// 但 gh_cfg 验活只需确认"这个 web_token 属于哪个玩家",玩家身份是确定的,
// 不需要登录 token 仍有效。故直接查 weblogin_tokens(Java 侧)拿 player_name,
// 不做过期判定;同时保留 validateTokenSilent(普通 java token)作为主路径。
$probePlayer = '';
$pi = validateTokenSilent($probeTk); // 普通 java tokens(未过期才命中)
if ($pi && !empty($pi['player'])) {
$probePlayer = $pi['player'];
} else {
// 普通 token 没命中(或已过期)→ 查 weblogin_tokens 拿玩家身份(不过期也认)
try {
$dbProbe = getDB();
$stProbe = $dbProbe->prepare("SELECT player_name FROM weblogin_tokens WHERE web_token = :wtk");
$stProbe->bindValue(':wtk', $probeTk, SQLITE3_TEXT);
$pr = $stProbe->execute()->fetchArray(SQLITE3_ASSOC);
if ($pr && !empty($pr['player_name'])) $probePlayer = $pr['player_name'];
} catch (\Throwable $e) {
ghLog('cfg weblogin identity lookup fail: ' . $e->getMessage());
}
}
if ($probePlayer !== '') {
try {
$db = getDB();
// ★ 15m44w 续4:建表用 VARCHAR(500) 对齐线上 caoyuanphp.gh_auth_tokens 实际 schema
// (旧版 TEXT PRIMARY KEY 在 MySQL 下 TEXT 不能做 PK 报 1101; 表已存在时 IF NOT EXISTS
// 跳过不抛, 但为双模式兼容(老 SQLite 库也建 VARCHAR 无害)改 VARCHAR(500))。
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
player_name VARCHAR(500) PRIMARY KEY, gh_login VARCHAR(500) DEFAULT '',
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->bindValue(':p', $probePlayer, SQLITE3_TEXT);
$trow = $st->execute()->fetchArray(SQLITE3_ASSOC);
if ($trow && !empty($trow['access_token'])) {
// 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法)
$v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']);
$ghAuthorized = !empty($v['ok']);
// ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀)
ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8)
. ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n'));
} else {
ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens');
}
} catch (\Throwable $e) {
ghLog('cfg verify exception: ' . $e->getMessage());
$ghAuthorized = false;
}
}
}
success([
'gh_enabled' => !empty($cfg['gh_enabled']),
'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '',
'gh_authorized' => $ghAuthorized,
], 'ok');