mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 16:59:22 +02:00
config: add metrics_basic_auth option (#1917)
* config: add metrics_basic_auth option * remove println * use constant time compare
This commit is contained in:
parent
03d8ffaee2
commit
8b42eb5ebd
8 changed files with 309 additions and 170 deletions
|
@ -2,6 +2,7 @@ package httputil
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -64,3 +65,25 @@ func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
e.ErrorResponse(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// RequireBasicAuth creates a new handler that requires basic auth from the client before
|
||||
// calling the underlying handler.
|
||||
func RequireBasicAuth(handler http.Handler, username, password string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||
|
||||
u, p, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if subtle.ConstantTimeCompare([]byte(u), []byte(username)) != 1 ||
|
||||
subtle.ConstantTimeCompare([]byte(p), []byte(password)) != 1 {
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue