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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
package digest
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"sync"
)
var _ = log.Print
// Use an htdigest file from apache's htdigest command as a source of user authentication
// As provided it will read the file once. You may invoke .Reload() if you wish to track changes,
// perhaps in response to fsnotify.
type HtdigestUserStore struct {
simpleUserStore
mutex sync.Mutex
filename string
}
// Reload the htdigest's file. If there is an error, the old data will be kept instead.
// This function is thread safe.
func (us *HtdigestUserStore) Reload(onbad BadLineHandler) error {
f, err := os.Open(us.filename)
if err != nil {
return err
}
defer f.Close()
c := csv.NewReader(f)
c.Comma = ':'
c.Comment = '#'
c.TrimLeadingSpace = true
c.FieldsPerRecord = -1 // don't check
n := map[string]string{}
for {
r, err := c.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
if len(r) != 3 {
if onbad != nil {
// sad error message, there is some wreckage of better errors in encoding/csv, but
// I'm not sure it is in use anymore.
onbad(fmt.Errorf("line is not three fields: %v", r))
}
// skip this line, it isn't three fields
continue
}
n[r[0]+":"+r[1]] = strings.TrimSpace(r[2]) // might have trailing space
}
us.mutex.Lock()
defer us.mutex.Unlock()
us.userToHA1 = n
return nil
}
// Reload the htdigest's file on a signal. If there is an error, the old data will be kept instead.
// Typically you would use syscall.SIGHUP for the value of "when"
func (us *HtdigestUserStore) ReloadOn(when os.Signal, onbad BadLineHandler) {
// this is rather common with code in htpasswd, but I don't have a common area...
c := make(chan os.Signal, 1)
signal.Notify(c, when)
go func() {
for {
_ = <-c
log.Printf("Reloading!")
us.Reload(onbad)
}
}()
}
// Create a new UserStore loaded from an Apache style htdigest file.
func NewHtdigestUserStore(filename string, onbad BadLineHandler) (*HtdigestUserStore, error) {
us := HtdigestUserStore{
simpleUserStore: simpleUserStore{userToHA1: map[string]string{}},
filename: filename,
}
if err := us.Reload(onbad); err != nil {
return nil, err
} else {
return &us, nil
}
}
|