feat(auth): 实现全自动无感续杯 (401/即将过期自动重试换票) 与企业多身份唯一标识分层
This commit is contained in:
1 parent
189415cdac
commit
b7a3bf2c03
7 files changed
+122
-17
No files matched your search
@@ -104,18 +104,21 @@ export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
};
|
||||
}
|
||||
|
||||
const corpId = corp.corpId || corp.credentials.corpId;
|
||||
const staffId = corp.staffId || corp.credentials.staffId;
|
||||
|
||||
// 重新换取
|
||||
const hoseToken = await fetchHoseToken(
|
||||
corp.credentials.accessToken,
|
||||
corp.credentials.corpId
|
||||
corpId
|
||||
);
|
||||
const unityJwt = await fetchUnityJwt(
|
||||
corp.credentials.accessToken,
|
||||
corp.credentials.corpId,
|
||||
corp.credentials.staffId
|
||||
corpId,
|
||||
staffId
|
||||
);
|
||||
const easoToken = await fetchEasoRealmToken(
|
||||
corp.credentials.corpId,
|
||||
corpId,
|
||||
unityJwt,
|
||||
hoseToken
|
||||
);
|
||||
@@ -134,7 +137,7 @@ export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
getHeaders: () => ({
|
||||
Authorization: `Bearer ${easoToken}`,
|
||||
accesstoken: corp.credentials.accessToken,
|
||||
corporationid: corp.credentials.corpId,
|
||||
corporationid: corpId,
|
||||
hoseaccesstoken: hoseToken,
|
||||
}),
|
||||
onUnauthorized: () => {
|
||||
|
||||
+19
-6
@@ -1,17 +1,30 @@
|
||||
import { CorpConfig } from "../config/types.js";
|
||||
import { createClient } from "../http/factory.js";
|
||||
import { createAuthClient } from "../http/factory.js";
|
||||
import { refreshSingleCorpAccessToken } from "./login.js";
|
||||
|
||||
export function getEkbHeaders(corp: CorpConfig): Record<string, string> {
|
||||
return {
|
||||
accesstoken: corp.credentials.accessToken,
|
||||
corpid: corp.credentials.corpId,
|
||||
staffid: corp.credentials.staffId,
|
||||
corpid: corp.corpId || corp.credentials.corpId,
|
||||
staffid: corp.staffId || corp.credentials.staffId,
|
||||
};
|
||||
}
|
||||
|
||||
export function createEkbClient(corp: CorpConfig) {
|
||||
return createClient({
|
||||
export async function createEkbClient(corp: CorpConfig) {
|
||||
// 1. 预检:若 Token 即将过期 (提前 refreshTtl 秒),静默无感自动续杯
|
||||
if (corp.auth?.expiresAt) {
|
||||
const ttlMs = (corp.auth.refreshTtl || 300) * 1000;
|
||||
if (Date.now() >= corp.auth.expiresAt - ttlMs) {
|
||||
await refreshSingleCorpAccessToken(corp);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 使用带 401 自动重试与换票的 AuthClient
|
||||
return createAuthClient({
|
||||
prefixUrl: corp.baseUrl || "https://app.ekuaibao.com",
|
||||
headers: getEkbHeaders(corp),
|
||||
getHeaders: () => getEkbHeaders(corp),
|
||||
onUnauthorized: async () => {
|
||||
await refreshSingleCorpAccessToken(corp);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -391,6 +391,9 @@ export async function switchCorporationAndExchangeToken(
|
||||
const corpConfig: CorpConfig = {
|
||||
id: corp.corporationId,
|
||||
name: corp.corporationName,
|
||||
corpId: corp.corporationId,
|
||||
userId: corp.userId,
|
||||
staffId: corp.staffId,
|
||||
baseUrl: corp.baseUrl || "https://app.ekuaibao.com",
|
||||
source: "account",
|
||||
accountId,
|
||||
@@ -414,6 +417,51 @@ export async function switchCorporationAndExchangeToken(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对单个企业凭证进行静默无感自动续杯
|
||||
*/
|
||||
export async function refreshSingleCorpAccessToken(
|
||||
corp: CorpConfig,
|
||||
businessCenterUrl: string = DEFAULT_BUSINESS_CENTER_URL,
|
||||
idpUrl: string = DEFAULT_IDP_URL
|
||||
): Promise<boolean> {
|
||||
const account = corp.accountId ? getAccount(corp.accountId) : getActiveAccount();
|
||||
if (!account?.cloudAuthToken) return false;
|
||||
|
||||
try {
|
||||
const corpItem: CorporationItem = {
|
||||
corporationId: corp.corpId || corp.credentials.corpId,
|
||||
corporationName: corp.name,
|
||||
cluster: "app",
|
||||
clientId: "whispered-app",
|
||||
staffId: corp.staffId || corp.credentials.staffId,
|
||||
userId: corp.userId || account.userId || "",
|
||||
baseUrl: corp.baseUrl || "https://app.ekuaibao.com",
|
||||
external: false,
|
||||
needAuth: false,
|
||||
};
|
||||
|
||||
const res = await switchCorporationAndExchangeToken(
|
||||
account.cloudAuthToken,
|
||||
corpItem,
|
||||
corp.accountId,
|
||||
idpUrl,
|
||||
businessCenterUrl
|
||||
);
|
||||
|
||||
corp.credentials.accessToken = res.accessToken;
|
||||
if (!corp.auth) corp.auth = { refreshTtl: 300 };
|
||||
corp.auth.refreshToken = res.refreshToken;
|
||||
corp.auth.expiresAt = Date.now() + res.expiresIn * 1000;
|
||||
|
||||
saveCorpConfig(corp);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.warn(`[auth] 自动续杯刷新企业 ${corp.name} 失败:`, err instanceof Error ? err.message : String(err));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 全流程一站式高阶函数 ─────────────────────────────────────────────────────
|
||||
|
||||
export interface FullLoginOptions {
|
||||
|
||||
@@ -80,15 +80,18 @@ export async function getPiperClient(
|
||||
});
|
||||
}
|
||||
|
||||
const corpId = corp.corpId || corp.credentials.corpId;
|
||||
const staffId = corp.staffId || corp.credentials.staffId;
|
||||
|
||||
// 重新换取
|
||||
const hoseToken = await fetchHoseToken(
|
||||
corp.credentials.accessToken,
|
||||
corp.credentials.corpId
|
||||
corpId
|
||||
);
|
||||
const easoJwt = await fetchEasoJwt(
|
||||
corp.credentials.accessToken,
|
||||
corp.credentials.corpId,
|
||||
corp.credentials.staffId
|
||||
corpId,
|
||||
staffId
|
||||
);
|
||||
|
||||
const entry = {
|
||||
@@ -104,6 +107,7 @@ export async function getPiperClient(
|
||||
getHeaders: () => ({
|
||||
Authorization: `Bearer ${easoJwt}`,
|
||||
platformid: platformId,
|
||||
hoseaccesstoken: hoseToken,
|
||||
}),
|
||||
onUnauthorized: () => {
|
||||
piperMemoryCache.delete(cacheKey);
|
||||
|
||||
Reference in new issue
Block a user