fix(offers): support Claude OAuth token (citti/claude_oauth_token)

The key was provisioned as an OAuth token, not an API key. Read it from
Consul into ANTHROPIC_AUTH_TOKEN (the SDK uses it as a Bearer credential)
and add the required anthropic-beta: oauth-2025-04-20 header on the
messages calls. Credential guards now accept a key or a token. The job no
longer sets ANTHROPIC_API_KEY, since even an empty value would shadow the
token.
This commit is contained in:
Henrik Jess Nielsen
2026-10-30 11:46:57 +01:00
parent 06656ffd4f
commit 1dec3b7bd8
2 changed files with 27 additions and 13 deletions

27
app.py
View File

@@ -300,13 +300,25 @@ def fetch_offers():
'offers_total': total, 'translated': translated, 'matched': matched} 'offers_total': total, 'translated': translated, 'matched': matched}
def _claude_mode():
"""'key' for an API key, 'oauth' for a Bearer auth token, else None."""
if os.environ.get("ANTHROPIC_API_KEY"):
return "key"
if os.environ.get("ANTHROPIC_AUTH_TOKEN"):
return "oauth"
return None
def _claude_json(client, model, max_tokens, prompt): def _claude_json(client, model, max_tokens, prompt):
"""One Claude call returning text. Thinking is disabled (these are mechanical """One Claude call returning text. Thinking is disabled (these are mechanical
JSON tasks and thinking would eat the token budget); if a model rejects the JSON tasks and thinking would eat the token budget); if a model rejects the
disabled flag we retry without it, and the large max_tokens still fits the disabled flag we retry without it, and the large max_tokens still fits the
output alongside thinking.""" output alongside thinking. OAuth tokens require the oauth beta header on
/v1/messages, so add it when authenticating with a token."""
extra = {"anthropic-beta": "oauth-2025-04-20"} if _claude_mode() == "oauth" else {}
kwargs = dict(model=model, max_tokens=max_tokens, kwargs = dict(model=model, max_tokens=max_tokens,
messages=[{"role": "user", "content": prompt}]) messages=[{"role": "user", "content": prompt}],
extra_headers=extra)
try: try:
msg = client.messages.create(thinking={"type": "disabled"}, **kwargs) msg = client.messages.create(thinking={"type": "disabled"}, **kwargs)
except Exception as e: except Exception as e:
@@ -338,9 +350,10 @@ def _translate_batch(client, headings):
def translate_missing(): def translate_missing():
"""Translate offer headings that have no Danish cache entry yet. Needs """Translate offer headings that have no Danish cache entry yet. Needs a
ANTHROPIC_API_KEY (as in BlaaAi); without it, headings stay German.""" Claude credential (ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN); without one,
if not os.environ.get("ANTHROPIC_API_KEY"): headings stay German."""
if not _claude_mode():
return 0 return 0
with get_db() as c: with get_db() as c:
todo = [r['heading'] for r in c.execute( todo = [r['heading'] for r in c.execute(
@@ -387,9 +400,9 @@ def attach_da(tmap, *offer_lists):
def match_offers_ai(): def match_offers_ai():
"""Ask Claude which offer (if any) is the same product as each fixed item. """Ask Claude which offer (if any) is the same product as each fixed item.
Stores results in offer_matches. Needs ANTHROPIC_API_KEY; on no key or Stores results in offer_matches. Needs a Claude credential; on none or
failure the heuristic matcher remains the fallback.""" failure the heuristic matcher remains the fallback."""
if not os.environ.get("ANTHROPIC_API_KEY"): if not _claude_mode():
return 0 return 0
with get_db() as c: with get_db() as c:
offers = [dict(r) for r in c.execute( offers = [dict(r) for r in c.execute(

View File

@@ -97,14 +97,15 @@ job "citti" {
HOST = "0.0.0.0" HOST = "0.0.0.0"
} }
# Claude key for DA translation + AI offer matching (same account as # Claude OAuth token for DA translation + AI offer matching. Set as
# BlaaAi). Use citti's own Consul key if set, else fall back to the # ANTHROPIC_AUTH_TOKEN (the SDK reads it as a Bearer token); the app adds
# existing blaaai/anthropic_api_key. `or` keeps it empty (not a deploy # the required anthropic-beta: oauth-2025-04-20 header. We deliberately do
# failure) if neither exists — offers then stay German with heuristic # NOT set ANTHROPIC_API_KEY — even an empty value would shadow the token.
# matching. # keyOrDefault keeps it empty (not a deploy failure) if the KV is unset —
# offers then stay German with heuristic matching.
template { template {
data = <<EOT data = <<EOT
ANTHROPIC_API_KEY={{ or (keyOrDefault "citti/anthropic_api_key" "") (keyOrDefault "blaaai/anthropic_api_key" "") }} ANTHROPIC_AUTH_TOKEN={{ keyOrDefault "citti/claude_oauth_token" "" }}
EOT EOT
destination = "secrets/app.env" destination = "secrets/app.env"
env = true env = true