33 lines
738 B
Go
33 lines
738 B
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"git.pyer.club/kingecg/gohttpd/model"
|
||
|
)
|
||
|
|
||
|
type Middleware func(w http.ResponseWriter, r *http.Request, next func())
|
||
|
|
||
|
func BasicAuth(w http.ResponseWriter, r *http.Request, next func()) {
|
||
|
config := model.GetConfig()
|
||
|
|
||
|
if config.Admin.Username == "" || config.Admin.Password == "" {
|
||
|
next()
|
||
|
return
|
||
|
}
|
||
|
user, pass, ok := r.BasicAuth()
|
||
|
if ok && user == config.Admin.Username && pass == config.Admin.Password {
|
||
|
next()
|
||
|
} else {
|
||
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||
|
http.Error(w, "Unauthorized.", http.StatusUnauthorized)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Parse(w http.ResponseWriter, r *http.Request, next func()) {
|
||
|
if r.Method == "POST" || r.Method == "PUT" {
|
||
|
r.ParseForm()
|
||
|
}
|
||
|
next()
|
||
|
}
|