refactor: remove useless try/catch wrappers

移除了 3 个无用的 try/catch 包装器:
- users.ts: 移除只重新抛出错误的外层 try/catch
- FileManager.tsx: 移除只重新抛出错误的内层 try/catch
- DiffViewer.tsx: 移除只重新抛出错误的内层 try/catch

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ZacharyZcR
2025-10-05 21:37:34 +08:00
parent 57a99ae31b
commit f3fde836bc
3 changed files with 106 additions and 118 deletions

View File

@@ -28,93 +28,89 @@ async function verifyOIDCToken(
issuerUrl: string, issuerUrl: string,
clientId: string, clientId: string,
): Promise<any> { ): Promise<any> {
const normalizedIssuerUrl = issuerUrl.endsWith("/")
? issuerUrl.slice(0, -1)
: issuerUrl;
const possibleIssuers = [
issuerUrl,
normalizedIssuerUrl,
issuerUrl.replace(/\/application\/o\/[^\/]+$/, ""),
normalizedIssuerUrl.replace(/\/application\/o\/[^\/]+$/, ""),
];
const jwksUrls = [
`${normalizedIssuerUrl}/.well-known/jwks.json`,
`${normalizedIssuerUrl}/jwks/`,
`${normalizedIssuerUrl.replace(/\/application\/o\/[^\/]+$/, "")}/.well-known/jwks.json`,
];
try { try {
const normalizedIssuerUrl = issuerUrl.endsWith("/") const discoveryUrl = `${normalizedIssuerUrl}/.well-known/openid-configuration`;
? issuerUrl.slice(0, -1) const discoveryResponse = await fetch(discoveryUrl);
: issuerUrl; if (discoveryResponse.ok) {
const possibleIssuers = [ const discovery = (await discoveryResponse.json()) as any;
issuerUrl, if (discovery.jwks_uri) {
normalizedIssuerUrl, jwksUrls.unshift(discovery.jwks_uri);
issuerUrl.replace(/\/application\/o\/[^\/]+$/, ""),
normalizedIssuerUrl.replace(/\/application\/o\/[^\/]+$/, ""),
];
const jwksUrls = [
`${normalizedIssuerUrl}/.well-known/jwks.json`,
`${normalizedIssuerUrl}/jwks/`,
`${normalizedIssuerUrl.replace(/\/application\/o\/[^\/]+$/, "")}/.well-known/jwks.json`,
];
try {
const discoveryUrl = `${normalizedIssuerUrl}/.well-known/openid-configuration`;
const discoveryResponse = await fetch(discoveryUrl);
if (discoveryResponse.ok) {
const discovery = (await discoveryResponse.json()) as any;
if (discovery.jwks_uri) {
jwksUrls.unshift(discovery.jwks_uri);
}
}
} catch (discoveryError) {
authLogger.error(`OIDC discovery failed: ${discoveryError}`);
}
let jwks: any = null;
for (const url of jwksUrls) {
try {
const response = await fetch(url);
if (response.ok) {
const jwksData = (await response.json()) as any;
if (jwksData && jwksData.keys && Array.isArray(jwksData.keys)) {
jwks = jwksData;
break;
} else {
authLogger.error(
`Invalid JWKS structure from ${url}: ${JSON.stringify(jwksData)}`,
);
}
} else {
// Non-200 response
}
} catch {
continue;
} }
} }
} catch (discoveryError) {
if (!jwks) { authLogger.error(`OIDC discovery failed: ${discoveryError}`);
throw new Error("Failed to fetch JWKS from any URL");
}
if (!jwks.keys || !Array.isArray(jwks.keys)) {
throw new Error(
`Invalid JWKS response structure. Expected 'keys' array, got: ${JSON.stringify(jwks)}`,
);
}
const header = JSON.parse(
Buffer.from(idToken.split(".")[0], "base64").toString(),
);
const keyId = header.kid;
const publicKey = jwks.keys.find((key: any) => key.kid === keyId);
if (!publicKey) {
throw new Error(
`No matching public key found for key ID: ${keyId}. Available keys: ${jwks.keys.map((k: any) => k.kid).join(", ")}`,
);
}
const { importJWK, jwtVerify } = await import("jose");
const key = await importJWK(publicKey);
const { payload } = await jwtVerify(idToken, key, {
issuer: possibleIssuers,
audience: clientId,
});
return payload;
} catch (error) {
throw error;
} }
let jwks: any = null;
for (const url of jwksUrls) {
try {
const response = await fetch(url);
if (response.ok) {
const jwksData = (await response.json()) as any;
if (jwksData && jwksData.keys && Array.isArray(jwksData.keys)) {
jwks = jwksData;
break;
} else {
authLogger.error(
`Invalid JWKS structure from ${url}: ${JSON.stringify(jwksData)}`,
);
}
} else {
// Non-200 response
}
} catch {
continue;
}
}
if (!jwks) {
throw new Error("Failed to fetch JWKS from any URL");
}
if (!jwks.keys || !Array.isArray(jwks.keys)) {
throw new Error(
`Invalid JWKS response structure. Expected 'keys' array, got: ${JSON.stringify(jwks)}`,
);
}
const header = JSON.parse(
Buffer.from(idToken.split(".")[0], "base64").toString(),
);
const keyId = header.kid;
const publicKey = jwks.keys.find((key: any) => key.kid === keyId);
if (!publicKey) {
throw new Error(
`No matching public key found for key ID: ${keyId}. Available keys: ${jwks.keys.map((k: any) => k.kid).join(", ")}`,
);
}
const { importJWK, jwtVerify } = await import("jose");
const key = await importJWK(publicKey);
const { payload } = await jwtVerify(idToken, key, {
issuer: possibleIssuers,
audience: clientId,
});
return payload;
} }
const router = express.Router(); const router = express.Router();

View File

@@ -710,27 +710,23 @@ function FileManagerContent({ initialHost, onClose }: FileManagerProps) {
try { try {
const currentSessionId = sshSessionId; const currentSessionId = sshSessionId;
try { const status = await getSSHStatus(currentSessionId);
const status = await getSSHStatus(currentSessionId); if (!status.connected) {
if (!status.connected) { const result = await connectSSH(currentSessionId, {
const result = await connectSSH(currentSessionId, { hostId: currentHost.id,
hostId: currentHost.id, host: currentHost.ip,
host: currentHost.ip, port: currentHost.port,
port: currentHost.port, username: currentHost.username,
username: currentHost.username, authType: currentHost.authType,
authType: currentHost.authType, password: currentHost.password,
password: currentHost.password, key: currentHost.key,
key: currentHost.key, keyPassword: currentHost.keyPassword,
keyPassword: currentHost.keyPassword, credentialId: currentHost.credentialId,
credentialId: currentHost.credentialId, });
});
if (!result.success) { if (!result.success) {
throw new Error(t("fileManager.failedToReconnectSSH")); throw new Error(t("fileManager.failedToReconnectSSH"));
}
} }
} catch (sessionErr) {
throw sessionErr;
} }
const symlinkInfo = await identifySSHSymlink(currentSessionId, file.path); const symlinkInfo = await identifySSHSymlink(currentSessionId, file.path);

View File

@@ -62,22 +62,18 @@ export function DiffViewer({
}); });
} }
} catch (error) { } catch (error) {
try { await connectSSH(sshSessionId, {
await connectSSH(sshSessionId, { hostId: sshHost.id,
hostId: sshHost.id, ip: sshHost.ip,
ip: sshHost.ip, port: sshHost.port,
port: sshHost.port, username: sshHost.username,
username: sshHost.username, password: sshHost.password,
password: sshHost.password, sshKey: sshHost.key,
sshKey: sshHost.key, keyPassword: sshHost.keyPassword,
keyPassword: sshHost.keyPassword, authType: sshHost.authType,
authType: sshHost.authType, credentialId: sshHost.credentialId,
credentialId: sshHost.credentialId, userId: sshHost.userId,
userId: sshHost.userId, });
});
} catch (reconnectError) {
throw reconnectError;
}
} }
}; };