12// lookup passwords in a htpasswd file
13// The entries must have been created with -s for SHA encryption
15type HtpasswdFile struct {
16 Users map[string]string
19func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
20 log.Printf("using htpasswd file %s", path)
21 r, err := os.Open(path)
28func NewHtpasswd(file io.Reader) (*HtpasswdFile, error) {
29 csv_reader := csv.NewReader(file)
30 csv_reader.Comma = ':'
31 csv_reader.Comment = '#'
32 csv_reader.TrimLeadingSpace = true
34 records, err := csv_reader.ReadAll()
38 h := &HtpasswdFile{Users: make(map[string]string)}
39 for _, record := range records {
40 h.Users[record[0]] = record[1]
45func (h *HtpasswdFile) Validate(user string, password string) bool {
46 realPassword, exists := h.Users[user]
50 if realPassword[:5] == "{SHA}" {
52 d.Write([]byte(password))
53 if realPassword[5:] == base64.StdEncoding.EncodeToString(d.Sum(nil)) {
57 log.Printf("Invalid htpasswd entry for %s. Must be a SHA entry.", user)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list