11 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
12 "golang.org/x/crypto/bcrypt"
15// htpasswdMap represents the structure of an htpasswd file.
16// Passwords must be generated with -B for bcrypt or -s for SHA1.
17type htpasswdMap struct {
18 users map[string]interface{}
21// bcryptPass is used to identify bcrypt passwords in the
25// sha1Pass os used to identify sha1 passwords in the
29// NewHTPasswdValidator constructs an httpasswd based validator from the file
31func NewHTPasswdValidator(path string) (Validator, error) {
32 r, err := os.Open(path)
34 return nil, fmt.Errorf("could not open htpasswd file: %v", err)
40// newHtpasswd consctructs an htpasswd from an io.Reader (an opened file).
41func newHtpasswd(file io.Reader) (*htpasswdMap, error) {
42 csvReader := csv.NewReader(file)
44 csvReader.Comment = '#'
45 csvReader.TrimLeadingSpace = true
47 records, err := csvReader.ReadAll()
49 return nil, fmt.Errorf("could not read htpasswd file: %v", err)
52 return createHtpasswdMap(records)
55// createHtasswdMap constructs an htpasswdMap from the given records
56func createHtpasswdMap(records [][]string) (*htpasswdMap, error) {
57 h := &htpasswdMap{users: make(map[string]interface{})}
58 for _, record := range records {
59 user, realPassword := record[0], record[1]
60 shaPrefix := realPassword[:5]
61 if shaPrefix == "{SHA}" {
62 h.users[user] = sha1Pass(realPassword[5:])
66 bcryptPrefix := realPassword[:4]
67 if bcryptPrefix == "$2a$" || bcryptPrefix == "$2b$" || bcryptPrefix == "$2x$" || bcryptPrefix == "$2y$" {
68 h.users[user] = bcryptPass(realPassword)
72 // Password is neither sha1 or bcrypt
73 // TODO(JoelSpeed): In the next breaking release, make this return an error.
74 logger.Printf("Invalid htpasswd entry for %s. Must be a SHA or bcrypt entry.", user)
79// Validate checks a users password against the htpasswd entries
80func (h *htpasswdMap) Validate(user string, password string) bool {
81 realPassword, exists := h.users[user]
86 switch real := realPassword.(type) {
89 d.Write([]byte(password))
90 return string(real) == base64.StdEncoding.EncodeToString(d.Sum(nil))
92 return bcrypt.CompareHashAndPassword([]byte(real), []byte(password)) == nil
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