15370: Fix flaky test.
[arvados.git] / sdk / go / auth / handlers.go
index 7b1760f4b8192dd28867e2f96a7dcccf4cffbc1a..b638f7982516b431e15322adde0b55b0637c8a3f 100644 (file)
@@ -9,16 +9,17 @@ import (
        "net/http"
 )
 
-type contextKey string
-
-var contextKeyCredentials contextKey = "credentials"
+type contextKeyCredentials struct{}
 
 // LoadToken wraps the next handler, adding credentials to the request
 // context so subsequent handlers can access them efficiently via
 // CredentialsFromRequest.
 func LoadToken(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-               next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), contextKeyCredentials, CredentialsFromRequest(r))))
+               if _, ok := r.Context().Value(contextKeyCredentials{}).(*Credentials); !ok {
+                       r = r.WithContext(context.WithValue(r.Context(), contextKeyCredentials{}, CredentialsFromRequest(r)))
+               }
+               next.ServeHTTP(w, r)
        })
 }