Add Support For BCrypt Storage of Passwords

This commit is contained in:
Fabian Zoske
2018-03-31 12:01:00 +02:00
parent 541defc796
commit 62aea27486
2 changed files with 16 additions and 1 deletions
+14 -1
View File
@@ -11,8 +11,10 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/hacdias/webdav" "github.com/hacdias/webdav"
"golang.org/x/crypto/bcrypt"
wd "golang.org/x/net/webdav" wd "golang.org/x/net/webdav"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
@@ -213,7 +215,8 @@ func basicAuth(c *cfg) http.Handler {
return return
} }
if password != p { if !checkPassword(p, password) {
log.Println("Wrong Password for user", username)
http.Error(w, "Not authorized", 401) http.Error(w, "Not authorized", 401)
return return
} }
@@ -222,6 +225,16 @@ func basicAuth(c *cfg) http.Handler {
}) })
} }
func checkPassword(saved, input string) bool {
if strings.HasPrefix(saved, "{bcrypt}") {
savedPassword := strings.TrimPrefix(saved, "{bcrypt}")
return bcrypt.CompareHashAndPassword([]byte(savedPassword), []byte(input)) == nil
}
return saved == input
}
func main() { func main() {
flag.Parse() flag.Parse()
cfg := parseConfig() cfg := parseConfig()
+2
View File
@@ -12,6 +12,8 @@ port: 8080
users: users:
- username: admin - username: admin
password: admin password: admin
- username: encrypted
password: "{bcrypt}$2y$10$zEP6oofmXFeHaeMfBNLnP.DO8m.H.Mwhd24/TOX2MWLxAExXi4qgi"
- username: basic - username: basic
password: basic password: basic
modify: false modify: false