Conn2Flow
Docs navigation

Reference / Libraries

2fa.php library

Second authentication factor: TOTP compatible with authenticator apps and a 6-digit code by e-mail.

Path in the Gestor bibliotecas/2fa.php

It implements the two second factors of the login: TOTP (RFC 6238, the 30-second code of Google Authenticator, Authy and similar apps) and e-mail code. The screens and the flow live in the perfil-usuario module (signin-2fa/ and its OAuth equivalent); the library only computes, validates and sends.

The data lives in the usuarios table: two_factor_enabled, two_factor_type (app or email), two_factor_secret, two_factor_email_code and two_factor_email_expire. The offered methods come from AUTH_2FA_METHOD_APP and AUTH_2FA_METHOD_EMAIL (default true) in the .env.

TOTP

gestor_incluir_biblioteca('2fa');
$secret = two_factor_generate_secret();              // 16 Base32 characters = 80 bits
$uri    = two_factor_get_qr_code($email, $secret);   // otpauth://totp/Conn2Flow:<email>?secret=…
// render $uri as a QR Code; store $secret in usuarios.two_factor_secret
$ok = two_factor_validate_code($secret, $typedCode);
  • SHA-1, 6 digits, 30 s period, with a tolerance of one period back and one forward (a code is valid for up to 90 s).

  • two_factor_generate_secret($n) accepts at least 16 characters.

  • The issuer shown in the app is always Conn2Flow, not the site name.

  • The secret is stored in plain text in the database.

E-mail code

  • two_factor_email_send_code($id_usuarios, $email) draws 6 digits, stores them valid for 5 minutes (replacing any previous code) and sends them with comunicacao_email(). The e-mail subject and text are hard-coded in Portuguese.

  • two_factor_email_validate($id_usuarios, $code) checks value and deadline and clears the code when it accepts it, preventing reuse.

perfil-usuario adds recovery codes (req-119), tried only when the second factor fails and valid once each.

Internal helpers (used by the functions above; rarely called directly): two_factor_base32_encode(), two_factor_base32_decode(), two_factor_hotp().

Functions

Generated from gestor/bibliotecas/2fa.php · 8 functions

8 of 8

two_factor_get_qr_code

view source · line 109 ↗
two_factor_get_qr_code(string $email, string $secret): string

Monta a URI otpauth:// para renderização em QR Code.

Parameters

$emailstring
E-mail / conta do usuário (rótulo da conta).
$secretstring
Segredo Base32.

Returns

string
URI otpauth.

two_factor_hotp

view source · line 126 ↗
two_factor_hotp(string $secret, int $counter): string

Calcula um código HOTP (RFC 4226) para um contador específico.

Parameters

$secretstring
Segredo Base32.
$counterint
Contador (no TOTP é floor(time()/30)).

Returns

string
Código de 6 dígitos.

two_factor_validate_code

view source · line 155 ↗
two_factor_validate_code(string $secret, string $code): bool

Valida um código TOTP informado pelo usuário, com tolerância de ±1 ciclo de 30s.

Parameters

$secretstring
Segredo Base32 do usuário.
$codestring
Código de 6 dígitos informado.

Returns

bool
true se o código for válido na janela de tempo.

two_factor_email_send_code

view source · line 178 ↗
two_factor_email_send_code(int $usuario_id, string $email): bool

Gera um código numérico de 6 dígitos, grava-o no usuário com validade de 5 minutos e o envia por e-mail HTML.

Parameters

$usuario_idint
ID do usuário (coluna id_usuarios).
$emailstring
E-mail de destino.

Returns

bool
true se o e-mail foi enviado.

two_factor_email_validate

view source · line 223 ↗
two_factor_email_validate(int $usuario_id, string $code): bool

Valida o código 2FA enviado por e-mail, conferindo valor e prazo de validade. Em caso de sucesso, limpa o código para impedir reutilização.

Parameters

$usuario_idint
ID do usuário (coluna id_usuarios).
$codestring
Código de 6 dígitos informado.

Returns

bool
true se o código for válido e estiver dentro do prazo.

The Future is Hybrid: Human + AI.

Conn2Flow is the tool. If you need the Architect to design your company's strategy using this technology...

Hire Specialized Consulting