All files / services/crawler-service-api/src/middleware auth.js

69.44% Statements 25/36
60% Branches 3/5
66.66% Functions 2/3
69.44% Lines 25/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x 1x             1x 5x 5x 5x 5x 5x     5x 1x 1x 1x 1x 1x 1x 5x 5x       5x 5x 1x 1x 1x 1x 1x 1x  
const { requireAuth: jwtRequireAuth, peekUserId } = require('../../shared/jwtAuth');
 
function getUserId(req) {
  // Authentifié par requireAuth ? → userId vérifié
  if (req.userId) return req.userId;
  // Sinon, extraction non fiable pour logs uniquement
  return peekUserId(req);
}
 
function getOrganizationId(req) {
  // Organisation courante : path `/:organizationId` (appel utilisateur) ou
  // header `x-organization-id` posé par la gateway (appel API-key).
  // Le JWT ne porte plus d'organizationId (cf. PRD nxt-012, D8).
  if (req.params?.organizationId) return req.params.organizationId;
  if (req.organizationId) return req.organizationId;
  return null;
}
 
/**
 * Authentification : valide la signature JWT, ou accepte le contexte d'organisation
 * posé par l'api-gateway pour un appel API-key (`x-organization-id`).
 * `x-config-id` n'est plus accepté (cf. spec/prd-remove-xconfig-id.md, D7/D8).
 */
function requireAuth(req, res, next) {
  if (req.headers['x-organization-id']) {
    req.authMethod = 'api-key';
    return next();
  }
  return jwtRequireAuth(req, res, next);
}
 
module.exports = {
  getUserId,
  getOrganizationId,
  requireAuth,
};