#!/bin/sh # Render a Caddyfile picking either OAuth Bearer or x-api-key auth based on # which env var is set. OAuth wins if both are set (matches the pipe's prior # precedence). The rendered file lives under /tmp and is never written into # the image layer. # # We use Caddy's {env.X} placeholder for the actual secret so it's loaded at # runtime, never baked into the config file on disk and never printed. The # admin API is disabled (admin off) so the config can't be read back over HTTP. set -eu if [ -n "${CLAUDE_CODE_OAUTH_TOKEN:-}" ]; then # Subscription OAuth token (from `claude setup-token`). Must be sent # as Authorization: Bearer, plus the oauth-* anthropic-beta header that # tells Anthropic to resolve the Bearer as a subscription token rather # than demanding x-api-key. # # NOTE: we substitute the token into the Caddyfile at render time (shell # heredoc) instead of using Caddy's {env.X} placeholder. Empirically the # {env.X} form inside a `header_up` directive silently drops the header # when the value contains an `=` or other characters in some Caddy # versions. Literal substitution is unambiguous. The file lives on # tmpfs and isn't readable from outside the container (admin off). # `+anthropic-beta` APPENDS rather than replaces. The CLI sends its own # beta flags (interleaved-thinking, context-management, etc.) that must # be preserved; we only need to add `oauth-2025-04-20` so Anthropic # resolves the Bearer as a subscription token. AUTH_DIRECTIVE="header_up Authorization \"Bearer ${CLAUDE_CODE_OAUTH_TOKEN}\" header_up +anthropic-beta \"oauth-2025-04-20\"" echo "anthropic-proxy: injecting Authorization (OAuth bearer) + anthropic-beta oauth-2025-04-20" elif [ -n "${ANTHROPIC_API_KEY:-}" ]; then AUTH_DIRECTIVE="header_up x-api-key \"${ANTHROPIC_API_KEY}\"" echo "anthropic-proxy: injecting x-api-key (API key)" else echo "anthropic-proxy: FATAL — set CLAUDE_CODE_OAUTH_TOKEN or ANTHROPIC_API_KEY" >&2 exit 1 fi cat > /tmp/Caddyfile <