Add validation and logging improvements based on code review

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-11-07 16:32:04 +00:00
parent 013f44b2bd
commit 5e7e39753c
2 changed files with 29 additions and 8 deletions

View File

@ -193,6 +193,11 @@ export default class UserService {
} }
public async logout(platform: string, tokenValue: string): Promise<any> { public async logout(platform: string, tokenValue: string): Promise<any> {
if (!platform || !tokenValue) {
this.logger.warn('Invalid logout parameters - empty platform or token');
return;
}
const authInfo = await this.getAuthInfo(); const authInfo = await this.getAuthInfo();
// Verify the token exists before attempting to remove it // Verify the token exists before attempting to remove it
@ -203,6 +208,9 @@ export default class UserService {
); );
if (!tokenExists && authInfo.token !== tokenValue) { if (!tokenExists && authInfo.token !== tokenValue) {
// Token not found, but don't throw error - user may have already logged out // Token not found, but don't throw error - user may have already logged out
this.logger.info(
`Logout attempted for non-existent token on platform: ${platform}`,
);
return; return;
} }
@ -430,6 +438,14 @@ export default class UserService {
tokenInfo: TokenInfo, tokenInfo: TokenInfo,
maxTokensPerPlatform: number = config.maxTokensPerPlatform, maxTokensPerPlatform: number = config.maxTokensPerPlatform,
): Record<string, TokenInfo[]> { ): Record<string, TokenInfo[]> {
// Validate maxTokensPerPlatform parameter
if (!Number.isInteger(maxTokensPerPlatform) || maxTokensPerPlatform < 1) {
this.logger.warn(
`Invalid maxTokensPerPlatform value: ${maxTokensPerPlatform}, using default`,
);
maxTokensPerPlatform = config.maxTokensPerPlatform;
}
const normalized = this.normalizeTokens(tokens); const normalized = this.normalizeTokens(tokens);
if (!normalized[platform]) { if (!normalized[platform]) {

View File

@ -27,15 +27,20 @@ export function isValidToken(
// Check platform-specific tokens (support both legacy string and new TokenInfo[] format) // Check platform-specific tokens (support both legacy string and new TokenInfo[] format)
const platformTokens = tokens[platform]; const platformTokens = tokens[platform];
if (platformTokens) {
// Handle null/undefined platformTokens
if (platformTokens === null || platformTokens === undefined) {
return false;
}
if (typeof platformTokens === 'string') { if (typeof platformTokens === 'string') {
// Legacy format: single string token // Legacy format: single string token
return headerToken === platformTokens; return headerToken === platformTokens;
} else if (Array.isArray(platformTokens)) { } else if (Array.isArray(platformTokens)) {
// New format: array of TokenInfo objects // New format: array of TokenInfo objects
return platformTokens.some((t: TokenInfo) => t.value === headerToken); return platformTokens.some((t: TokenInfo) => t && t.value === headerToken);
}
} }
// Unexpected type - log warning and reject
return false; return false;
} }