Refactor auth routes: explicit dependency injection
- Updated all auth route modules to use destructured dependencies - Added JSDoc comments for factory functions - Replaced ctx. references with direct parameter access - Updated auth/index.js to extract and pass explicit dependencies - sso-gate.js maintains session helper exports from session-handlers - All files pass syntax validation Files refactored: - routes/auth/keys.js - routes/auth/session-handlers.js - routes/auth/sso-gate.js - routes/auth/totp.js - routes/auth/index.js (orchestrator)
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
const { SESSION_TTL, APP, PLEX, TIMEOUTS, buildMediaAuth } = require('../../constants');
|
||||
const { createCache, CACHE_CONFIGS } = require('../../cache-config');
|
||||
|
||||
module.exports = function(ctx) {
|
||||
module.exports = function({ authManager, credentialManager, asyncHandler, errorResponse, log }) {
|
||||
// App session cache for auto-login
|
||||
/**
|
||||
* Auth session handlers routes factory
|
||||
* @param {Object} deps - Explicit dependencies
|
||||
* @param {Object} deps.authManager - Auth manager
|
||||
* @param {Object} deps.credentialManager - Credential manager
|
||||
* @param {Function} deps.asyncHandler - Async route handler wrapper
|
||||
* @param {Function} deps.errorResponse - Error response helper
|
||||
* @param {Object} deps.log - Logger instance
|
||||
* @returns {express.Router}
|
||||
*/
|
||||
const appSessionCache = createCache(CACHE_CONFIGS.appSessions);
|
||||
|
||||
async function getAppSession(serviceId, baseUrl, username, password) {
|
||||
@@ -36,12 +46,12 @@ module.exports = function(ctx) {
|
||||
const location = locationMatch ? locationMatch[1].trim() : '';
|
||||
if (location && !location.includes('login')) {
|
||||
appSessionCache.set(serviceId, { cookies: '__ip_session=1', exp: Date.now() + SESSION_TTL.IP_SESSION });
|
||||
ctx.log.info('auth', 'Router auto-login successful (IP-based session)', { serviceId });
|
||||
log.info('auth', 'Router auto-login successful (IP-based session)', { serviceId });
|
||||
return '__ip_session=1';
|
||||
}
|
||||
ctx.log.warn('auth', 'Router auto-login failed', { serviceId });
|
||||
log.warn('auth', 'Router auto-login failed', { serviceId });
|
||||
} catch (e) {
|
||||
ctx.log.warn('auth', 'Router auto-login error', { serviceId, error: e.message?.substring(0, 100) });
|
||||
log.warn('auth', 'Router auto-login error', { serviceId, error: e.message?.substring(0, 100) });
|
||||
}
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
return null;
|
||||
@@ -73,12 +83,12 @@ module.exports = function(ctx) {
|
||||
serverId: authData.ServerId, serverName: authData.User?.ServerName || serviceId,
|
||||
};
|
||||
appSessionCache.set(serviceId, { cookies: `token=${authData.AccessToken}`, token: authData.AccessToken, tokenData, exp: Date.now() + SESSION_TTL.TOKEN_SESSION });
|
||||
ctx.log.info('auth', 'Auto-login successful (token + userId obtained)', { serviceId });
|
||||
log.info('auth', 'Auto-login successful (token + userId obtained)', { serviceId });
|
||||
return `token=${authData.AccessToken}`;
|
||||
}
|
||||
ctx.log.warn('auth', 'Auto-login failed', { serviceId, status: authResp.status });
|
||||
log.warn('auth', 'Auto-login failed', { serviceId, status: authResp.status });
|
||||
} catch (e) {
|
||||
ctx.log.warn('auth', 'Auto-login error', { serviceId, error: e.message });
|
||||
log.warn('auth', 'Auto-login error', { serviceId, error: e.message });
|
||||
}
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
return null;
|
||||
@@ -99,12 +109,12 @@ module.exports = function(ctx) {
|
||||
const token = plexData?.user?.authToken;
|
||||
if (token) {
|
||||
appSessionCache.set(serviceId, { cookies: `plexToken=${token}`, token, exp: Date.now() + SESSION_TTL.TOKEN_SESSION });
|
||||
ctx.log.info('auth', 'Plex auto-login successful via plex.tv', { serviceId });
|
||||
log.info('auth', 'Plex auto-login successful via plex.tv', { serviceId });
|
||||
return `plexToken=${token}`;
|
||||
}
|
||||
ctx.log.warn('auth', 'Plex auto-login failed: no token in response', { serviceId, status: plexResp.status });
|
||||
log.warn('auth', 'Plex auto-login failed: no token in response', { serviceId, status: plexResp.status });
|
||||
} catch (e) {
|
||||
ctx.log.warn('auth', 'Plex auto-login error', { serviceId, error: e.message });
|
||||
log.warn('auth', 'Plex auto-login error', { serviceId, error: e.message });
|
||||
}
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
return null;
|
||||
@@ -129,11 +139,11 @@ module.exports = function(ctx) {
|
||||
if (data.token) {
|
||||
const cookies = `token=${data.token}`;
|
||||
appSessionCache.set(serviceId, { cookies, exp: Date.now() + SESSION_TTL.COOKIE_SESSION });
|
||||
ctx.log.info('auth', 'Auto-login successful (JWT token cached)', { serviceId });
|
||||
log.info('auth', 'Auto-login successful (JWT token cached)', { serviceId });
|
||||
return cookies;
|
||||
}
|
||||
} catch (e) { /* JSON parse failed */ }
|
||||
ctx.log.warn('auth', 'Auto-login: no token in response', { serviceId, status: resp.status });
|
||||
log.warn('auth', 'Auto-login: no token in response', { serviceId, status: resp.status });
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
return null;
|
||||
}
|
||||
@@ -141,7 +151,7 @@ module.exports = function(ctx) {
|
||||
if (serviceId === 'torrent') {
|
||||
const text = await resp.text();
|
||||
if (text.trim() !== 'Ok.') {
|
||||
ctx.log.warn('auth', 'Auto-login failed', { serviceId, response: text.trim() });
|
||||
log.warn('auth', 'Auto-login failed', { serviceId, response: text.trim() });
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
return null;
|
||||
}
|
||||
@@ -151,7 +161,7 @@ module.exports = function(ctx) {
|
||||
if (setCookies.length > 0) {
|
||||
const cookies = setCookies.map(c => c.split(';')[0]).join('; ');
|
||||
appSessionCache.set(serviceId, { cookies, exp: Date.now() + SESSION_TTL.COOKIE_SESSION });
|
||||
ctx.log.info('auth', 'Auto-login successful, session cached', { serviceId, cookieCount: setCookies.length });
|
||||
log.info('auth', 'Auto-login successful, session cached', { serviceId, cookieCount: setCookies.length });
|
||||
return cookies;
|
||||
}
|
||||
|
||||
@@ -159,14 +169,14 @@ module.exports = function(ctx) {
|
||||
if (rawCookie) {
|
||||
const cookies = rawCookie.split(/,(?=[^ ])/).map(c => c.split(';')[0].trim()).join('; ');
|
||||
appSessionCache.set(serviceId, { cookies, exp: Date.now() + SESSION_TTL.COOKIE_SESSION });
|
||||
ctx.log.info('auth', 'Auto-login successful (fallback), session cached', { serviceId });
|
||||
log.info('auth', 'Auto-login successful (fallback), session cached', { serviceId });
|
||||
return cookies;
|
||||
}
|
||||
|
||||
ctx.log.warn('auth', 'Auto-login: no cookies in response', { serviceId, status: resp.status });
|
||||
log.warn('auth', 'Auto-login: no cookies in response', { serviceId, status: resp.status });
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
} catch (e) {
|
||||
ctx.log.warn('auth', 'Auto-login error', { serviceId, error: e.message });
|
||||
log.warn('auth', 'Auto-login error', { serviceId, error: e.message });
|
||||
appSessionCache.set(serviceId, { failed: true, exp: Date.now() + SESSION_TTL.FAILED_LOGIN });
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user