magpie sends a user's own Anthropic-compatible provider its key in both
x-api-key and Authorization: Bearer. Some relays answer 401 "鉴权 header,
x-api-key 和 Authorization 不可以同时存在", so every request failed.
- Provider.AuthHeader ("" both, "x-api-key", "bearer") picks which
AuthHeaders sends on the Anthropic protocol. Anthropic's own API,
Bedrock, Azure, Vertex and subscription accounts are untouched: they
send x-api-key alone (or their own) whatever it says, and BothAuth()
is false for them.
- Left at both, a 401 whose body catalog.BothAuthRefused tells as that
refusal (names both headers and says they can't be together, Chinese or
English) is asked once more with x-api-key alone, then Bearer alone
(RetryOneAuth, in the gateway's forwardOnce and the provider test's
probe; the model-list fetch through catalog.fetchOne too). The first not
answered 401/403 is saved (LearnAuthHeader), never over a pick the user
made. A request carrying the user's own Authorization/x-api-key header
is never retried; any other 401 is passed on as it came, asked once.
- The editor has a "Key header" row (Both / x-api-key /
Authorization: Bearer, the app's segmented control) while the provider
has a third-party Anthropic URL; strings in zh, zh-TW, ja, de.
- docs/subsystems/providers-accounts.md: an "Anthropic key header" row.
Run (sandbox HOME, GOCACHE/GOMODCACHE pinned, set -o pipefail):
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; GOOS=linux/windows go build.
- go test -tags nogui ./...: 47 packages ok; agent and gateway timed out
at 10m under load average ~50-98 (other sessions' test runs), so rerun
with -p 1 -timeout 60m: agent ok; gateway ok but for
TestOTelWaterfallIncludesUnbilledFallback, a load flake unrelated to
this (it reads only the first 200ms OTel export; passes 5/5 alone),
filed as #1794.
- New tests: internal/provider/authheader_test.go (official Anthropic and
Bedrock unchanged for every pick; Aloha's 401 body retried with
x-api-key and kept; Bearer-only relay learns bearer; the model list
learns; another 401 and the user's own header aren't retried; a user's
pick isn't overwritten), internal/gateway/authheader_test.go (a
/v1/messages turn through the gateway to such a relay succeeds and the
next Chat Completions turn goes with one header first).
- Broken by hand: RetryOneAuth returning at once and AuthHeaders ignoring
the pick -> TestAuthHeaderChoice, TestRetryOneAuthLearns,
TestRetryOneAuthBearer, TestOneAuthHeaderRelay FAIL; the list retry off
in live.go -> TestListLearnsOneAuth FAIL.
- GUI: provider-auth-header.test.cjs 20/20 in chromium and webkit, en and
zh, 390px wide; the editor's other tests (provider-*, nokey-editor,
editor-short-window, segs-*, vertex-editor) 439/440, the one
(provider-discovery, chromium zh) passing 26/26 alone.
Not tried: a real relay of Aloha's; the retry is built from the body the
user quoted.
107 lines
3.3 KiB
Go
107 lines
3.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// The headers a third-party Anthropic API's key goes in (Provider.AuthHeader).
|
|
const (
|
|
AuthBoth = "" // x-api-key and Authorization: Bearer, the default
|
|
AuthXAPIKey = catalog.AuthXAPIKey // x-api-key alone
|
|
AuthBearer = catalog.AuthBearer // Authorization: Bearer alone
|
|
)
|
|
|
|
// AuthChoices are the values Provider.AuthHeader takes.
|
|
var AuthChoices = []string{AuthBoth, AuthXAPIKey, AuthBearer}
|
|
|
|
// BothAuth says p's Anthropic requests carry its key in both x-api-key and
|
|
// Authorization, as they do by default to a third-party Anthropic API: the
|
|
// ones AuthHeader can narrow to one. Anthropic's, Bedrock's, Azure's and
|
|
// Vertex AI's never do, nor a signed-in account's.
|
|
func (p Provider) BothAuth() bool {
|
|
if p.Account != nil || p.Anthropic == "" {
|
|
return false
|
|
}
|
|
p.AuthHeader = AuthBoth
|
|
return len(AuthHeaders(p, Anthropic)) == 2
|
|
}
|
|
|
|
// RetryOneAuth hands on res, the answer to req, which p signed for its
|
|
// Anthropic API; or, when the vendor refused it for carrying the key in
|
|
// both x-api-key and Authorization (catalog.BothAuthRefused), the answer to
|
|
// req asked again with each alone, x-api-key first, and remembers for p the
|
|
// one it took (LearnAuthHeader). do sends a request as req was sent.
|
|
func RetryOneAuth(p Provider, req *http.Request, res *http.Response, do func(*http.Request) (*http.Response, error)) (*http.Response, error) {
|
|
if res == nil || res.StatusCode != http.StatusUnauthorized || p.AuthHeader != AuthBoth || !p.BothAuth() || req.GetBody == nil {
|
|
return res, nil
|
|
}
|
|
// the user's own Authorization or x-api-key (Headers) is theirs
|
|
if req.Header.Get("x-api-key") != p.Key || req.Header.Get("Authorization") != "Bearer "+p.Key {
|
|
return res, nil
|
|
}
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 64<<10))
|
|
res.Body.Close()
|
|
res.Body = io.NopCloser(bytes.NewReader(b))
|
|
if !catalog.BothAuthRefused(res.StatusCode, b) {
|
|
return res, nil
|
|
}
|
|
for i, only := range catalog.OneAuthOrder {
|
|
body, err := req.GetBody()
|
|
if err != nil {
|
|
return res, nil
|
|
}
|
|
again := req.Clone(req.Context())
|
|
again.Body = body
|
|
catalog.OneAuth(again.Header, only)
|
|
if i > 0 {
|
|
res.Body.Close()
|
|
}
|
|
if res, err = do(again); err != nil {
|
|
return nil, err
|
|
}
|
|
if res.StatusCode != http.StatusUnauthorized && res.StatusCode != http.StatusForbidden {
|
|
LearnAuthHeader(p.ID, only)
|
|
return res, nil
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
var learnMu sync.Mutex
|
|
|
|
// LearnAuthHeader keeps only as the header provider id's key goes in, when
|
|
// it sends both still: one the user picked is theirs.
|
|
func LearnAuthHeader(id, only string) {
|
|
learnMu.Lock()
|
|
defer learnMu.Unlock()
|
|
f, err := read()
|
|
if err != nil {
|
|
return
|
|
}
|
|
for i := range f.Providers {
|
|
if f.Providers[i].ID == id {
|
|
if f.Providers[i].AuthHeader != AuthBoth {
|
|
return
|
|
}
|
|
f.Providers[i].AuthHeader = only
|
|
_ = store(f)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// listAuth is ctx for asking p's model list at its Anthropic base: its key
|
|
// in the header AuthHeader says, and learned as RetryOneAuth learns it.
|
|
func (p Provider) listAuth(ctx context.Context) context.Context {
|
|
if !p.BothAuth() {
|
|
return ctx
|
|
}
|
|
return catalog.WithListAuth(ctx, p.AuthHeader, func(only string) { LearnAuthHeader(p.ID, only) })
|
|
}
|