str_pad(ltrim($x, "\x00"), 32, "\x00", STR_PAD_LEFT); return $fix($r) . $fix($s); } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); exit; } if (($_SERVER['HTTP_ORIGIN'] ?? MY_ORIGIN) !== MY_ORIGIN) { http_response_code(403); exit; } $user = $_SESSION['user'] ?? null; // your login if (!$user) { http_response_code(401); exit; } $key = openssl_pkey_get_private(file_get_contents(KEY_FILE)); $d = openssl_pkey_get_details($key)['ec']; $jwk = ['kty' => 'EC', 'crv' => 'P-256', 'x' => b64u($d['x']), 'y' => b64u($d['y'])]; $now = time(); $claims = [ 'aud' => FACTURARE, 'sub' => (string)$user['id'], 'name' => $user['name'] ?? null, 'iat' => $now, 'exp' => $now + 120, 'jti' => bin2hex(random_bytes(16)), 'ori' => MY_ORIGIN, ]; $input = b64u(json_encode(['alg' => 'ES256', 'typ' => 'JWT', 'jwk' => $jwk])) . '.' . b64u(json_encode(array_filter($claims, fn($v) => $v !== null))); if (!openssl_sign($input, $der, $key, OPENSSL_ALGO_SHA256)) { http_response_code(500); exit; } header('Content-Type: text/plain'); header('Cache-Control: no-store'); echo $input . '.' . b64u(der_to_raw($der));