Files
lotherk e86c52b074 oauth: make Claude subscription tokens usable as endpoint keys
An Anthropic OAuth token (sk-ant-oat01…, e.g. CLAUDE_CODE_OAUTH_TOKEN) is
not an API key. Sent with an ordinary body, a current model answers
429 rate_limit_error "Error" — the same status and shape as an exhausted
quota, so the endpoint looks broken at random instead of misconfigured.

`oauth on` per endpoint puts Claude Code's system prompt in place, and only
for endpoints that ask for it. The client's own system prompt is kept:
on /v1/messages as further blocks after the marker (cache_control intact),
on /v1/chat/completions as its own user message before the first turn —
there the system content must be the marker alone.

Measured against api.anthropic.com, each case repeated:

  body                                  /v1/messages  /v1/chat/completions
  no system                                    429           429
  system = <marker>                            200           200
  system = [<marker>, <text>]                  200           429
  system = [<text>, <marker>]                  429           429

The gating is model-dependent: retired names such as
claude-haiku-4-5-20251001 answer without the marker and prove nothing.
claude-sonnet-5-5, claude-haiku-5-5, claude-opus-5 and claude-opus-4-8 are
gated. The marker matches byte for byte — a missing final period or a
trailing newline is worth the same as no marker at all.

Verified: full suite PASS, 574 OK, 0 failures. Against the live API through
junction with a real subscription token: 200 on claude-sonnet-5-5 for
/v1/messages (client system prompt applied, answer "Paris") and for
/v1/chat/completions; streaming with a tool call works; the same request
with `oauth off` still gets 429 upstream, which is the counter-proof that
the option is what changes the outcome. Token B in ~/toktok is dead
independently of this (403 oauth_not_allowed_for_organization).

The stub's new STUB_BODY_FILE writes request bodies verbatim; a structural
check (is the marker the first block) cannot be done from a header.
2026-11-24 10:56:16 +00:00

44 lines
1.3 KiB
C

/* tests/oauth_probe.c — run the OAuth rewrite on stdin, print the result on stdout.
*
* oauth_probe native < body.json > rewritten.json
* oauth_probe compat < body.json > rewritten.json
*
* Exit 0 = rewritten, 3 = unchanged (already usable or not a shape it touches).
* Lets a test look at the exact bytes the module produces without starting junction,
* so a broken rewrite is visible as a JSON diff and not as "the API answered 429
* again" — the failure mode this module exists for looks identical to a quota error.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/oauth.h"
int main(int argc, char **argv)
{
int compat = (argc > 1 && !strcmp(argv[1], "compat"));
size_t cap = 1 << 16, len = 0;
char *buf = malloc(cap);
if (!buf) return 2;
size_t r;
while ((r = fread(buf + len, 1, cap - len, stdin)) > 0) {
len += r;
if (len == cap) {
cap *= 2;
char *nb = realloc(buf, cap);
if (!nb) { free(buf); return 2; }
buf = nb;
}
}
char *out = NULL;
size_t out_len = 0;
int rc = j_oauth_body(buf, len, compat, &out, &out_len);
free(buf);
if (rc != 0) return rc == 1 ? 3 : 4; /* 3 = already marked, 4 = not a body we touch */
fwrite(out, 1, out_len, stdout);
free(out);
return 0;
}