forked from foomo/htpasswd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtpasswd.go
More file actions
168 lines (152 loc) · 4.44 KB
/
htpasswd.go
File metadata and controls
168 lines (152 loc) · 4.44 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Package htpasswd is utility package to manipulate htpasswd files. I supports\
// bcrypt and sha hashes.
package htpasswd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)
// HashedPasswords name => hash
type HashedPasswords map[string]string
// HashAlgorithm enum for hashing algorithms
type HashAlgorithm string
const (
// HashBCrypt bcrypt - recommended
HashBCrypt = "bcrypt"
// HashSHA sha5 insecure - do not use
HashSHA = "sha"
)
const (
// PasswordSeparator separates passwords from hashes
PasswordSeparator = ":"
// LineSeparator separates password records
LineSeparator = "\n"
)
// MaxHtpasswdFilesize if your htpassd file is larger than 8MB, then your are doing it wrong
const MaxHtpasswdFilesize = 8 * 1024 * 1024
// ErrNotExist is the error returned when a user does not exist.
var ErrNotExist = errors.New("user did not exist in file")
// Bytes bytes representation
func (hp HashedPasswords) Bytes() (passwordBytes []byte) {
passwordBytes = []byte{}
for name, hash := range hp {
passwordBytes = append(passwordBytes, []byte(name+PasswordSeparator+hash+LineSeparator)...)
}
return passwordBytes
}
// WriteToFile put them to a file will be overwritten or created
func (hp HashedPasswords) WriteToFile(file string) error {
return ioutil.WriteFile(file, hp.Bytes(), 0644)
}
// SetPassword set a password for a user with a hashing algo
func (hp HashedPasswords) SetPassword(name, password string, hashAlgorithm HashAlgorithm) (err error) {
if len(password) == 0 {
return errors.New("passwords must not be empty, if you want to delete a user call RemoveUser")
}
hash := ""
prefix := ""
switch hashAlgorithm {
case HashBCrypt:
hash, err = hashBcrypt(password)
case HashSHA:
hash = hashSha(password)
}
if err != nil {
return err
}
hp[name] = prefix + hash
return nil
}
// ParseHtpasswdFile load a htpasswd file
func ParseHtpasswdFile(file string) (passwords HashedPasswords, err error) {
htpasswdBytes, err := ioutil.ReadFile(file)
if err != nil {
return
}
if len(htpasswdBytes) > MaxHtpasswdFilesize {
err = errors.New("this file is too large, use a database instead")
return
}
return ParseHtpasswd(htpasswdBytes)
}
// ParseHtpasswd parse htpasswd bytes
func ParseHtpasswd(htpasswdBytes []byte) (passwords HashedPasswords, err error) {
lines := strings.Split(string(htpasswdBytes), LineSeparator)
passwords = make(map[string]string)
for lineNumber, line := range lines {
// scan lines
line = strings.Trim(line, " ")
if len(line) == 0 {
// skipping empty lines
continue
}
parts := strings.Split(line, PasswordSeparator)
if len(parts) != 2 {
err = errors.New(fmt.Sprintln("invalid line", lineNumber+1, "unexpected number of parts split by", PasswordSeparator, len(parts), "instead of 2 in\"", line, "\""))
return
}
for i, part := range parts {
parts[i] = strings.Trim(part, " ")
}
_, alreadyExists := passwords[parts[0]]
if alreadyExists {
err = errors.New("invalid htpasswords file - user " + parts[0] + " was already defined")
return
}
passwords[parts[0]] = parts[1]
}
return
}
// SetHtpasswdHash set password hash for a user
func SetHtpasswdHash(file, name, hash string) error {
passwords, err := ParseHtpasswdFile(file)
if err != nil {
return err
}
passwords[name] = hash
return passwords.WriteToFile(file)
}
// RemoveUser remove an existing user from a file, returns an error, if the user does not \
// exist in the file
func RemoveUser(file, user string) error {
passwords, err := ParseHtpasswdFile(file)
if err != nil {
return err
}
_, ok := passwords[user]
if !ok {
return ErrNotExist
}
delete(passwords, user)
return passwords.WriteToFile(file)
}
// SetPasswordHash directly set a hash for a user in a file
func SetPasswordHash(file, user, hash string) error {
if len(hash) == 0 {
return errors.New("you might want to rethink your hashing algorithm, it left you with an empty hash")
}
passwords, err := ParseHtpasswdFile(file)
if err != nil {
return err
}
passwords[user] = hash
return passwords.WriteToFile(file)
}
// SetPassword set password for a user with a given hashing algorithm
func SetPassword(file, name, password string, hashAlgorithm HashAlgorithm) error {
_, err := os.Stat(file)
passwords := HashedPasswords(map[string]string{})
if err == nil {
passwords, err = ParseHtpasswdFile(file)
if err != nil {
return err
}
}
err = passwords.SetPassword(name, password, hashAlgorithm)
if err != nil {
return err
}
return passwords.WriteToFile(file)
}