secret_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package auth
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. )
  7. func TestHashPassword(t *testing.T) {
  8. hash, err := HashPassword("correct-horse-battery")
  9. if err != nil {
  10. t.Fatalf("HashPassword 返回错误: %v", err)
  11. }
  12. if !strings.HasPrefix(hash, "$2a$12$") {
  13. t.Errorf("应为 bcrypt cost 12 哈希,实际 %q", hash)
  14. }
  15. if !passwordMatches(hash, "correct-horse-battery") {
  16. t.Error("正确密码应校验通过")
  17. }
  18. if passwordMatches(hash, "wrong-password") {
  19. t.Error("错误密码不应通过")
  20. }
  21. }
  22. func TestValidatePassword(t *testing.T) {
  23. cases := []struct {
  24. name string
  25. newPw string
  26. oldPw string
  27. wantErr bool
  28. }{
  29. {name: "太短", newPw: "short", oldPw: "x", wantErr: true},
  30. {name: "刚好12位", newPw: "123456789012", oldPw: "old", wantErr: false},
  31. {name: "与旧密码相同", newPw: "123456789012", oldPw: "123456789012", wantErr: true},
  32. {name: "中文12字", newPw: "密码密码密码密码密码密码", oldPw: "old", wantErr: false},
  33. }
  34. for _, tc := range cases {
  35. t.Run(tc.name, func(t *testing.T) {
  36. err := ValidatePassword(tc.newPw, tc.oldPw)
  37. if tc.wantErr && err == nil {
  38. t.Error("应返回错误")
  39. }
  40. if !tc.wantErr && err != nil {
  41. t.Errorf("不应返回错误: %v", err)
  42. }
  43. })
  44. }
  45. }
  46. func TestNewTotpSecret(t *testing.T) {
  47. secret, uri, err := NewTotpSecret("Haven", "kairo")
  48. if err != nil {
  49. t.Fatalf("NewTotpSecret 返回错误: %v", err)
  50. }
  51. if len(secret) != 32 {
  52. t.Errorf("20 字节 base32 无填充应为 32 字符,实际 %d (%q)", len(secret), secret)
  53. }
  54. if strings.Contains(secret, "=") {
  55. t.Errorf("secret 不应含 padding: %q", secret)
  56. }
  57. for _, want := range []string{
  58. "otpauth://totp/Haven:kairo",
  59. "secret=" + secret,
  60. "issuer=Haven",
  61. "digits=6",
  62. "period=30",
  63. } {
  64. if !strings.Contains(uri, want) {
  65. t.Errorf("otpauth URI 缺少 %q: %s", want, uri)
  66. }
  67. }
  68. }
  69. // RFC 6238 附录 B 的 SHA1 测试向量(secret 为 "12345678901234567890")。
  70. func TestTotpRFC6238Vectors(t *testing.T) {
  71. // base32("12345678901234567890")
  72. const secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
  73. cases := []struct {
  74. unix int64
  75. want string
  76. }{
  77. {unix: 59, want: "287082"},
  78. {unix: 1111111109, want: "081804"},
  79. {unix: 1111111111, want: "050471"},
  80. {unix: 1234567890, want: "005924"},
  81. {unix: 2000000000, want: "279037"},
  82. }
  83. for _, tc := range cases {
  84. at := time.Unix(tc.unix, 0).UTC()
  85. if !TotpValid(secret, tc.want, at) {
  86. t.Errorf("TOTP(%d) 应校验通过 %s", tc.unix, tc.want)
  87. }
  88. code, err := GenerateTotpCode(secret, at)
  89. if err != nil {
  90. t.Fatalf("GenerateTotpCode 返回错误: %v", err)
  91. }
  92. if code != tc.want {
  93. t.Errorf("GenerateTotpCode(%d) = %s,期望 %s", tc.unix, code, tc.want)
  94. }
  95. }
  96. }
  97. func TestTotpSkewWindow(t *testing.T) {
  98. const secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
  99. base := time.Unix(1111111109, 0).UTC()
  100. prev, _ := GenerateTotpCode(secret, base.Add(-totpPeriod))
  101. next, _ := GenerateTotpCode(secret, base.Add(totpPeriod))
  102. far, _ := GenerateTotpCode(secret, base.Add(5*totpPeriod))
  103. if !TotpValid(secret, prev, base) {
  104. t.Error("上一个时间窗的码应通过(容忍漂移)")
  105. }
  106. if !TotpValid(secret, next, base) {
  107. t.Error("下一个时间窗的码应通过(容忍漂移)")
  108. }
  109. if TotpValid(secret, far, base) {
  110. t.Error("超出漂移窗口的码不应通过")
  111. }
  112. }
  113. func TestTotpInvalidInputs(t *testing.T) {
  114. const secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
  115. now := time.Now()
  116. cases := []struct{ name, secret, code string }{
  117. {name: "码太短", secret: secret, code: "12345"},
  118. {name: "码太长", secret: secret, code: "1234567"},
  119. {name: "非数字", secret: secret, code: "abcdef"},
  120. {name: "空 secret", secret: "", code: "123456"},
  121. {name: "非法 base32", secret: "!!!!", code: "123456"},
  122. }
  123. for _, tc := range cases {
  124. t.Run(tc.name, func(t *testing.T) {
  125. if TotpValid(tc.secret, tc.code, now) {
  126. t.Error("非法输入不应通过")
  127. }
  128. })
  129. }
  130. // 前后空白应被容忍
  131. code, _ := GenerateTotpCode(secret, now)
  132. if !TotpValid(secret, " "+code+" ", now) {
  133. t.Error("码前后空白应被裁剪")
  134. }
  135. }
  136. func TestCookieConstants(t *testing.T) {
  137. // 这些是文档规定的常量,改动需要先改 docs(architecture.md 第四节)
  138. if CookieSession != "haven_session" {
  139. t.Errorf("会话 cookie 名 = %q", CookieSession)
  140. }
  141. if CookieTrustedDevice != "haven_trusted_device" {
  142. t.Errorf("授信设备 cookie 名 = %q", CookieTrustedDevice)
  143. }
  144. if SessionTTL != 7*24*time.Hour {
  145. t.Errorf("会话有效期 = %v", SessionTTL)
  146. }
  147. if TrustedDeviceTTL != 30*24*time.Hour {
  148. t.Errorf("授信设备有效期 = %v", TrustedDeviceTTL)
  149. }
  150. if PendingLoginTTL != 5*time.Minute {
  151. t.Errorf("待验证登录有效期 = %v", PendingLoginTTL)
  152. }
  153. if BcryptCost != 12 {
  154. t.Errorf("bcrypt cost = %d", BcryptCost)
  155. }
  156. }
苏ICP备2026082406号
苏公网安备 待审核