Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #549 from brennie/dev/bcrypt-htpasswd
Browse files Browse the repository at this point in the history
Support bcrypt passwords in htpasswd
  • Loading branch information
jehiah authored Mar 25, 2018
2 parents 1c1db88 + 008ffae commit a94b0a8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 19 deletions.
53 changes: 45 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
[[constraint]]
name = "gopkg.in/fsnotify.v1"
version = "~1.2.0"

[[constraint]]
branch = "master"
name = "golang.org/x/crypto"
24 changes: 16 additions & 8 deletions htpasswd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"io"
"log"
"os"

"golang.org/x/crypto/bcrypt"
)

// lookup passwords in a htpasswd file
// The entries must have been created with -s for SHA encryption
// Lookup passwords in a htpasswd file
// Passwords must be generated with -B for bcrypt or -s for SHA1.

type HtpasswdFile struct {
Users map[string]string
Expand Down Expand Up @@ -47,14 +49,20 @@ func (h *HtpasswdFile) Validate(user string, password string) bool {
if !exists {
return false
}
if realPassword[:5] == "{SHA}" {

shaPrefix := realPassword[:5]
if shaPrefix == "{SHA}" {
shaValue := realPassword[5:]
d := sha1.New()
d.Write([]byte(password))
if realPassword[5:] == base64.StdEncoding.EncodeToString(d.Sum(nil)) {
return true
}
} else {
log.Printf("Invalid htpasswd entry for %s. Must be a SHA entry.", user)
return shaValue == base64.StdEncoding.EncodeToString(d.Sum(nil))
}

bcryptPrefix := realPassword[:4]
if bcryptPrefix == "$2a$" || bcryptPrefix == "$2b$" || bcryptPrefix == "$2x$" || bcryptPrefix == "$2y$" {
return bcrypt.CompareHashAndPassword([]byte(realPassword), []byte(password)) == nil
}

log.Printf("Invalid htpasswd entry for %s. Must be a SHA or bcrypt entry.", user)
return false
}
25 changes: 23 additions & 2 deletions htpasswd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@ package main

import (
"bytes"
"github.com/stretchr/testify/assert"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
)

func TestHtpasswd(t *testing.T) {
func TestSHA(t *testing.T) {
file := bytes.NewBuffer([]byte("testuser:{SHA}PaVBVZkYqAjCQCu6UBL2xgsnZhw=\n"))
h, err := NewHtpasswd(file)
assert.Equal(t, err, nil)

valid := h.Validate("testuser", "asdf")
assert.Equal(t, valid, true)
}

func TestBcrypt(t *testing.T) {
hash1, err := bcrypt.GenerateFromPassword([]byte("password"), 1)
hash2, err := bcrypt.GenerateFromPassword([]byte("top-secret"), 2)
assert.Equal(t, err, nil)

contents := fmt.Sprintf("testuser1:%s\ntestuser2:%s\n", hash1, hash2)
file := bytes.NewBuffer([]byte(contents))

h, err := NewHtpasswd(file)
assert.Equal(t, err, nil)

valid := h.Validate("testuser1", "password")
assert.Equal(t, valid, true)

valid = h.Validate("testuser2", "top-secret")
assert.Equal(t, valid, true)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
flagSet.String("client-id", "", "the OAuth Client ID: ie: \"123456.apps.googleusercontent.com\"")
flagSet.String("client-secret", "", "the OAuth Client Secret")
flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption or \"htpasswd -B\" for bcrypt encryption")
flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
flagSet.String("custom-templates-dir", "", "path to custom html templates")
flagSet.String("footer", "", "custom footer string. Use \"-\" to disable default footer.")
Expand Down

0 comments on commit a94b0a8

Please sign in to comment.