-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic_authenticator.go
74 lines (61 loc) · 1.94 KB
/
basic_authenticator.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
65
66
67
68
69
70
71
72
73
74
package main
import (
"errors"
"fmt"
"net/http"
"strings"
)
var (
ErrInvalidUserOrPassword = errors.New("incorrect username or password")
ErrFailedHttpBasicAuth = errors.New("failed http basic auth, missing or malformed Authorization headers?")
)
// BasicAuthenticator uses HTTP Basic Auth to check whether the incoming request
// is allowed access.
type BasicAuthenticator struct {
// mapping of user to password
userToPassword map[string]string
}
func (a *BasicAuthenticator) AuthenticateRequest(r *http.Request) error {
user, pass, ok := r.BasicAuth()
if !ok {
return ErrFailedHttpBasicAuth
}
password, userPresent := a.userToPassword[user]
if !userPresent || password != pass {
return ErrInvalidUserOrPassword
}
return nil
}
func NewBasicAuthenticator(userToPassword map[string]string) (*BasicAuthenticator, error) {
if len(userToPassword) == 0 {
return nil, errors.New("provided an empty userToPassword map")
}
return &BasicAuthenticator{
userToPassword: userToPassword,
}, nil
}
// parseAllowedUsersString is used to convert a user1:password1,user2:password2
// string into a mapping of users to passwords
func parseAllowedUsersString(userString string) (map[string]string, error) {
if userString == "" {
return nil, errors.New("empty userString provided")
}
userToPassword := map[string]string{}
pairs := strings.Split(userString, ",")
for _, pair := range pairs {
userPassword := strings.Split(pair, ":")
if len(userPassword) != 2 {
return nil, fmt.Errorf("could not parse the following user pair: %s", pair)
}
user, password := strings.TrimSpace(userPassword[0]), strings.TrimSpace(userPassword[1])
userToPassword[user] = password
}
return userToPassword, nil
}
func NewBasicAuthenticatorFromConfigString(allowedUserString string) (*BasicAuthenticator, error) {
userToPassword, err := parseAllowedUsersString(allowedUserString)
if err != nil {
return nil, err
}
return NewBasicAuthenticator(userToPassword)
}