Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 00:45 UTC
caddyauth: Set authentication provider error in placeholder (#6932)
* caddyauth: Set authentication provider error in placeholder for handle_errors directive * caddyauth: Simplify error placeholder setting for authentication provider
Steffen Busch committed
Apr 16, 2025 at 00:32 UTC
5be77d07ab730e6035ec7a47fb0fe161785af35c
1 file changed
+8
-1
modules/caddyhttp/caddyauth/caddyauth.go
+8
-1
@@ -37,6 +37,10 @@ func init() {
37
// `{http.auth.user.*}` placeholders may be set for any authentication
38
// modules that provide user metadata.
39
//
40
+// In case of an error, the placeholder `{http.auth.<provider>.error}`
41
+// will be set to the error message returned by the authentication
42
+// provider.
43
+//
44
// Its API is still experimental and may be subject to change.
45
type Authentication struct {
46
// A set of authentication providers. If none are specified,
@@ -71,6 +75,7 @@ func (a *Authentication) Provision(ctx caddy.Context) error {
75
}
76
77
func (a Authentication) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
78
+ repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
79
var user User
80
var authed bool
81
var err error
@@ -80,6 +85,9 @@ func (a Authentication) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
85
if c := a.logger.Check(zapcore.ErrorLevel, "auth provider returned error"); c != nil {
86
c.Write(zap.String("provider", provName), zap.Error(err))
87
}
88
+ // Set the error from the authentication provider in a placeholder,
89
+ // so it can be used in the handle_errors directive.
90
+ repl.Set("http.auth."+provName+".error", err.Error())
91
continue
92
}
93
if authed {
@@ -90,7 +98,6 @@ func (a Authentication) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
98
return caddyhttp.Error(http.StatusUnauthorized, fmt.Errorf("not authenticated"))
99
}
100
93
- repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
101
repl.Set("http.auth.user.id", user.ID)
102
for k, v := range user.Metadata {
103
repl.Set("http.auth.user."+k, v)