docs: record auth hash alignment and validation results

This commit is contained in:
Gabriel20xx committed 2026-10-20 15:26:58 +00:00
1 parent 22887fef03
commit f7819904b9
51 files changed
+1792 -542

No files matched your search

+35 -14
View File
@@ -19,10 +19,12 @@ type authContract 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"`
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"`
@@ -32,11 +34,12 @@ type authContract struct {
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"`
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 {
@@ -71,21 +74,29 @@ 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"] {
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, contract.Password.WrongPassword) {
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, contract.Password.Plaintext)
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) || !VerifyPassword(upgraded, contract.Password.Plaintext)) {
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)
}
}
@@ -109,9 +120,19 @@ func TestContractTOTP(t *testing.T) {
if entry.Secret != nil {
secret = *entry.Secret
}
if got := VerifyTOTP(secret, entry.Code, time.Unix(entry.Unix, 0)); got != entry.Valid {
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)
}
}
})
}
}