fix(ai): say why ChatGPT refused a sign-in, and start a fresh one after any refusal

Every refusal of the code by OpenAI's token endpoint came back as "The
sign-in took too long", and the endpoint's own error was dropped without a
log line. Only invalid_grant means the code ran out or was used already; any
other refusal (invalid_client, a redirect or resource mismatch) now names
its OAuth error in the answer and in a warning with the endpoint's
description, so an admin can see what went wrong.

The server takes a sign-in once whatever it answers, but the panel only
started a fresh one after "expired", so the next try after any other
refusal used a spent sign-in. It now starts a fresh one after every refusal
the server sent; offline and a wrong address keep the one in progress, as
neither reached it.

The address the sign-in window ends on is also taken without http://, the
way the hint under the field and the address bar show it.

Closes #3196
This commit is contained in:
Maurice
2026-11-24 23:22:45 +01:00
parent eed5f75487
commit 3754518d37
5 changed files with 120 additions and 18 deletions
@@ -5,6 +5,7 @@ import type { AiConnectionRow, AiConnectionsRepository } from '../../db/reposito
import { readCappedJson } from '../../utils/cappedFetch';
import { decodeJson, encodeJson } from '../../utils/json-column';
import { safeFetch } from '../../utils/ssrfGuard';
import { logWarn } from '../audit/audit-log.logger';
import { OidcService } from '../oidc/oidc.service';
import { AI_AUTH_FLOW_TTL_MS, AiAuthFlowStore } from './ai-auth-flow.store';
import { aiRefusal, providerRefusal } from './ai-connections.constants';
@@ -64,16 +65,24 @@ const tokenResponseSchema = z.object({
});
type TokenResponse = z.infer<typeof tokenResponseSchema>;
/** The token endpoint said no, with its HTTP status and its error code. */
/** The token endpoint said no, with its HTTP status, its error code and the reason it gave. */
class TokenEndpointError extends Error {
constructor(
readonly status: number,
readonly code: string | undefined,
readonly description?: string,
) {
super(`token endpoint ${status}${code ? ` (${code})` : ''}`);
super(`token endpoint ${status}${code ? ` (${code})` : ''}${description ? `: ${description}` : ''}`);
}
}
/** The OAuth `error_description` of a refusal, cut to one log line. */
function errorDescriptionOf(body: unknown): string | undefined {
if (!body || typeof body !== 'object') return undefined;
const text = (body as Record<string, unknown>).error_description;
return typeof text === 'string' && text ? text.slice(0, 300) : undefined;
}
function randomToken(): string {
return randomBytes(32).toString('base64url');
}
@@ -348,11 +357,14 @@ export class ChatgptAuthService {
resource: RESOURCE,
});
} catch (err) {
// A code that is refused has expired or was used already: the sign-in starts over.
if (err instanceof TokenEndpointError && err.status === 400) {
throw aiRefusal('expired', 'The sign-in took too long; start it again');
if (!(err instanceof TokenEndpointError) || err.status === 0 || err.status >= 500) {
throw aiRefusal('provider_unavailable', 'ChatGPT is not responding');
}
throw aiRefusal('provider_unavailable', 'ChatGPT is not responding');
// The endpoint's own words, so an admin can tell an expired code from a client or redirect it refused.
logWarn(`[AI] ChatGPT sign-in: the token endpoint refused the code, ${err.message}`);
// Only invalid_grant means the code ran out or was used already; anything else is not the user's delay.
if (err.code === 'invalid_grant') throw aiRefusal('expired', 'The sign-in took too long; start it again');
throw aiRefusal('generic', `ChatGPT refused the sign-in (${err.code ?? `HTTP ${err.status}`})`);
}
}
@@ -407,7 +419,7 @@ export class ChatgptAuthService {
throw new TokenEndpointError(0, undefined);
}
const json = await readCappedJson<unknown>(res, MAX_TOKEN_BYTES);
if (!res.ok) throw new TokenEndpointError(res.status, errorCodeOf(json));
if (!res.ok) throw new TokenEndpointError(res.status, errorCodeOf(json), errorDescriptionOf(json));
const parsed = tokenResponseSchema.safeParse(json);
if (!parsed.success) throw new TokenEndpointError(res.status, 'unreadable_answer');
return parsed.data;