Files
junction/src/oauth.h
T
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

43 lines
2.0 KiB
C

/* junction/oauth.h — the OAuth-token gate on Anthropic requests.
*
* An Anthropic OAuth token (sk-ant-oat01…) is accepted only on requests that carry a
* specific system prompt; see oauth.c for the measurements. These functions put that
* prompt in place and nothing else.
*/
#ifndef JUNCTION_OAUTH_H
#define JUNCTION_OAUTH_H
#include <stddef.h>
/* The exact string the gate looks for. Exposed for tests; do not edit it. */
extern const char j_oauth_marker[];
/* 1 when S of length N is exactly the marker. */
int j_oauth_is_marker(const char *s, size_t n);
/* Does this body still need the marker?
*
* COMPAT = 0 for the native Messages API (`system` at the top level), 1 for the
* OpenAI-compatible path (`/v1/chat/completions`, system as a message).
* Returns 1 when a rewrite would change something, 0 when the body is already usable
* or is not a chat body this function understands. A body that is not JSON, or has no
* messages, is left alone (0) rather than guessed at. */
int j_oauth_needed(const char *body, size_t len, int compat);
/* Rewrite the body so the marker leads the system prompt.
*
* Returns 0 and a malloc'ed body in *out / *outlen when the body was rewritten,
* 1 when it already led with the marker (nothing written, caller sends its own body),
* -1 when the body is not a shape this can touch (same: caller sends the original).
* Distinguishing 0 from 1 is not cosmetic — a caller that used *out on every success
* would send an empty body. Check j_oauth_needed first and skip the call when it says
* no; these two then only differ for a body that changed underneath.
* On the native path the client's system prompt is kept as the following blocks.
* On the compat path, where the system content must be the marker alone, the client's
* system text becomes its own user message before the first turn.
* The caller owns *out. */
int j_oauth_body(const char *body, size_t len, int compat, char **out,
size_t *outlen);
#endif