145 lines
4.8 KiB
Go
145 lines
4.8 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"`
|
||
|
|
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"`
|
||
|
|
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) {
|
||
|
|
if got := VerifyPassword(entry.Hash, contract.Password.Plaintext); got != entry.Verifies["go"] {
|
||
|
|
t.Errorf("VerifyPassword = %v, want %v", got, entry.Verifies["go"])
|
||
|
|
}
|
||
|
|
if VerifyPassword(entry.Hash, contract.Password.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, contract.Password.Plaintext)
|
||
|
|
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) || !VerifyPassword(upgraded, contract.Password.Plaintext)) {
|
||
|
|
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
|
||
|
|
}
|
||
|
|
if got := VerifyTOTP(secret, entry.Code, time.Unix(entry.Unix, 0)); got != entry.Valid {
|
||
|
|
t.Errorf("VerifyTOTP = %v, want %v", got, entry.Valid)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|