Files

167 lines
5.4 KiB
Go

package auth
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
)
// contractFile holds the fixtures the Go, Node and Python auth helpers must agree on. The Node and
// Python suites load the same file; see the "description" field in it.
const contractFile = "../../contract/auth-contract.json"
type authContract struct {
Password struct {
Plaintext string `json:"plaintext"`
WrongPassword string `json:"wrongPassword"`
Hashes []struct {
Name string `json:"name"`
Hash string `json:"hash"`
Password string `json:"password"`
WrongPassword string `json:"wrongPassword"`
Verifies map[string]bool `json:"verifies"`
NeedsUpgrade map[string]bool `json:"needsUpgrade"`
} `json:"hashes"`
TooLongToHash struct {
Bytes int `json:"bytes"`
Accepted map[string]bool `json:"accepted"`
} `json:"tooLongToHash"`
} `json:"password"`
TOTP struct {
Secret string `json:"secret"`
Cases []struct {
Name string `json:"name"`
Unix int64 `json:"unix"`
Code string `json:"code"`
Secret *string `json:"secret"`
MatchedStep *int64 `json:"matchedStep"`
Valid bool `json:"valid"`
} `json:"cases"`
} `json:"totp"`
ClientAddress struct {
InvalidTrustedProxyLists []string `json:"invalidTrustedProxyLists"`
Cases []struct {
Name string `json:"name"`
Trusted string `json:"trusted"`
Peer string `json:"peer"`
Forwarded []string `json:"forwarded"`
Want string `json:"want"`
} `json:"cases"`
} `json:"clientAddress"`
}
func loadContract(t *testing.T) authContract {
t.Helper()
data, err := os.ReadFile(contractFile)
if os.IsNotExist(err) {
t.Skipf("%s is not part of this checkout", contractFile)
}
if err != nil {
t.Fatal(err)
}
var contract authContract
if err := json.Unmarshal(data, &contract); err != nil {
t.Fatalf("decode %s: %v", contractFile, err)
}
return contract
}
func TestContractPasswordHashes(t *testing.T) {
contract := loadContract(t)
for _, entry := range contract.Password.Hashes {
t.Run(entry.Name, func(t *testing.T) {
password := entry.Password
if password == "" {
password = contract.Password.Plaintext
}
wrongPassword := entry.WrongPassword
if wrongPassword == "" {
wrongPassword = contract.Password.WrongPassword
}
if got := VerifyPassword(entry.Hash, password); got != entry.Verifies["go"] {
t.Errorf("VerifyPassword = %v, want %v", got, entry.Verifies["go"])
}
if VerifyPassword(entry.Hash, wrongPassword) {
t.Error("VerifyPassword accepted the wrong password")
}
if want, ok := entry.NeedsUpgrade["go"]; ok {
if got := PasswordHashNeedsUpgrade(entry.Hash); got != want {
t.Errorf("PasswordHashNeedsUpgrade = %v, want %v", got, want)
}
valid, upgraded, err := VerifyAndUpgradePassword(entry.Hash, password)
if err != nil || !valid || (upgraded != "") != want {
t.Errorf("VerifyAndUpgradePassword = %v, upgraded %v, %v; want valid with an upgrade = %v", valid, upgraded != "", err, want)
}
if upgraded != "" && (!strings.HasPrefix(upgraded, bcryptScheme) && !strings.HasPrefix(upgraded, bcryptSHA256Scheme) || !VerifyPassword(upgraded, password)) {
t.Errorf("the upgraded hash %q is not a verifiable current-scheme hash", upgraded)
}
}
})
}
}
func TestContractPasswordLength(t *testing.T) {
contract := loadContract(t)
_, err := HashPassword(strings.Repeat("a", contract.Password.TooLongToHash.Bytes))
if accepted := err == nil; accepted != contract.Password.TooLongToHash.Accepted["go"] {
t.Fatalf("HashPassword of %d bytes accepted = %v, want %v (error %v)", contract.Password.TooLongToHash.Bytes, accepted, contract.Password.TooLongToHash.Accepted["go"], err)
}
}
func TestContractTOTP(t *testing.T) {
contract := loadContract(t)
for _, entry := range contract.TOTP.Cases {
t.Run(entry.Name, func(t *testing.T) {
secret := contract.TOTP.Secret
if entry.Secret != nil {
secret = *entry.Secret
}
step, got := VerifyTOTP(secret, entry.Code, time.Unix(entry.Unix, 0))
if got != entry.Valid {
t.Errorf("VerifyTOTP = %v, want %v", got, entry.Valid)
}
if got {
want := entry.Unix / 30
if entry.MatchedStep != nil {
want = *entry.MatchedStep
}
if step != want {
t.Errorf("VerifyTOTP matched step %d, want %d", step, want)
}
}
})
}
}
func TestContractClientAddress(t *testing.T) {
contract := loadContract(t)
for _, entry := range contract.ClientAddress.Cases {
t.Run(entry.Name, func(t *testing.T) {
networks, err := ParseTrustedProxies(entry.Trusted)
if err != nil {
t.Fatal(err)
}
request := httptest.NewRequest(http.MethodPost, "/auth/login", nil)
request.RemoteAddr = entry.Peer + ":4000"
if strings.Contains(entry.Peer, ":") {
request.RemoteAddr = "[" + entry.Peer + "]:4000"
}
for _, line := range entry.Forwarded {
request.Header.Add("X-Forwarded-For", line)
}
if got := (&Service{trustedProxies: networks}).clientAddress(request); got != entry.Want {
t.Errorf("clientAddress = %q, want %q", got, entry.Want)
}
})
}
for _, list := range contract.ClientAddress.InvalidTrustedProxyLists {
if _, err := ParseTrustedProxies(list); err == nil {
t.Errorf("ParseTrustedProxies(%q) accepted an invalid list", list)
}
}
}