Browse Source

htpasswd: standarize function names

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/4/head
Alejandro Mery 1 year ago
parent
commit
7b8b0e6700
  1. 28
      htpasswd/htpasswd.go
  2. 6
      htpasswd/htpasswd_test.go

28
htpasswd/htpasswd.go

@ -19,18 +19,18 @@ type Hasher interface {
Prefix() string
}
// ParseHtpasswdFile parses a .htpasswd file
// ParseFile parses a .htpasswd file
// and returns a Passwd type
func ParseHtpasswdFile(file string) (Passwds, error) {
func ParseFile(file string) (Passwds, error) {
htpasswdBytes, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return ParseHtpasswd(htpasswdBytes)
return Parse(htpasswdBytes)
}
// ParseHtpasswd parses a slice of bytes in htpasswd style
func ParseHtpasswd(htpasswdBytes []byte) (Passwds, error) {
// Parse parses a slice of bytes in htpasswd style
func Parse(htpasswdBytes []byte) (Passwds, error) {
lines := strings.Split(string(htpasswdBytes), "\n")
passwords := make(map[string]string)
var err error
@ -63,7 +63,7 @@ func ParseHtpasswd(htpasswdBytes []byte) (Passwds, error) {
// CreateUser creates a record in the named file with
// the named password and hash algorithm
func CreateUser(file, user, passwd string, algo Hasher) error {
pp, err := ParseHtpasswdFile(file)
pp, err := ParseFile(file)
if err != nil {
return err
}
@ -71,7 +71,7 @@ func CreateUser(file, user, passwd string, algo Hasher) error {
if err != nil {
return err
}
return pp.Write(file)
return pp.WriteFile(file)
}
// CreateUser will create a new user in the given Passwd object
@ -91,7 +91,7 @@ func (pp Passwds) CreateUser(user, passwd string, algo Hasher) error {
// UpdateUser will update the password for the named user
// in the named file
func UpdateUser(file, user, passwd string, algo Hasher) error {
pp, err := ParseHtpasswdFile(file)
pp, err := ParseFile(file)
if err != nil {
return err
}
@ -99,7 +99,7 @@ func UpdateUser(file, user, passwd string, algo Hasher) error {
if err != nil {
return err
}
return pp.Write(file)
return pp.WriteFile(file)
}
// UpdateUser will update the password for the named user
@ -118,7 +118,7 @@ func (pp Passwds) UpdateUser(user, passwd string, algo Hasher) error {
// DeleteUser deletes the named user from the named file
func DeleteUser(file, user string) error {
pp, err := ParseHtpasswdFile(file)
pp, err := ParseFile(file)
if err != nil {
return err
}
@ -127,7 +127,7 @@ func DeleteUser(file, user string) error {
return err
}
return pp.Write(file)
return pp.WriteFile(file)
}
// DeleteUser deletes the named user from the named file
@ -143,7 +143,7 @@ func (pp Passwds) DeleteUser(user string) error {
// VerifyUser will check if the given user and password are matching
// with the content of the given file
func VerifyUser(file, user, passwd string) error {
pp, err := ParseHtpasswdFile(file)
pp, err := ParseFile(file)
if err != nil {
return err
}
@ -163,8 +163,8 @@ func (pp Passwds) VerifyUser(user, passwd string) error {
return alg.Match(passwd, pp[user])
}
// Write will write the Passwd object to the given file
func (pp Passwds) Write(file string) error {
// WriteFile will write the Passwds object to the given file
func (pp Passwds) WriteFile(file string) error {
return os.WriteFile(file, pp.Bytes(), os.ModePerm)
}

6
htpasswd/htpasswd_test.go

@ -6,7 +6,7 @@ import (
)
func TestParseHtpasswd(t *testing.T) {
passwords, err := ParseHtpasswd([]byte("sha:{SHA}IRRjboXT92QSYXm8lpGPCZUvU1E=\n"))
passwords, err := Parse([]byte("sha:{SHA}IRRjboXT92QSYXm8lpGPCZUvU1E=\n"))
if err != nil {
t.Fatal(err)
}
@ -51,7 +51,7 @@ func TestVerifyPassword(t *testing.T) {
TestCreateUser(t)
}
_, err = ParseHtpasswdFile(f.Name())
_, err = ParseFile(f.Name())
if err != nil {
t.Fatal(err)
}
@ -83,7 +83,7 @@ func TestVerifyUser(t *testing.T) {
TestCreateUser(t)
}
pp, err := ParseHtpasswdFile(f.Name())
pp, err := ParseFile(f.Name())
if err != nil {
t.Fatal(err)
}

Loading…
Cancel
Save