@@ -4,6 +4,7 @@ import (
44 "crypto/sha1"
55 "encoding/base64"
66 "encoding/csv"
7+ "io"
78 "log"
89 "os"
910)
@@ -15,26 +16,30 @@ type HtpasswdFile struct {
1516 Users map[string]string
1617}
1718
18−func NewHtpasswdFile(path string) *HtpasswdFile {
19+func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
1920 log.Printf("using htpasswd file %s", path)
2021 r, err := os.Open(path)
2122 if err != nil {
22− log.Fatalf("failed opening %v, %s", path, err.Error())
23+ return nil, err
2324 }
24− csv_reader := csv.NewReader(r)
25+ return NewHtpasswd(r)
26+}
27+
28+func NewHtpasswd(file io.Reader) (*HtpasswdFile, error) {
29+ csv_reader := csv.NewReader(file)
2530 csv_reader.Comma = ':'
2631 csv_reader.Comment = '#'
2732 csv_reader.TrimLeadingSpace = true
2833
2934 records, err := csv_reader.ReadAll()
3035 if err != nil {
31− log.Fatalf("Failed reading file %s", err.Error())
36+ return nil, err
3237 }
3338 h := &HtpasswdFile{Users: make(map[string]string)}
3439 for _, record := range records {
3540 h.Users[record[0]] = record[1]
3641 }
37− return h
42+ return h, nil
3843}
3944
4045func (h *HtpasswdFile) Validate(user string, password string) bool {