15m44z 第三部分: GitHub App bot 匿名工单 + 匿名/实名开关 + 归属派发
【匿名工单主体】 - App 鉴权: gh_api.php 新增 ghAppJwt(PEM签RS256 JWT)+ghAppInstallationToken(换1h installation token, 缓存到 /plugin/gh_app_token_cache.json 55min)+ghAppBotReady。 - 匿名代发: 用 installation token(g hs_前缀)建 issue 归属 bot(caoyuan-server[bot]), 不暴露提单人 GitHub 账号; 提单人信息写 issue 正文「玩家:xxx」+ PHP 后台保留。 - loadImgJson 解析 repo.imgrepo."github app"(appid/安装id/私钥路径/匿名模式)。 【匿名/实名开关(用户定调: 共存, 做成开关)】 - json「匿名模式」字段(github app 段): true=gh模式支持匿名(前端出开关); false=gh模式 一律实名授权(即使 appbot 配了也不给匿名选项)。 - 前端: gh_anon_available=true(匿名模式开+appbot可用)才显示「匿名工单」开关; 开关开→提交带 anon=1(bot代发), 关→走玩家OAuth建单; 模式留空(PHP内置工单)不给匿名。 - 建单: anon=1 且匿名模式开→appbot token; 否则玩家 OAuth token。 【归属派发(用户定调: 按工单所有人)】 - ghSyncCreate 建 issue 后回写 gh_issue_owner(bot/realname, 按 token 前缀 ghs_/gho_)。 - 撤单/完结/回复按 ghTicketTokenByTicket 归属: bot建的单→机器人代发撤/完结/评论; 实名单→玩家 OAuth token(保持现有逻辑)。 实测: App JWT→installation token→建issue#24(归属caoyuan-server[bot])全链路通; 线上 gh_cfg 返回 gh_anon_available=true。6件PHP+img.json模板+真值img.json+私钥全MATCH。
This commit is contained in:
+100
-1
@@ -31,6 +31,99 @@ function ghGetCfg() {
|
||||
return loadImgJson();
|
||||
}
|
||||
|
||||
// ===== ★ 15m44z 第三部分:GitHub App bot 匿名工单鉴权 =====
|
||||
// 流程:App 私钥(PEM) 签 RS256 JWT → POST /app/installations/{id}/access_tokens
|
||||
// 换 installation token(1h 有效)→ 用该 token 建 issue(归属 bot,提单人匿名)。
|
||||
// installation token 缓存到文件(/plugin/gh_app_token_cache.json,git 拉黑),
|
||||
// 有效期内复用,过期/失效重取。私钥路径来自 img.json 的 repo.imgrepo."github app".私钥。
|
||||
|
||||
/** base64url(无 padding) */
|
||||
function ghB64Url($bin) {
|
||||
return rtrim(strtr(base64_encode($bin), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
/** 用 App 私钥签 RS256 JWT(GitHub App 鉴权用,有效期 10min 内即可,取 9min)。 */
|
||||
function ghAppJwt() {
|
||||
$cfg = ghGetCfg();
|
||||
$pemPath = isset($cfg['gh_app_pem_path']) ? (string)$cfg['gh_app_pem_path'] : '';
|
||||
$appId = isset($cfg['gh_app_id']) ? (string)$cfg['gh_app_id'] : '';
|
||||
if ($pemPath === '' || $appId === '' || !is_readable($pemPath)) {
|
||||
return ''; // 私钥缺失/不可读
|
||||
}
|
||||
$keyPem = file_get_contents($pemPath);
|
||||
if ($keyPem === false) return '';
|
||||
if (!function_exists('openssl_pkey_get_private')) return ''; // openssl 扩展缺失
|
||||
$priv = openssl_pkey_get_private($keyPem);
|
||||
if ($priv === false) return '';
|
||||
$now = time();
|
||||
$header = ghB64Url(json_encode(['alg' => 'RS256', 'typ' => 'JWT']));
|
||||
$payload = ghB64Url(json_encode(['iat' => $now, 'exp' => $now + 540, 'iss' => $appId]));
|
||||
$signingInput = $header . '.' . $payload;
|
||||
$ok = openssl_sign($signingInput, $signature, $priv, OPENSSL_ALGO_SHA256);
|
||||
if (!$ok) return '';
|
||||
return $signingInput . '.' . ghB64Url($signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取(并缓存)installation access token。
|
||||
* @return 字符串 token;取不到返回 ''。
|
||||
* 缓存文件含 {token, expires_at},有效期 55min(留 5min 余量防过期)。
|
||||
*/
|
||||
function ghAppInstallationToken() {
|
||||
$cfg = ghGetCfg();
|
||||
if (empty($cfg['gh_app_bot'])) return ''; // appbot 未启用
|
||||
$cacheFile = __DIR__ . '/gh_app_token_cache.json';
|
||||
// 读缓存
|
||||
if (is_readable($cacheFile)) {
|
||||
$c = json_decode((string)file_get_contents($cacheFile), true);
|
||||
if (is_array($c) && !empty($c['token']) && (int)($c['expires_at'] ?? 0) > time() + 300) {
|
||||
return (string)$c['token'];
|
||||
}
|
||||
}
|
||||
$jwt = ghAppJwt();
|
||||
if ($jwt === '') return '';
|
||||
$instId = (string)($cfg['gh_app_installation_id'] ?? '');
|
||||
// 未配 installation id → 动态查(取该 App 在目标仓 owner 下的第一个安装)
|
||||
if ($instId === '' || $instId === '0') {
|
||||
$r = ghApiRequest('GET', '/app/installations?per_page=100', null, $jwt);
|
||||
if (!$r['ok'] || !is_array($r['data'])) return '';
|
||||
$target = (string)($cfg['gh_repo'] ?? '');
|
||||
$owner = $target !== '' ? explode('/', $target)[0] : '';
|
||||
$found = '';
|
||||
foreach ($r['data'] as $ins) {
|
||||
if (!is_array($ins)) continue;
|
||||
$accLogin = isset($ins['account']['login']) ? (string)$ins['account']['login'] : '';
|
||||
$accId = isset($ins['account']['id']) ? (int)$ins['account']['id'] : 0;
|
||||
if ($owner !== '' && ($accLogin === $owner || $accId === (int)$owner)) { $found = (string)$ins['id']; break; }
|
||||
}
|
||||
if ($found === '' && !empty($r['data'][0]['id'])) $found = (string)$r['data'][0]['id'];
|
||||
if ($found === '') return '';
|
||||
$instId = $found;
|
||||
}
|
||||
// 换 installation token
|
||||
$r = ghApiRequest('POST', '/app/installations/' . $instId . '/access_tokens', [], $jwt);
|
||||
if (!$r['ok'] || !is_array($r['data']) || empty($r['data']['token'])) {
|
||||
ghLog('appbot install token fail: ' . $r['msg']);
|
||||
return '';
|
||||
}
|
||||
$token = (string)$r['data']['token'];
|
||||
// 缓存(installation token 有效期 1h,缓存 55min)
|
||||
@file_put_contents($cacheFile, json_encode([
|
||||
'token' => $token,
|
||||
'expires_at' => time() + 3300,
|
||||
'installation_id' => $instId,
|
||||
]));
|
||||
@chmod($cacheFile, 0600);
|
||||
return $token;
|
||||
}
|
||||
|
||||
/** appbot 是否可用(启用 + 能拿到 installation token)。 */
|
||||
function ghAppBotReady() {
|
||||
$cfg = ghGetCfg();
|
||||
if (empty($cfg['gh_app_bot'])) return false;
|
||||
return ghAppInstallationToken() !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* GitHub REST 底层请求。
|
||||
* @return ['ok'=>bool,'status'=>int,'msg'=>string,'data'=>mixed]
|
||||
@@ -197,8 +290,14 @@ function ghSyncCreate($ticketId, $player, $title, $description, $images = [], $t
|
||||
try {
|
||||
$db = getDB();
|
||||
try { $db->exec("ALTER TABLE web_tickets ADD COLUMN gh_issue_number TEXT DEFAULT ''"); } catch (\Throwable $e) { /* 列已存在 */ }
|
||||
$st = $db->prepare("UPDATE web_tickets SET gh_issue_number = :n WHERE id = :id");
|
||||
// ★ 15m44z 第三部分:记录 issue 归属(bot/realname)——撤单/完结按归属者派发:
|
||||
// bot 建的单→撤/完结交给机器人(installation token);实名单→玩家 OAuth token(老逻辑)。
|
||||
// 判定:installation token 前缀 ghs_,玩家 OAuth token 前缀 gho_/ghp_。
|
||||
try { $db->exec("ALTER TABLE web_tickets ADD COLUMN gh_issue_owner TEXT DEFAULT ''"); } catch (\Throwable $e2) { /* 列已存在 */ }
|
||||
$owner = (strpos($token, 'ghs_') === 0) ? 'bot' : ((strpos($token, 'gho_') === 0 || strpos($token, 'ghp_') === 0) ? 'realname' : '');
|
||||
$st = $db->prepare("UPDATE web_tickets SET gh_issue_number = :n, gh_issue_owner = :o WHERE id = :id");
|
||||
$st->bindValue(':n', $issueNo, SQLITE3_TEXT);
|
||||
$st->bindValue(':o', $owner, SQLITE3_TEXT);
|
||||
$st->bindValue(':id', $ticketId, SQLITE3_INTEGER);
|
||||
$st->execute();
|
||||
} catch (\Throwable $e) { ghLog('write gh_issue_number: ' . $e->getMessage()); }
|
||||
|
||||
Reference in New Issue
Block a user