blob: 7bb97aaf98860398f287394dbd5afc7c1cc89161 (
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
35
36
37
38
39
40
41
42
43
44
|
// admin_auth.go
package main
import (
"net/http"
"os"
)
type AdminAuth struct {
users map[string]string
}
func newAdminAuthFromEnv() *AdminAuth {
users := make(map[string]string)
u1 := os.Getenv("ADMIN_USER")
p1 := os.Getenv("ADMIN_PASS")
if u1 != "" && p1 != "" {
users[u1] = p1
}
u2 := os.Getenv("ADMIN_USER2")
p2 := os.Getenv("ADMIN_PASS2")
if u2 != "" && p2 != "" {
users[u2] = p2
}
return &AdminAuth{users: users}
}
func (a *AdminAuth) requireAdmin(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok {
w.Header().Set("WWW-Authenticate", "Basic realm=\"bridge-admin\"")
w.WriteHeader(http.StatusUnauthorized)
return
}
expected, ok := a.users[user]
if !ok || expected != pass {
w.Header().Set("WWW-Authenticate", "Basic realm=\"bridge-admin\"")
w.WriteHeader(http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
|