stacklok / stacklok/mecatl

provider/*: support an expiring-credential source (token helper), not just a static API key

Open
#606 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
152
Forks
16
Avg merge
14h 48m
Merged PRs (30d)
536

Description

Follow-up to #573 (closed: the provider/* adapters already exist per ADR 0093). This is the one part of that issue's ask that is genuinely unbuilt, split out as requested in the closing comment.

Problem

The public provider adapters (provider/anthropic, provider/openai, provider/openaichat) authenticate with a static key baked in at construction — WithAPIKey is a fixed string. A common deployment mints short-lived credentials from a token helper, so a run can outlive its credential and lose access mid-run with no re-mint path. The only token-refresh code in the repo is root-side composition (internal/app/registry.go, bearerRoundTripper) and is narrower than what's needed: hardcoded Authorization: Bearer with no x-api-key alternative, no refresh-and-retry on auth rejection, no single-flight, and coupled to toolhivellm.DirectTokenSource. So a builder who needs expiring credentials has to rebuild this today.

What a shipped adapter should own out of the box

Support a caller-supplied credential source the adapter invokes on demand, not a value fixed at construction. That means the adapter owns the credential lifecycle:

  • fetch from the source and reuse across requests (don't re-invoke the helper per call),
  • on a provider auth rejection, refresh from the source and retry once, transparently — token expiry is a normal, expected event, not an error to surface,
  • do this safely under concurrent requests (one refresh, not a stampede),
  • carry the credential in the header style the target requires — Authorization: Bearer <token> or x-api-key: <token>, as two mutually exclusive choices selectable by the caller (which one a target accepts depends on whether it's the vendor API or a fronting gateway, not on the credential); and on a refresh-and-retry, clear the unused header so a replayed request never carries a stale one.

Static-key auth (WithAPIKey) stays the simple path; the expiring-credential path is the one a builder shouldn't have to rebuild.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the adapters in provider/anthropic, provider/openai, and provider/openaichat, then compare the existing bearerRoundTripper in internal/app/registry.go. Trace how WithAPIKey supplies credentials and define the source, header selection, refresh-and-retry, reuse, and concurrent-refresh behavior. Done means expiring credentials work across the public adapters while static-key authentication remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, authentication, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.