diff --git a/env.example b/env.example index 60d0ef115a..30cc85c340 100644 --- a/env.example +++ b/env.example @@ -66,10 +66,10 @@ AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=false # =========================================== # LICENÇA — ATIVAÇÃO AUTOMÁTICA POR E-MAIL # =========================================== -# Defina este valor com o e-mail usado no seu primeiro registro manual. -# Na inicialização, o serviço chama silenciosamente /v1/register/auto e pula -# a etapa do navegador. Cai para o fluxo manual se o e-mail ainda não estiver -# registrado no servidor de licenciamento. +# Na inicialização sem licença salva, se AUTHENTICATION_API_KEY for recusada +# como chave de licença, o serviço chama /v1/register/auto com este e-mail e +# pula a etapa do navegador. O servidor de licenciamento decide se o e-mail +# basta; se ele recusar, cai para o fluxo manual. # Deixe vazio (ou não defina) para manter o comportamento padrão (registro manual). # EVOLUTION_OPERATOR_EMAIL=operator@example.com diff --git a/src/licensing/runtime.ts b/src/licensing/runtime.ts index f99d026bc0..9a624332c6 100644 --- a/src/licensing/runtime.ts +++ b/src/licensing/runtime.ts @@ -15,7 +15,7 @@ import { NextFunction, Request, Response } from 'express'; import { activateIntegrity } from './integrity'; import { RegisterExchangeResponse, RuntimeContextSnapshot } from './model'; import { loadOrCreateInstanceID, loadRuntimeData, saveRuntimeData } from './store'; -import { postSigned, postUnsigned, readErrorMessage } from './transport'; +import { isRejectedCredential, postSigned, postUnsigned, readErrorMessage } from './transport'; const logger = new Logger('Licensing'); @@ -119,7 +119,8 @@ export function trackMessageRecv(): void { * 1. Load or create instance ID (hardware-based, persistent) * 2. If license exists in DB → activate locally (works even if licensing server is down) * 3. If no license but globalApiKey is set → try to use it as api_key - * 4. Otherwise → inactive; gate middleware will return 503 until activation + * 4. Otherwise → try EVOLUTION_OPERATOR_EMAIL auto-activation + * 5. Otherwise → inactive; gate middleware will return 503 until activation */ export async function initializeRuntime(opts: InitializeOptions = {}): Promise { const tier = opts.tier || 'evolution-api'; @@ -176,33 +177,42 @@ export async function initializeRuntime(opts: InitializeOptions = {}): Promise { + if (await tryAutoRegisterFromEnv(rc)) { + logger.info('License activated automatically via EVOLUTION_OPERATOR_EMAIL'); + return; + } + printRegistrationBanner(rc); + rc.setActive(false); +} + /** * tryAutoRegisterFromEnv attempts a silent license activation using only the - * operator email from EVOLUTION_OPERATOR_EMAIL. The customer must have completed - * at least one manual registration in the past. + * operator email from EVOLUTION_OPERATOR_EMAIL. Whether the email alone is enough + * to get a key is decided by the licensing server. * * Returns true on success. Returns false on any failure — the caller is expected * to fall back to the manual flow. Non-fatal best-effort path. diff --git a/src/licensing/transport.ts b/src/licensing/transport.ts index 9ece90db42..5923e670e1 100644 --- a/src/licensing/transport.ts +++ b/src/licensing/transport.ts @@ -38,6 +38,12 @@ export async function getUnsigned(path: string): Promise(resolveEndpoint() + path); } +/** True when the licensing server answered and refused the credential (401/403). */ +export function isRejectedCredential(err: unknown): boolean { + const status = axios.isAxiosError(err) ? err.response?.status : undefined; + return status === 401 || status === 403; +} + export function readErrorMessage(err: unknown): string { if (axios.isAxiosError(err)) { const data = err.response?.data as { message?: string; error?: string } | undefined;