231 lines
5.8 KiB
TypeScript
231 lines
5.8 KiB
TypeScript
import ky from "ky";
|
|
import { CorpConfig } from "../../core/config/types.js";
|
|
import { createAuthClient } from "../../core/http/factory.js";
|
|
import { AuthenticationError } from "../../core/errors.js";
|
|
|
|
const EASO_BASE_URL = "https://unity.ekuaibao.com";
|
|
const BUSINESS_CENTER_URL = "https://business-center.ekuaibao.com";
|
|
const EKB_BASE_URL = "https://app.ekuaibao.com";
|
|
|
|
export function resolveCorpCluster(corp: { cluster?: string; baseUrl?: string }): string {
|
|
if (corp.cluster && corp.cluster.trim()) {
|
|
return corp.cluster.trim();
|
|
}
|
|
if (corp.baseUrl) {
|
|
try {
|
|
const url = new URL(corp.baseUrl);
|
|
const match = /^([a-z0-9-]+)\.ekuaibao\.com$/i.exec(url.hostname);
|
|
if (match && match[1]) {
|
|
return match[1].toLowerCase();
|
|
}
|
|
} catch {}
|
|
}
|
|
return "app";
|
|
}
|
|
|
|
// 内存缓存
|
|
const easoMemoryCache = new Map<
|
|
string,
|
|
{ token: string; hoseToken: string; expiresAt: number }
|
|
>();
|
|
|
|
const unityMemoryCache = new Map<
|
|
string,
|
|
{ token: string; expiresAt: number }
|
|
>();
|
|
|
|
function isExpired(entry: { expiresAt: number }, refreshTtl: number): boolean {
|
|
return Date.now() >= entry.expiresAt - refreshTtl * 1000;
|
|
}
|
|
|
|
async function fetchHoseToken(
|
|
accessToken: string,
|
|
corpId: string,
|
|
environment: string = "app"
|
|
): Promise<string> {
|
|
const response = await ky
|
|
.get(`${BUSINESS_CENTER_URL}/auth/oauth2/grant/ekb/accessToken`, {
|
|
searchParams: {
|
|
corpId,
|
|
ekbAccessToken: accessToken,
|
|
environment,
|
|
},
|
|
})
|
|
.json<{ data: string }>();
|
|
if (!response.data) {
|
|
throw new AuthenticationError("Failed to fetch hose-access-token");
|
|
}
|
|
return response.data;
|
|
}
|
|
|
|
export async function fetchUnityJwt(
|
|
accessToken: string,
|
|
corpId: string,
|
|
staffId: string,
|
|
baseUrl: string = EKB_BASE_URL
|
|
): Promise<string> {
|
|
const response = await ky
|
|
.get(`${baseUrl}/api/engine/realm/$UNITY/actions/jwt`, {
|
|
searchParams: { corpId },
|
|
headers: {
|
|
accesstoken: accessToken,
|
|
corpid: corpId,
|
|
staffid: staffId,
|
|
},
|
|
})
|
|
.json<{ id?: string; value?: string }>();
|
|
const token = response.id || response.value;
|
|
if (!token) {
|
|
throw new AuthenticationError("Failed to fetch UNITY JWT");
|
|
}
|
|
return token;
|
|
}
|
|
|
|
async function fetchEasoRealmToken(
|
|
corpId: string,
|
|
unityJwt: string,
|
|
hoseToken: string
|
|
): Promise<string> {
|
|
const response = await ky
|
|
.get(`${EASO_BASE_URL}/api/realms/EASO`, {
|
|
searchParams: { corpId },
|
|
headers: {
|
|
Authorization: `Bearer ${unityJwt}`,
|
|
hoseaccesstoken: hoseToken,
|
|
},
|
|
})
|
|
.json<{ value?: { accessToken?: string } }>();
|
|
const token = response.value?.accessToken;
|
|
if (!token) {
|
|
throw new AuthenticationError("Failed to fetch EASO realm token");
|
|
}
|
|
return token;
|
|
}
|
|
|
|
export async function getEasoClient(corp: CorpConfig): Promise<{
|
|
client: ReturnType<typeof createAuthClient>;
|
|
}> {
|
|
const refreshTtl = corp.auth?.refreshTtl ?? 300;
|
|
const cacheKey = `${corp.id}:easo`;
|
|
|
|
// 检查内存缓存
|
|
const mem = easoMemoryCache.get(cacheKey);
|
|
if (mem && !isExpired(mem, refreshTtl)) {
|
|
return {
|
|
client: createAuthClient({
|
|
prefixUrl: EASO_BASE_URL,
|
|
getHeaders: () => ({
|
|
Authorization: `Bearer ${mem.token}`,
|
|
accesstoken: corp.credentials.accessToken,
|
|
corporationid: corp.credentials.corpId,
|
|
hoseaccesstoken: mem.hoseToken,
|
|
}),
|
|
onUnauthorized: () => {
|
|
easoMemoryCache.delete(cacheKey);
|
|
},
|
|
}),
|
|
};
|
|
}
|
|
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
const staffId = corp.staffId || corp.credentials.staffId;
|
|
const environment = resolveCorpCluster(corp);
|
|
const baseUrl = corp.baseUrl || EKB_BASE_URL;
|
|
|
|
// 重新换取
|
|
const hoseToken = await fetchHoseToken(
|
|
corp.credentials.accessToken,
|
|
corpId,
|
|
environment
|
|
);
|
|
const unityJwt = await fetchUnityJwt(
|
|
corp.credentials.accessToken,
|
|
corpId,
|
|
staffId,
|
|
baseUrl
|
|
);
|
|
const easoToken = await fetchEasoRealmToken(
|
|
corpId,
|
|
unityJwt,
|
|
hoseToken
|
|
);
|
|
|
|
const entry = {
|
|
token: easoToken,
|
|
hoseToken,
|
|
expiresAt: Date.now() + 2 * 60 * 60 * 1000, // 2小时
|
|
};
|
|
|
|
easoMemoryCache.set(cacheKey, entry);
|
|
|
|
return {
|
|
client: createAuthClient({
|
|
prefixUrl: EASO_BASE_URL,
|
|
getHeaders: () => ({
|
|
Authorization: `Bearer ${easoToken}`,
|
|
accesstoken: corp.credentials.accessToken,
|
|
corporationid: corpId,
|
|
hoseaccesstoken: hoseToken,
|
|
}),
|
|
onUnauthorized: () => {
|
|
easoMemoryCache.delete(cacheKey);
|
|
},
|
|
}),
|
|
};
|
|
}
|
|
|
|
export async function getUnityClient(corp: CorpConfig): Promise<{
|
|
client: ReturnType<typeof createAuthClient>;
|
|
unityJwt: string;
|
|
}> {
|
|
const refreshTtl = corp.auth?.refreshTtl ?? 300;
|
|
const cacheKey = `${corp.id}:unity`;
|
|
|
|
const mem = unityMemoryCache.get(cacheKey);
|
|
if (mem && !isExpired(mem, refreshTtl)) {
|
|
return {
|
|
unityJwt: mem.token,
|
|
client: createAuthClient({
|
|
prefixUrl: EASO_BASE_URL,
|
|
getHeaders: () => ({
|
|
Authorization: `Bearer ${mem.token}`,
|
|
}),
|
|
onUnauthorized: () => {
|
|
unityMemoryCache.delete(cacheKey);
|
|
},
|
|
}),
|
|
};
|
|
}
|
|
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
const staffId = corp.staffId || corp.credentials.staffId;
|
|
const baseUrl = corp.baseUrl || EKB_BASE_URL;
|
|
|
|
const unityJwt = await fetchUnityJwt(
|
|
corp.credentials.accessToken,
|
|
corpId,
|
|
staffId,
|
|
baseUrl
|
|
);
|
|
|
|
const entry = {
|
|
token: unityJwt,
|
|
expiresAt: Date.now() + 2 * 60 * 60 * 1000,
|
|
};
|
|
unityMemoryCache.set(cacheKey, entry);
|
|
|
|
return {
|
|
unityJwt,
|
|
client: createAuthClient({
|
|
prefixUrl: EASO_BASE_URL,
|
|
getHeaders: () => ({
|
|
Authorization: `Bearer ${unityJwt}`,
|
|
}),
|
|
onUnauthorized: () => {
|
|
unityMemoryCache.delete(cacheKey);
|
|
},
|
|
}),
|
|
};
|
|
}
|
|
|