-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpbasicauth.go
50 lines (41 loc) · 1.05 KB
/
httpbasicauth.go
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
45
46
47
48
49
50
package httpbasicauth
import (
"fmt"
"net/http"
)
type Checker interface {
Check(username, password string) bool
}
// SimpleCredentialMap wraps map[string]string
type SimpleCredentialMap map[string]string
func (c SimpleCredentialMap) Check(u, p string) bool {
password, ok := c[u]
if !ok {
return false
}
return password == p
}
// Handle is a middleware that wraps your handler with http basic auth functionality
func Handle(c Checker, realm string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
u, p := extractCreds(r)
if !c.Check(u, p) {
unauthorized(w, realm)
return
}
next.ServeHTTP(w, r)
},
)
}
}
func unauthorized(w http.ResponseWriter, realm string) {
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
}
func extractCreds(r *http.Request) (username, password string) {
username, password, _ = r.BasicAuth()
return
}