Conn2Flow
Docs navigation

Reference / Libraries

jwt.php library

HS256 JWT tokens with versioned symmetric keys in the database, rotation and a grace period. It is not the Gestor login token.

Path in the Gestor bibliotecas/jwt.php

Generates and validates HS256 JWTs with a set of symmetric keys stored in the database, with rotation and a grace period.

Keys

The set is stored as JSON in the variaveis table (modulo='sistema', id='jwt_keys'), each key with key_id, key_secret (256 bits), created_at, status (active/expired) and expired_at. The first call to jwt_generate_token() creates the active key if there is none.

jwt_rotate_keys() expires the active key, creates a new one and deletes the expired ones whose grace period is over. The grace period is AUTH_JWT_GRACE_HOURS (default 24) hours counted from expired_at.

Usage

gestor_incluir_biblioteca('jwt');
$token = jwt_generate_token(['sub' => $userId, 'exp' => time() + 3600]);
try {
    $r = jwt_validate_token($token);          // ['status' => 'Active'|'Grace', 'payload' => [...]]
    if (($r['payload']['exp'] ?? 0) < time()) throw new Exception('Expired token.');
    if ($r['status'] === 'Grace') { /* issue a new token */ }
} catch (Exception $e) {
    // malformed, unknown kid, invalid signature or key outside the grace period
}

Other details:

  • iat is added when missing; the header carries kid with the key_id.

  • The header's alg is ignored (verification is always HMAC-SHA256), which prevents algorithm-confusion attacks.

  • The secret keys are stored in plain text in the variaveis table: anyone who can read the database (or a backup) can issue tokens.

Internal helpers (used by the functions above; rarely called directly): jwt_base64url_encode(), jwt_base64url_decode(), jwt_rotation_days(), jwt_grace_hours(), jwt_keys_load(), jwt_keys_save(), jwt_nova_chave(), jwt_get_active_key().

Functions

Generated from gestor/bibliotecas/jwt.php · 11 functions

11 of 11

jwt_generate_token

view source · line 142 ↗
jwt_generate_token(array $payload): string

Gera um token JWT assinado com a chave ativa.

Parameters

$payloadarray
Reivindicações do token.

Returns

string
Token JWT (header.payload.signature).

jwt_rotate_keys

view source · line 216 ↗
jwt_rotate_keys(): array

Rotaciona as chaves JWT: marca a ativa como expirada, gera uma nova ativa e purga as chaves expiradas que já ultrapassaram o período de carência.

Returns

array
A nova chave ativa.

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