/
basic.go
64 lines (51 loc) · 1.37 KB
/
basic.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package basicauth
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
"github.com/urfave/negroni"
)
func getCreds(req *http.Request) (string, string) {
if req == nil {
return "", ""
}
authVal := req.Header.Get("Authorization")
authTokens := strings.Split(authVal, " ")
if len(authTokens) < 2 || authTokens[0] != "Basic" {
return "", ""
}
userpass, err := base64.StdEncoding.DecodeString(authTokens[1])
if err != nil {
return "", ""
}
userpassTokens := strings.Split(string(userpass), ":")
if len(userpassTokens) < 2 {
return "", ""
}
return userpassTokens[0], userpassTokens[1]
}
func unauthorized(res http.ResponseWriter, realm string) {
res.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
http.Error(res, "Not Authorized", http.StatusUnauthorized)
}
func BasicFunc(realm string, authfn func(string, string, *http.Request) bool) negroni.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
user, pass := getCreds(req)
if len(user) == 0 || !authfn(user, pass, req) {
unauthorized(res, realm)
return
}
next(res, req)
}
}
func BasicAuth(realm string, users map[string]string) negroni.HandlerFunc {
return BasicFunc(realm, func(user, pass string, req *http.Request) bool {
if userpass, ok := users[user]; ok {
if pass == userpass {
return true
}
}
return false
})
}