forked from TrueCloudLab/distribution
Fixed golint, gofmt warning advice.
Signed-off-by: Dave Trombley <dave.trombley@gmail.com>
This commit is contained in:
parent
ff67393b2b
commit
15bbde99c1
3 changed files with 47 additions and 35 deletions
|
@ -21,7 +21,6 @@ func TestBasicAccessController(t *testing.T) {
|
||||||
MiShil:$2y$05$0oHgwMehvoe8iAWS8I.7l.KoECXrwVaC16RPfaSCU5eVTFrATuMI2
|
MiShil:$2y$05$0oHgwMehvoe8iAWS8I.7l.KoECXrwVaC16RPfaSCU5eVTFrATuMI2
|
||||||
DeokMan:공주님`
|
DeokMan:공주님`
|
||||||
|
|
||||||
|
|
||||||
tempFile, err := ioutil.TempFile("", "htpasswd-test")
|
tempFile, err := ioutil.TempFile("", "htpasswd-test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("could not create temporary htpasswd file")
|
t.Fatal("could not create temporary htpasswd file")
|
||||||
|
@ -106,5 +105,4 @@ func TestBasicAccessController(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// AuthenticationFailureErr - a generic error message for authentication failure to be presented to agent.
|
// AuthenticationFailureErr - a generic error message for authentication failure to be presented to agent.
|
||||||
var AuthenticationFailureErr = errors.New("Bad username or password")
|
var ErrAuthenticationFailure = errors.New("Bad username or password")
|
||||||
|
|
||||||
// HTPasswd - holds a path to a system .htpasswd file and the machinery to parse it.
|
// HTPasswd - holds a path to a system .htpasswd file and the machinery to parse it.
|
||||||
type HTPasswd struct {
|
type HTPasswd struct {
|
||||||
|
@ -23,33 +23,43 @@ type HTPasswd struct {
|
||||||
|
|
||||||
// AuthType represents a particular hash function used in the htpasswd file.
|
// AuthType represents a particular hash function used in the htpasswd file.
|
||||||
type AuthType int
|
type AuthType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// PlainText - Plain-text password storage (htpasswd -p)
|
||||||
PlainText AuthType = iota
|
PlainText AuthType = iota
|
||||||
|
// SHA1 - sha hashed password storage (htpasswd -s)
|
||||||
SHA1
|
SHA1
|
||||||
|
// ApacheMD5 - apr iterated md5 hashing (htpasswd -m)
|
||||||
ApacheMD5
|
ApacheMD5
|
||||||
|
// BCrypt - BCrypt adapative password hashing (htpasswd -B)
|
||||||
BCrypt
|
BCrypt
|
||||||
|
// Crypt - System crypt() hashes. (htpasswd -d)
|
||||||
Crypt
|
Crypt
|
||||||
)
|
)
|
||||||
|
|
||||||
// String returns a text representation of the AuthType
|
// String returns a text representation of the AuthType
|
||||||
func (at AuthType) String() string {
|
func (at AuthType) String() string {
|
||||||
switch(at) {
|
switch at {
|
||||||
case PlainText: return "plaintext"
|
case PlainText:
|
||||||
case SHA1: return "sha1"
|
return "plaintext"
|
||||||
case ApacheMD5: return "md5"
|
case SHA1:
|
||||||
case BCrypt: return "bcrypt"
|
return "sha1"
|
||||||
case Crypt: return "system crypt"
|
case ApacheMD5:
|
||||||
|
return "md5"
|
||||||
|
case BCrypt:
|
||||||
|
return "bcrypt"
|
||||||
|
case Crypt:
|
||||||
|
return "system crypt"
|
||||||
}
|
}
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// NewHTPasswd - Create a new HTPasswd with the given path to .htpasswd file.
|
// NewHTPasswd - Create a new HTPasswd with the given path to .htpasswd file.
|
||||||
func NewHTPasswd(htpath string) *HTPasswd {
|
func NewHTPasswd(htpath string) *HTPasswd {
|
||||||
return &HTPasswd{path: htpath}
|
return &HTPasswd{path: htpath}
|
||||||
}
|
}
|
||||||
|
|
||||||
var bcryptPrefixRegexp *regexp.Regexp = regexp.MustCompile(`^\$2[ab]?y\$`)
|
var bcryptPrefixRegexp = regexp.MustCompile(`^\$2[ab]?y\$`)
|
||||||
|
|
||||||
// GetAuthCredentialType - Inspect an htpasswd file credential and guess the encryption algorithm used.
|
// GetAuthCredentialType - Inspect an htpasswd file credential and guess the encryption algorithm used.
|
||||||
func GetAuthCredentialType(cred string) AuthType {
|
func GetAuthCredentialType(cred string) AuthType {
|
||||||
|
@ -72,7 +82,6 @@ func GetAuthCredentialType(cred string) AuthType {
|
||||||
// AuthenticateUser - Check a given user:password credential against the receiving HTPasswd's file.
|
// AuthenticateUser - Check a given user:password credential against the receiving HTPasswd's file.
|
||||||
func (htpasswd *HTPasswd) AuthenticateUser(user string, pwd string) (bool, error) {
|
func (htpasswd *HTPasswd) AuthenticateUser(user string, pwd string) (bool, error) {
|
||||||
|
|
||||||
|
|
||||||
// Open the file.
|
// Open the file.
|
||||||
in, err := os.Open(htpasswd.path)
|
in, err := os.Open(htpasswd.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -94,34 +103,39 @@ func (htpasswd *HTPasswd) AuthenticateUser(user string, pwd string) (bool, error
|
||||||
if entry[0] == user {
|
if entry[0] == user {
|
||||||
credential := entry[1]
|
credential := entry[1]
|
||||||
credType := GetAuthCredentialType(credential)
|
credType := GetAuthCredentialType(credential)
|
||||||
switch(credType) {
|
switch credType {
|
||||||
case SHA1: {
|
case SHA1:
|
||||||
|
{
|
||||||
sha := sha1.New()
|
sha := sha1.New()
|
||||||
sha.Write([]byte(pwd))
|
sha.Write([]byte(pwd))
|
||||||
hash := base64.StdEncoding.EncodeToString(sha.Sum(nil))
|
hash := base64.StdEncoding.EncodeToString(sha.Sum(nil))
|
||||||
return entry[1][5:] == hash, nil
|
return entry[1][5:] == hash, nil
|
||||||
}
|
}
|
||||||
case ApacheMD5: {
|
case ApacheMD5:
|
||||||
|
{
|
||||||
return false, errors.New(ApacheMD5.String() + " htpasswd hash function not yet supported")
|
return false, errors.New(ApacheMD5.String() + " htpasswd hash function not yet supported")
|
||||||
}
|
}
|
||||||
case BCrypt: {
|
case BCrypt:
|
||||||
|
{
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(credential), []byte(pwd))
|
err := bcrypt.CompareHashAndPassword([]byte(credential), []byte(pwd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
case Crypt: {
|
case Crypt:
|
||||||
|
{
|
||||||
return false, errors.New(Crypt.String() + " htpasswd hash function not yet supported")
|
return false, errors.New(Crypt.String() + " htpasswd hash function not yet supported")
|
||||||
}
|
}
|
||||||
case PlainText: {
|
case PlainText:
|
||||||
|
{
|
||||||
if pwd == credential {
|
if pwd == credential {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
return false, AuthenticationFailureErr
|
return false, ErrAuthenticationFailure
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false, AuthenticationFailureErr
|
return false, ErrAuthenticationFailure
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue