From 7e07eaa758cb0973dc38ec6361c66bb5946977ca Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Thu, 2 Jul 2026 22:36:09 +1000 Subject: [PATCH] fix: repair master build after conflicting merges (#96) ## Why `master` does not compile. Three PRs that each built individually combined badly: - #92 changed `fetchBearerToken` to return `(string, time.Duration, error)` and added `cachedBearerToken` (which hashes the challenge via `sha256Hash`). - #94 (streaming) removed the now-unused-in-that-PR `sha256Hash` helper and its `crypto/sha256` / `encoding/hex` imports. - #89 (HEAD) added `headUpstream`, which calls `fetchBearerToken` expecting two return values. Result on `master`: `internal/proxy/engine.go` fails to build (`assignment mismatch: 2 variables but fetchBearerToken returns 3 values`; `undefined: sha256Hash`). ## Changes - Re-add the `sha256Hash` helper and its `crypto/sha256` + `encoding/hex` imports. - Fix the `headUpstream` 401 path to handle `fetchBearerToken`s three return values. ## Validation - `go build ./...`, `go vet`, and `make e2e` all pass. Should merge before the other in-flight branches so they rebase onto a compiling `master`. Reviewed-on: https://git.unkin.net/unkin/artifactapi/pulls/96 Co-authored-by: Ben Vincent Co-committed-by: Ben Vincent --- internal/proxy/engine.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/proxy/engine.go b/internal/proxy/engine.go index 7774c62..7997a96 100644 --- a/internal/proxy/engine.go +++ b/internal/proxy/engine.go @@ -2,6 +2,8 @@ package proxy import ( "context" + "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -233,7 +235,7 @@ func (e *Engine) headUpstream(ctx context.Context, remote models.Remote, path st } if resp.StatusCode == http.StatusUnauthorized { resp.Body.Close() - token, terr := fetchBearerToken(ctx, resp.Header.Get("Www-Authenticate"), remote) + token, _, terr := fetchBearerToken(ctx, resp.Header.Get("Www-Authenticate"), remote) if terr == nil && token != "" { resp, err = doHead(http.Header{"Authorization": []string{"Bearer " + token}}) if err != nil { @@ -514,6 +516,11 @@ const ( bearerTokenTTLMargin = 10 * time.Second ) +func sha256Hash(data []byte) string { + h := sha256.Sum256(data) + return hex.EncodeToString(h[:]) +} + // cachedBearerToken returns a bearer token for the given challenge, reusing a // Redis-cached token for the same remote+challenge while it is still valid. func (e *Engine) cachedBearerToken(ctx context.Context, wwwAuth string, remote models.Remote) (string, error) {