/
authenticator.go
56 lines (49 loc) · 1.69 KB
/
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
package server
import (
"fmt"
"io/ioutil"
"strings"
"github.com/pkg/errors"
)
// Authenticator contains credentials.
// Implements https://godoc.org/github.com/goftp/server#Auth
type Authenticator struct {
credentials map[string]string
}
// AuthenticatorFromFile returns an Authenticator with credentials parsed from the given file path.
// The file must contain one credential pair per line where username and password is separated by a `:`.
func AuthenticatorFromFile(path string) (Authenticator, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return Authenticator{}, errors.Wrapf(err, "Failed to read %q", path)
}
return AuthenticatorFromString(string(raw))
}
// AuthenticatorFromString returns an Authenticator whose credentials where parsed from the given string.
// The contents must contain one credential pair per line where username and password is separated by a `:`.
func AuthenticatorFromString(contents string) (Authenticator, error) {
auth := Authenticator{make(map[string]string)}
lines := strings.Split(contents, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) > 0 {
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
auth.credentials[parts[0]] = parts[1]
}
}
}
if len(auth.credentials) == 0 {
return auth, fmt.Errorf("No credentials found")
}
return auth, nil
}
// CheckPasswd returns `true` if username and password was found in the credentials store.
func (c Authenticator) CheckPasswd(username, password string) (bool, error) {
for user, pass := range c.credentials {
if username == user && password == pass {
return true, nil
}
}
return false, fmt.Errorf("Unknown credentials: %q:%q", username, password)
}