forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
145 lines (129 loc) · 3.21 KB
/
auth.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package htpasswd
import (
"bytes"
"crypto/sha1"
"crypto/subtle"
"encoding/base64"
"encoding/csv"
"fmt"
"github.com/abbot/go-http-auth"
"github.com/tarent/loginsrv/logging"
"golang.org/x/crypto/bcrypt"
"io"
"os"
"strings"
"sync"
"time"
)
// File is a struct to serve an individual modTime
type File struct {
name string
// Used in func reloadIfChanged to reload htpasswd file if it changed
modTime time.Time
}
// Auth is the htpassword authenticater
type Auth struct {
filenames []File
userHash map[string]string
muUserHash sync.RWMutex
}
// NewAuth creates an htpassword authenticater
func NewAuth(filenames []string) (*Auth, error) {
var htpasswdFiles []File
for _, file := range filenames {
htpasswdFiles = append(htpasswdFiles, File{name: file})
}
a := &Auth{
filenames: htpasswdFiles,
}
return a, a.parse(htpasswdFiles)
}
func (a *Auth) parse(filenames []File) error {
tmpUserHash := map[string]string{}
for _, filename := range a.filenames {
r, err := os.Open(filename.name)
if err != nil {
return err
}
fileInfo, err := os.Stat(filename.name)
if err != nil {
return err
}
filename.modTime = fileInfo.ModTime()
cr := csv.NewReader(r)
cr.Comma = ':'
cr.Comment = '#'
cr.TrimLeadingSpace = true
for {
record, err := cr.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
if len(record) != 2 {
return fmt.Errorf("password file in wrong format (%v)", filename)
}
if _, exist := tmpUserHash[record[0]]; exist {
logging.Logger.Warnf("Found duplicate entry for user: (%v)", record[0])
}
tmpUserHash[record[0]] = record[1]
}
}
a.muUserHash.Lock()
a.userHash = tmpUserHash
a.muUserHash.Unlock()
return nil
}
// Authenticate the user
func (a *Auth) Authenticate(username, password string) (bool, error) {
reloadIfChanged(a)
a.muUserHash.RLock()
defer a.muUserHash.RUnlock()
if hash, exist := a.userHash[username]; exist {
h := []byte(hash)
p := []byte(password)
if strings.HasPrefix(hash, "$2y$") || strings.HasPrefix(hash, "$2b$") {
matchErr := bcrypt.CompareHashAndPassword(h, p)
return (matchErr == nil), nil
}
if strings.HasPrefix(hash, "{SHA}") {
return compareSha(h, p), nil
}
if strings.HasPrefix(hash, "$apr1$") {
return compareMD5(h, p), nil
}
return false, fmt.Errorf("unknown algorithm for user %q", username)
}
return false, nil
}
// Reload htpasswd file if it changed during current run
func reloadIfChanged(a *Auth) {
for _, file := range a.filenames {
fileInfo, err := os.Stat(file.name)
if err != nil {
//On error, retain current file
break
}
currentmodTime := fileInfo.ModTime()
if currentmodTime != file.modTime {
a.parse(a.filenames)
return
}
}
}
func compareSha(hashedPassword, password []byte) bool {
d := sha1.New()
d.Write(password)
return 1 == subtle.ConstantTimeCompare(hashedPassword[5:], []byte(base64.StdEncoding.EncodeToString(d.Sum(nil))))
}
func compareMD5(hashedPassword, password []byte) bool {
parts := bytes.SplitN(hashedPassword, []byte("$"), 4)
if len(parts) != 4 {
return false
}
magic := []byte("$" + string(parts[1]) + "$")
salt := parts[2]
return 1 == subtle.ConstantTimeCompare(hashedPassword, auth.MD5Crypt(password, salt, magic))
}