forwardauth_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package auth
  2. import (
  3. "path/filepath"
  4. "testing"
  5. db "imuslab.com/arozos/mod/database"
  6. )
  7. // newHashOnlyAgent builds a minimal agent backed by a temporary database,
  8. // enough for the account primitives and login decision logic.
  9. func newHashOnlyAgent(t *testing.T) *AuthAgent {
  10. t.Helper()
  11. sysdb, err := db.NewDatabase(filepath.Join(t.TempDir(), "ao.db"), false)
  12. if err != nil {
  13. t.Fatalf("NewDatabase: %v", err)
  14. }
  15. if err := sysdb.NewTable("auth"); err != nil {
  16. t.Fatalf("NewTable: %v", err)
  17. }
  18. t.Cleanup(sysdb.Close)
  19. return &AuthAgent{Database: sysdb}
  20. }
  21. func TestPasswordHashPrimitives(t *testing.T) {
  22. a := newHashOnlyAgent(t)
  23. if _, err := a.GetPasswordHash("nobody"); err == nil {
  24. t.Errorf("missing user should error")
  25. }
  26. if err := a.SetPasswordHash("", Hash("x")); err == nil {
  27. t.Errorf("empty username accepted")
  28. }
  29. if err := a.SetPasswordHash("toby", ""); err == nil {
  30. t.Errorf("empty hash accepted")
  31. }
  32. if err := a.SetPasswordHash("toby", Hash("secret")); err != nil {
  33. t.Fatalf("SetPasswordHash: %v", err)
  34. }
  35. got, err := a.GetPasswordHash("toby")
  36. if err != nil || got != Hash("secret") {
  37. t.Errorf("GetPasswordHash = %q, %v", got, err)
  38. }
  39. tests := []struct {
  40. name string
  41. user string
  42. hash string
  43. want bool
  44. }{
  45. {"correct", "toby", Hash("secret"), true},
  46. {"wrong", "toby", Hash("nope"), false},
  47. {"unknown user", "alice", Hash("secret"), false},
  48. {"empty hash", "toby", "", false},
  49. }
  50. for _, tc := range tests {
  51. t.Run(tc.name, func(t *testing.T) {
  52. if got := a.ValidateUsernameAndPasswordHash(tc.user, tc.hash); got != tc.want {
  53. t.Errorf("got %v want %v", got, tc.want)
  54. }
  55. })
  56. }
  57. }
  58. func TestUserGroupPrimitives(t *testing.T) {
  59. a := newHashOnlyAgent(t)
  60. if _, err := a.GetUserGroups("nobody"); err == nil {
  61. t.Errorf("missing user should error")
  62. }
  63. if err := a.SetUserGroups("", []string{"a"}); err == nil {
  64. t.Errorf("empty username accepted")
  65. }
  66. if err := a.SetUserGroups("toby", nil); err != nil {
  67. t.Fatalf("SetUserGroups(nil): %v", err)
  68. }
  69. groups, err := a.GetUserGroups("toby")
  70. if err != nil || len(groups) != 0 {
  71. t.Errorf("nil groups should read back empty, got %v %v", groups, err)
  72. }
  73. if err := a.SetUserGroups("toby", []string{"administrator", "photographers"}); err != nil {
  74. t.Fatalf("SetUserGroups: %v", err)
  75. }
  76. groups, _ = a.GetUserGroups("toby")
  77. if len(groups) != 2 || groups[0] != "administrator" {
  78. t.Errorf("groups not stored: %v", groups)
  79. }
  80. }
  81. func TestValidateLoginForwardAuth(t *testing.T) {
  82. a := newHashOnlyAgent(t)
  83. a.SetPasswordHash("toby", Hash("local-pw"))
  84. tests := []struct {
  85. name string
  86. hook ForwardAuthHandler
  87. password string
  88. wantOK bool
  89. wantReason string
  90. }{
  91. {"no hook, local ok", nil, "local-pw", true, ""},
  92. {"no hook, local wrong", nil, "bad", false, "Invalid username or password"},
  93. {"hook undecided falls back", func(u, h string) ForwardAuthResult { return ForwardAuthResult{} }, "local-pw", true, ""},
  94. {"hook accepts", func(u, h string) ForwardAuthResult {
  95. if u == "toby" && h == Hash("remote-pw") {
  96. return ForwardAuthResult{Decided: true, Accepted: true}
  97. }
  98. return ForwardAuthResult{Decided: true, Accepted: false, Reason: "denied"}
  99. }, "remote-pw", true, ""},
  100. {"hook rejects without fallback", func(u, h string) ForwardAuthResult {
  101. return ForwardAuthResult{Decided: true, Accepted: false, Reason: "denied by origin"}
  102. }, "local-pw", false, "denied by origin"},
  103. }
  104. for _, tc := range tests {
  105. t.Run(tc.name, func(t *testing.T) {
  106. a.ForwardAuth = tc.hook
  107. ok, reason := a.validateLogin("toby", tc.password)
  108. if ok != tc.wantOK || reason != tc.wantReason {
  109. t.Errorf("validateLogin = (%v, %q) want (%v, %q)", ok, reason, tc.wantOK, tc.wantReason)
  110. }
  111. })
  112. }
  113. }
  114. func TestForwardAuthHookReceivesHashNotPassword(t *testing.T) {
  115. a := newHashOnlyAgent(t)
  116. var seen string
  117. a.ForwardAuth = func(u, h string) ForwardAuthResult {
  118. seen = h
  119. return ForwardAuthResult{Decided: true, Accepted: true}
  120. }
  121. a.validateLogin("toby", "clear-text")
  122. if seen == "clear-text" || seen != Hash("clear-text") {
  123. t.Errorf("hook must receive the SHA-512 hash, got %q", seen)
  124. }
  125. }