A key usage query that takes only "Authorization: Bearer <key>"
answered magpie's Check balance with {"code":"INVALID_AUTH_HEADER",
"message":"Authorization header format must be 'Bearer {token}'"}. Two
ways got there:
- a header written as the vendor's own error names it, "Authorization:
Bearer {token}", went with "{token}" in it as written: only {key},
{apiKey} and {api_key} were put in;
- the key pasted into the Account balance (token) field went bare in
Authorization, the way a new-api access token goes.
The key is now put in for {token} as well, for any case of key, apikey,
api_key, api-key and token, and in {{…}} or ${…}. A balance token that
is the provider's own key (with or without "Bearer ") is taken as no
token, so the query is asked as a chat request is, with the key as a
Bearer. A token of the account's own still goes as it is. Without a
header or a token the key already went as a Bearer.
Run:
- TestBalanceKeyAsBearer: an httptest server that answers only
"Bearer <key>"; no header, Bearer {token}/{key}/{API_KEY}/{{apikey}}/
${API_KEY}, the key and "Bearer <key>" as the token all read $8.50;
{TOKEN} in the URL is escaped; an access token still goes bare. On the
old code it fails with the reporter's INVALID_AUTH_HEADER for {token},
{API_KEY}, {{apikey}}, ${API_KEY} and the key as token.
- go test -tags nogui ./internal/provider/ ./internal/cli/.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
Not tried: the vendor's real endpoint (a relay, not contacted). Chat
requests still send Headers exactly as typed (provider.go's Headers
doc); only the balance query puts the key in.