# Autenticacion por dos (1) - [generateCodeAuthenticator]

## 🧾 Descripción

Genera un **código de verificación de 6 dígitos** para un usuario y lo envía al servicio externo encargado del sistema de **autenticación de doble factor (2FA)**.

Este servicio:

1. Recibe el número de documento del usuario.
2. Genera un código aleatorio numérico de 6 dígitos.
3. Envía ese código al servicio `https://sistema.shalomcontrol.com/api/generate_code_two_factor`.
4. Devuelve la respuesta del servicio externo.

Es usado para validar la identidad del usuario durante procesos de autenticación reforzada.

---

## 🚀 Endpoint

**POST** `/generate-code-authenticator`

---

## 📥 Request Body

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22username%22%3A-%2212345"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"username"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"12345678"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### Parámetros

<div class="_tableContainer_1rjym_1" id="bkmrk-campo-tipo-obligator"><div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1"><table class="w-fit min-w-(--thread-content-width)" data-end="1130" data-start="969"><thead data-end="1013" data-start="969"><tr data-end="1013" data-start="969"><th data-col-size="sm" data-end="977" data-start="969">Campo</th><th data-col-size="sm" data-end="984" data-start="977">Tipo</th><th data-col-size="sm" data-end="998" data-start="984">Obligatorio</th><th data-col-size="sm" data-end="1013" data-start="998">Descripción</th></tr></thead><tbody data-end="1130" data-start="1061"><tr data-end="1130" data-start="1061"><td data-col-size="sm" data-end="1074" data-start="1061">`username`</td><td data-col-size="sm" data-end="1083" data-start="1074">string</td><td data-col-size="sm" data-end="1088" data-start="1083">✔️</td><td data-col-size="sm" data-end="1130" data-start="1088">DNI, pasaporte o documento del usuario</td></tr></tbody></table>

</div></div>---

## 🔐 Seguridad

No requiere token del ERP.  
Sin embargo, el envío del código es gestionado mediante una llamada a un **servicio externo** con autenticación interna del cliente `Piero()`.

---

## 🧠 Flujo del Servicio (resumen real)

1. **Valida** que el request incluya `username`.
    
    
    - Si no existe, retorna error.
2. **Genera un código numérico aleatorio de 6 dígitos**, cada uno seleccionado de `"0123456789"`.
3. **Envía el código** al servicio:
    
    <div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
    </div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-attribute">POST</span> https://sistema.shalomcontrol.com/api/generate_code_two_factor`</div></div>Con los parámetros:
    
    <div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
    </div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-attr">document</span> = <username><span class="hljs-attr">code</span> = <<span class="hljs-number">6</span>-digit code>`</div></div>
4. **Recibe y retorna** la respuesta proporcionada por el servicio externo.
5. Si ocurre un error en la solicitud HTTP (BadResponseException), devuelve:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error al enviar al servicio"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn2"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"<detalle del error>"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>---

## 📤 Response 200 – Ejemplo

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-true%2C-%22ms"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Código generado y enviado correctamente"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>    <span class="hljs-attr">"document"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"12345678"</span><span class="hljs-punctuation">,</span>    <span class="hljs-attr">"success"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span>  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div></div>*(La estructura exacta depende del servicio externo `sistema.shalomcontrol.com`.)*

---

## ❗ Posibles Errores

### 1. Falta el número de documento

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m-1"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Enviar numero de documento"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### 2. Error del servicio externo

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m-2"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error al enviar al servicio"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn2"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"<mensaje del servidor externo>"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### 3. Error inesperado (validación interna)

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m-3"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error inesperado"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>---

## 📚 Código que genera el servicio (resumen simplificado)

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-if%28%21%24document%29-retur"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-keyword">if</span>(!<span class="hljs-variable">$document</span>) <span class="hljs-keyword">return</span> error;code = <span class="hljs-title function_ invoke__">random_numeric</span>(<span class="hljs-number">6</span>);POST https:<span class="hljs-comment">//sistema.shalomcontrol.com/api/generate_code_two_factor</span>{  document: <username>,  code: <<span class="hljs-number">6</span>-digit code>}<span class="hljs-keyword">return</span> response_from_service;`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>