[grade=A] P1-5, P1-6: replace 40 console.* calls in credential-manager.js + auth-manager.js
credential-manager.js: 20 console calls → log.info/warn/error tagged 'cred'. auth-manager.js: 20 console calls → log.info/error tagged 'auth'. Mixed-content strings extracted into meta payload (key, keyId, operation, etc). 1539/1539 Jest tests pass. ESLint: 4 pre-existing warnings unchanged.
This commit is contained in:
@@ -8,6 +8,7 @@ const jwt = require('jsonwebtoken');
|
||||
const crypto = require('crypto');
|
||||
const credentialManager = require('./credential-manager');
|
||||
const cryptoUtils = require('../security/crypto-utils');
|
||||
const { log } = require('../utils/logging');
|
||||
|
||||
// JWT signing secret - derived from encryption key for consistency
|
||||
const JWT_SECRET = cryptoUtils.loadOrCreateKey();
|
||||
@@ -19,7 +20,7 @@ const API_KEY_METADATA_NAMESPACE = 'auth.metadata';
|
||||
class AuthManager {
|
||||
constructor() {
|
||||
this.keyMetadataCache = new Map(); // Cache for API key metadata
|
||||
console.log('[AuthManager] Initialized');
|
||||
log.info('auth', 'Initialized');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,10 +45,10 @@ class AuthManager {
|
||||
{ expiresIn }
|
||||
);
|
||||
|
||||
console.log(`[AuthManager] Generated JWT for user: ${payload.sub}, expires in: ${expiresIn}`);
|
||||
log.info('auth', 'Generated JWT', { user: payload.sub, expiresIn });
|
||||
return token;
|
||||
} catch (error) {
|
||||
console.error('[AuthManager] JWT generation failed:', error.message);
|
||||
log.error('auth', error, { operation: 'jwtGenerate' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -68,11 +69,11 @@ class AuthManager {
|
||||
};
|
||||
} catch (error) {
|
||||
if (error.name === 'TokenExpiredError') {
|
||||
console.log('[AuthManager] JWT token expired');
|
||||
log.info('auth', 'JWT token expired');
|
||||
} else if (error.name === 'JsonWebTokenError') {
|
||||
console.log('[AuthManager] JWT token invalid:', error.message);
|
||||
log.info('auth', 'JWT token invalid', { error: error.message });
|
||||
} else {
|
||||
console.error('[AuthManager] JWT verification failed:', error.message);
|
||||
log.error('auth', error, { operation: 'jwtVerify' });
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -116,7 +117,7 @@ class AuthManager {
|
||||
// Cache metadata
|
||||
this.keyMetadataCache.set(keyId, metadata);
|
||||
|
||||
console.log(`[AuthManager] Generated API key: ${name} (${keyId})`);
|
||||
log.info('auth', 'Generated API key', { name, keyId });
|
||||
|
||||
return {
|
||||
key: apiKey,
|
||||
@@ -126,7 +127,7 @@ class AuthManager {
|
||||
createdAt: metadata.createdAt
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[AuthManager] API key generation failed:', error.message);
|
||||
log.error('auth', error, { operation: 'apiKeyGenerate' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -154,30 +155,30 @@ class AuthManager {
|
||||
// Retrieve stored hash
|
||||
const storedHash = await credentialManager.retrieve(credentialKey);
|
||||
if (!storedHash) {
|
||||
console.log(`[AuthManager] API key not found: ${keyId}`);
|
||||
log.info('auth', 'API key not found', { keyId });
|
||||
return null;
|
||||
}
|
||||
|
||||
// Verify key matches stored hash
|
||||
const providedHash = crypto.createHash('sha256').update(key).digest('hex');
|
||||
if (!crypto.timingSafeEqual(Buffer.from(storedHash), Buffer.from(providedHash))) {
|
||||
console.log(`[AuthManager] API key hash mismatch: ${keyId}`);
|
||||
log.info('auth', 'API key hash mismatch', { keyId });
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get metadata
|
||||
const metadata = await this.getKeyMetadata(keyId);
|
||||
if (!metadata) {
|
||||
console.log(`[AuthManager] API key metadata not found: ${keyId}`);
|
||||
log.info('auth', 'API key metadata not found', { keyId });
|
||||
return null;
|
||||
}
|
||||
|
||||
// Update last used timestamp (non-blocking)
|
||||
this.updateLastUsed(keyId, metadata).catch(err =>
|
||||
console.error(`[AuthManager] Failed to update lastUsed for ${keyId}:`, err.message)
|
||||
log.error('auth', err, { keyId, operation: 'updateLastUsed' })
|
||||
);
|
||||
|
||||
console.log(`[AuthManager] API key verified: ${metadata.name} (${keyId})`);
|
||||
log.info('auth', 'API key verified', { name: metadata.name, keyId });
|
||||
|
||||
return {
|
||||
keyId,
|
||||
@@ -185,7 +186,7 @@ class AuthManager {
|
||||
name: metadata.name
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[AuthManager] API key verification failed:', error.message);
|
||||
log.error('auth', error, { operation: 'apiKeyVerify' });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -205,10 +206,10 @@ class AuthManager {
|
||||
|
||||
this.keyMetadataCache.delete(keyId);
|
||||
|
||||
console.log(`[AuthManager] Revoked API key: ${keyId}`);
|
||||
log.info('auth', 'Revoked API key', { keyId });
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`[AuthManager] Failed to revoke API key ${keyId}:`, error.message);
|
||||
log.error('auth', error, { keyId, operation: 'revoke' });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -233,7 +234,7 @@ class AuthManager {
|
||||
|
||||
return keys;
|
||||
} catch (error) {
|
||||
console.error('[AuthManager] Failed to list API keys:', error.message);
|
||||
log.error('auth', error, { operation: 'listApiKeys' });
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -262,7 +263,7 @@ class AuthManager {
|
||||
|
||||
return metadata;
|
||||
} catch (error) {
|
||||
console.error(`[AuthManager] Failed to get metadata for ${keyId}:`, error.message);
|
||||
log.error('auth', error, { keyId, operation: 'getMetadata' });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -285,7 +286,7 @@ class AuthManager {
|
||||
|
||||
this.keyMetadataCache.set(keyId, updatedMetadata);
|
||||
} catch (error) {
|
||||
console.error(`[AuthManager] Failed to update lastUsed for ${keyId}:`, error.message);
|
||||
log.error('auth', error, { keyId, operation: 'updateLastUsed' });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +295,7 @@ class AuthManager {
|
||||
*/
|
||||
clearCache() {
|
||||
this.keyMetadataCache.clear();
|
||||
console.log('[AuthManager] Cache cleared');
|
||||
log.info('auth', 'Cache cleared');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user