blob: cf1adf25b705cfe97b01c5dfdd4fbd4f7184eb19 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package system
import (
"net/http"
"github.com/ponzu-cms/ponzu/system/db"
)
// BasicAuth adds HTTP Basic Auth check for requests that should implement it
func BasicAuth(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := db.ConfigCache("backup_basic_auth_user").(string)
p := db.ConfigCache("backup_basic_auth_password").(string)
if u == "" || p == "" {
res.WriteHeader(http.StatusForbidden)
return
}
user, password, ok := req.BasicAuth()
if !ok {
res.WriteHeader(http.StatusForbidden)
return
}
if u != user || p != password {
res.WriteHeader(http.StatusUnauthorized)
return
}
next.ServeHTTP(res, req)
})
}
|