Skip to content

Commit 5c90b95

Browse files
committed
add file: URL option to ACL onfail: syntax
1 parent 4be26d7 commit 5c90b95

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Several options support retrieving a value from request. The syntax is as follow
279279
- `host:<hostname>` to apply only for particular virtual hosts (req with `Host: hostname`)
280280
- `GET`, `POST`, etc. to filter by HTTP methods
281281
- `onfail:<URL>` redirect to URL when auth fails. can use `@param@` placeholders to solve into url-escaped values from request (ex: `@req:host@`)
282+
- use `file:` URL to serve file instead of redirection
282283
- `:` separates alternate roles (OR operation)
283284
- `+` makes all specified roles to be required (AND operation)
284285
- can be used to implement multi-factor auth

auth.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,14 @@ func (ah *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
765765
}
766766
next.ServeHTTP(w, authenticatedRequest)
767767
} else if errRedirect, ok := err.(ErrNeedAuthRedirected); ok {
768-
w.Header().Set("Location", errRedirect.RedirectTo)
769-
w.WriteHeader(http.StatusFound)
770-
w.Write([]byte(err.Error()))
768+
if strings.HasPrefix(errRedirect.RedirectTo, "file:") {
769+
w.Header().Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
770+
http.ServeFile(w, r, errRedirect.RedirectTo[5:])
771+
} else {
772+
w.Header().Set("Location", errRedirect.RedirectTo)
773+
w.WriteHeader(http.StatusFound)
774+
w.Write([]byte(err.Error()))
775+
}
771776
} else {
772777
logf(r, logLevelInfo, "auth failed: %s", err)
773778
for k := range ah.Auths {

0 commit comments

Comments
 (0)