|
@@ -7,36 +7,38 @@ import (
|
|
|
|
|
|
func BasicAuth(handler http.Handler, username, password, realm string) http.Handler {
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
- if realm == "" {
|
|
|
- realm = "Please enter username and password"
|
|
|
- }
|
|
|
+ if username != "" {
|
|
|
+ if realm == "" {
|
|
|
+ realm = "Please enter username and password"
|
|
|
+ }
|
|
|
|
|
|
- u, p, ok := r.BasicAuth()
|
|
|
- if !ok {
|
|
|
- w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
- w.WriteHeader(401)
|
|
|
- if _, err := w.Write([]byte("Unauthorised\n")); err != nil {
|
|
|
- log.Printf("%s\n", err.Error())
|
|
|
+ u, p, ok := r.BasicAuth()
|
|
|
+ if !ok {
|
|
|
+ w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
+ w.WriteHeader(401)
|
|
|
+ if _, err := w.Write([]byte("Unauthorised\n")); err != nil {
|
|
|
+ log.Printf("%s\n", err.Error())
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
- if u != username {
|
|
|
- w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
- w.WriteHeader(401)
|
|
|
- if _, err := w.Write([]byte("Unauthorised\n")); err != nil {
|
|
|
- log.Printf("%s\n", err.Error())
|
|
|
+ if u != username {
|
|
|
+ w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
+ w.WriteHeader(401)
|
|
|
+ if _, err := w.Write([]byte("Unauthorised\n")); err != nil {
|
|
|
+ log.Printf("%s\n", err.Error())
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
- if p != password {
|
|
|
- w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
- w.WriteHeader(401)
|
|
|
- if _, err := w.Write([]byte("Unauthorised\n")); err != nil {
|
|
|
- log.Printf("%s\n", err.Error())
|
|
|
+ if p != password {
|
|
|
+ w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
+ w.WriteHeader(401)
|
|
|
+ if _, err := w.Write([]byte("Unauthorised\n")); err != nil {
|
|
|
+ log.Printf("%s\n", err.Error())
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
- return
|
|
|
}
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|