1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-28 09:08:44 +02:00
oauth2-proxy/htpasswd_test.go

39 lines
912 B
Go
Raw Normal View History

2012-12-17 21:38:33 +03:00
package main
import (
"bytes"
2018-02-16 10:14:41 +02:00
"fmt"
2012-12-17 21:38:33 +03:00
"testing"
2018-02-16 10:14:41 +02:00
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
2012-12-17 21:38:33 +03:00
)
2018-02-16 10:14:41 +02:00
func TestSHA(t *testing.T) {
2012-12-17 21:38:33 +03:00
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)
}
2018-02-16 10:14:41 +02:00
func TestBcrypt(t *testing.T) {
hash1, err := bcrypt.GenerateFromPassword([]byte("password"), 1)
2018-11-29 16:26:41 +02:00
assert.Equal(t, err, nil)
2018-02-16 10:14:41 +02:00
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)
}