feat(delegation): v0.4.2 - Hose SSO fallback for synced users in users delegation

- createDelegationForUser now detects hosecloud-synced users (provider != local)
- Falls back to Hose Provisional Auth → Seal SSO session exchange → delegation create
- Generates a true user-identity Seal delegation link without password reset
- Fixes name search to prefer exact match before falling back to first result
- Exports normalizeBaseUrl, defaultSealUrl, ensureHoseOpenapiToken, fetchHoseCloseApiToken
- Updates SKILL.md: documents users delegation hosecloud limitation and fallback behavior
This commit is contained in:
gitea committed 2026-07-29 18:39:26 +08:00
1 parent c0318df70b
commit ef115835ac
5 files changed
+98 -19

No files matched your search

+77 -4
View File
@@ -1,5 +1,7 @@
import { KyInstance } from "ky";
import ky, { KyInstance } from "ky";
import { z } from "zod";
import { CorpConfig } from "../../core/config/types.js";
import { ensureHoseOpenapiToken, fetchHoseCloseApiToken, defaultSealUrl, normalizeBaseUrl } from "../../core/auth/hose.js";
import {
ApprovalDocumentListData,
ApprovalDocumentListDataSchema,
@@ -445,7 +447,8 @@ export async function loginWithPhonePassword(
export async function createDelegationForUser(
client: KyInstance,
params: { userIdOrPhone: string; note?: string },
createClientForToken: (token: string) => Promise<KyInstance>
createClientForToken: (token: string) => Promise<KyInstance>,
corp?: CorpConfig
): Promise<{
targetUser: SealUserDetail;
delegation: SealDelegationCreateResult;
@@ -464,7 +467,9 @@ export async function createDelegationForUser(
} else {
user = await getUserDetail(client, params.userIdOrPhone).catch(async () => {
const search = await searchUsers(client, params.userIdOrPhone);
const found = search.users[0];
// 优先精确匹配姓名,避免返回错误用户
const found =
search.users.find((u) => u.name === params.userIdOrPhone) ?? search.users[0];
if (!found) throw new Error(`User not found: ${params.userIdOrPhone}`);
return found;
});
@@ -475,7 +480,75 @@ export async function createDelegationForUser(
user = await getUserDetail(client, user._id);
}
const resetRes = await resetUserPassword(client, user._id);
const userAny = user as any;
const isSynced =
(Array.isArray(userAny.identities) &&
(userAny.identities as Array<{ provider?: string }>).some(
(i) => i.provider && i.provider !== "local"
)) ||
!!userAny.apps?.hosecloud;
if (isSynced && corp && corp.source.type === "hose") {
const rawPhone =
user.mobile ||
(userAny.identities as Array<{ providerId?: string }>)?.[0]?.providerId ||
"";
const cleanPhone = rawPhone.replace(/^\+86/, "");
const hoseIdentity = (userAny.identities as Array<{ provider?: string; providerId?: string }>)?.find(
(i) => i.provider === "hosecloud"
)?.providerId;
const staffId =
(userAny.apps?.hosecloud as { boundId?: string })?.boundId ||
hoseIdentity ||
(cleanPhone ? `${corp.source.corpId}:${cleanPhone}` : undefined);
if (staffId) {
const openapiToken = await ensureHoseOpenapiToken(corp);
const provisional = await fetchHoseCloseApiToken({ ...corp.source, staffId }, openapiToken);
const closeapiToken = provisional.token;
const sealUrl = defaultSealUrl(corp, corp.source);
const sealResponse = await ky
.get(`${normalizeBaseUrl(sealUrl)}/api/auth/oauth2/session/oem-hosecloud`, {
searchParams: {
token: closeapiToken,
returnToken: "1"
},
timeout: 30000
})
.json<{ token?: string; accessToken?: string; data?: { token?: string } }>();
const targetUserSealToken =
sealResponse.token ?? sealResponse.accessToken ?? sealResponse.data?.token;
if (targetUserSealToken) {
const userClient = await createClientForToken(targetUserSealToken);
const delegation = await createDelegation(userClient, {
note: params.note ?? `Delegation generated by admin for ${user.name}`
});
return {
targetUser: user,
delegation,
temporaryPassword: "N/A (Hose SSO)"
};
}
}
}
let resetRes: SealUserResetPasswordResult;
try {
resetRes = await resetUserPassword(client, user._id);
} catch (err: any) {
if (isSynced) {
throw new Error(
`用户 "${user.name}" (${user.mobile || user._id}) 为合思同步账号 (provider: hosecloud),无 Seal 本地密码。` +
`Seal 系统暂不支持对同步账号直接重置本地密码。`
);
}
throw err;
}
const rawPhone = user.mobile || (user as unknown as { identities?: Array<{ providerId?: string }> }).identities?.[0]?.providerId || "";
const cleanPhone = rawPhone.replace(/^\+86/, "");
if (!cleanPhone) {
+5 -2
View File
@@ -126,8 +126,11 @@ export const sealTools = [
params: { userIdOrPhone: string; note?: string },
context: ToolContext
) =>
api.createDelegationForUser(client, params, (token) =>
createSealClientForToken(context.corp, token)
api.createDelegationForUser(
client,
params,
(token) => createSealClientForToken(context.corp, token),
context.corp
)
},
{