-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
62 lines (51 loc) · 1.29 KB
/
handler.go
File metadata and controls
62 lines (51 loc) · 1.29 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
package basic
import (
"encoding/base64"
"errors"
"log"
"net/http"
"strings"
)
type BasicAuthenticator struct {
Validate func(username, password string) bool
Realm string
}
func NewAuthenticator(validator func(username, password string) bool, realm string) *BasicAuthenticator {
return &BasicAuthenticator{validator, realm}
}
func (a *BasicAuthenticator) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if a.auth(r) {
next.ServeHTTP(w, r)
} else {
w.Header().Set("WWW-Authenticate", `Basic realm="`+a.Realm+`"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
}
})
}
func (a *BasicAuthenticator) auth(req *http.Request) bool {
pb := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
if len(pb) != 2 {
err := errors.New("Bad auth")
log.Println("Error", err)
return false
}
if len(pb[1]) == 0 {
err := errors.New("Authentication required")
log.Println("Error", err)
return false
}
decoded, err := base64.StdEncoding.DecodeString(pb[1])
if err != nil {
log.Println("Error bad auth string")
return false
}
lp := strings.SplitN(string(decoded), ":", 2)
if len(pb) != 2 {
err := errors.New("Bad auth")
log.Println("Error", err)
return false
}
return a.Validate(lp[0], lp[1])
}