🐛 fix(kiro): 优化KIRO_OAUTH_CREDS_FILE_PATH配置的凭据加载逻辑

- 当配置KIRO_OAUTH_CREDS_FILE_PATH时跳过~/.aws/sso/cache/目录扫描
- 修复token刷新时保存路径问题,使用用户指定路径而不是默认路径
- 改进错误处理和调试日志
This commit is contained in:
LaelLuo 2025-08-12 14:33:43 +08:00
parent faf215dae1
commit 76411cad0d

View file

@ -347,20 +347,28 @@ async initializeAuth(forceRefresh = false) {
} }
} }
// Priority 3: Load from default directory if no specific file path and no token file credentials // Priority 3: Load from default directory only if no specific file path is configured
const dirPath = this.credPath; if (!this.credsFilePath) {
console.debug(`[Kiro Auth] Attempting to load credentials from directory: ${dirPath}`); const dirPath = this.credPath;
const files = await fs.readdir(dirPath); console.debug(`[Kiro Auth] Attempting to load credentials from directory: ${dirPath}`);
for (const file of files) { try {
if (file.endsWith('.json') && file !== KIRO_AUTH_TOKEN_FILE) { const files = await fs.readdir(dirPath);
const filePath = path.join(dirPath, file); for (const file of files) {
const credentials = await loadCredentialsFromFile(filePath); if (file.endsWith('.json') && file !== KIRO_AUTH_TOKEN_FILE) {
if (credentials) { const filePath = path.join(dirPath, file);
credentials.expiresAt = mergedCredentials.expiresAt; const credentials = await loadCredentialsFromFile(filePath);
Object.assign(mergedCredentials, credentials); if (credentials) {
console.debug(`[Kiro Auth] Loaded credentials from ${file}`); credentials.expiresAt = mergedCredentials.expiresAt;
Object.assign(mergedCredentials, credentials);
console.debug(`[Kiro Auth] Loaded credentials from ${file}`);
}
}
} }
} catch (error) {
console.debug(`[Kiro Auth] Could not read credential directory ${dirPath}: ${error.message}`);
} }
} else {
console.debug(`[Kiro Auth] Skipping directory scan because specific file path is configured: ${this.credsFilePath}`);
} }
// console.log('[Kiro Auth] Merged credentials:', mergedCredentials); // console.log('[Kiro Auth] Merged credentials:', mergedCredentials);
@ -385,7 +393,7 @@ async initializeAuth(forceRefresh = false) {
this.baseUrl = KIRO_CONSTANTS.BASE_URL.replace("{{region}}", this.region); this.baseUrl = KIRO_CONSTANTS.BASE_URL.replace("{{region}}", this.region);
this.amazonQUrl = KIRO_CONSTANTS.AMAZON_Q_URL.replace("{{region}}", this.region); this.amazonQUrl = KIRO_CONSTANTS.AMAZON_Q_URL.replace("{{region}}", this.region);
} catch (error) { } catch (error) {
console.warn(`[Kiro Auth] Could not read credential directory ${this.credPath}: ${error.message}`); console.warn(`[Kiro Auth] Error during credential loading: ${error.message}`);
} }
// Refresh token if forced or if access token is missing but refresh token is available // Refresh token if forced or if access token is missing but refresh token is available
@ -417,8 +425,8 @@ async initializeAuth(forceRefresh = false) {
this.expiresAt = expiresAt; this.expiresAt = expiresAt;
console.info('[Kiro Auth] Access token refreshed successfully'); console.info('[Kiro Auth] Access token refreshed successfully');
// Update the token file // Update the token file - use specified path if configured, otherwise use default
const tokenFilePath = path.join(this.credPath, KIRO_AUTH_TOKEN_FILE); const tokenFilePath = this.credsFilePath || path.join(this.credPath, KIRO_AUTH_TOKEN_FILE);
const updatedTokenData = { const updatedTokenData = {
accessToken: this.accessToken, accessToken: this.accessToken,
refreshToken: this.refreshToken, refreshToken: this.refreshToken,