Skip to content

Commit 4f8020a

Browse files
tobyamholt
authored andcommitted
basicauth: Simplify user replacement on auth failure (#2470)
* Can simply add user to replacer before Replace call. user is then also added to logs * use fmt.Errorf * fix how fmt.Errorf is called.
1 parent b295aab commit 4f8020a

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

caddyhttp/basicauth/basicauth.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
106106

107107
// Get a replacer so we can provide basic info for the authentication error.
108108
repl := httpserver.NewReplacer(r, nil, "-")
109-
errstr := repl.Replace("BasicAuth: user \"%s\" was not found or password was incorrect. {remote} {host} {uri} {proto}")
110-
111-
// Username will not exist in Replacer so provide here.
112-
err := fmt.Errorf(errstr, username)
109+
repl.Set("user", username)
110+
errstr := repl.Replace("BasicAuth: user \"{user}\" was not found or password was incorrect. {remote} {host} {uri} {proto}")
111+
err := fmt.Errorf("%s", errstr)
113112
return http.StatusUnauthorized, err
114113
}
115114

0 commit comments

Comments
 (0)