mock_provider.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package ldap
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/seaweedfs/seaweedfs/weed/iam/providers"
  7. )
  8. // MockLDAPProvider is a mock implementation for testing
  9. // This is a standalone mock that doesn't depend on production LDAP code
  10. type MockLDAPProvider struct {
  11. name string
  12. initialized bool
  13. TestUsers map[string]*providers.ExternalIdentity
  14. TestCredentials map[string]string // username -> password
  15. }
  16. // NewMockLDAPProvider creates a mock LDAP provider for testing
  17. func NewMockLDAPProvider(name string) *MockLDAPProvider {
  18. return &MockLDAPProvider{
  19. name: name,
  20. initialized: true, // Mock is always initialized
  21. TestUsers: make(map[string]*providers.ExternalIdentity),
  22. TestCredentials: make(map[string]string),
  23. }
  24. }
  25. // Name returns the provider name
  26. func (m *MockLDAPProvider) Name() string {
  27. return m.name
  28. }
  29. // Initialize initializes the mock provider (no-op for testing)
  30. func (m *MockLDAPProvider) Initialize(config interface{}) error {
  31. m.initialized = true
  32. return nil
  33. }
  34. // AddTestUser adds a test user with credentials
  35. func (m *MockLDAPProvider) AddTestUser(username, password string, identity *providers.ExternalIdentity) {
  36. m.TestCredentials[username] = password
  37. m.TestUsers[username] = identity
  38. }
  39. // Authenticate authenticates using test data
  40. func (m *MockLDAPProvider) Authenticate(ctx context.Context, credentials string) (*providers.ExternalIdentity, error) {
  41. if !m.initialized {
  42. return nil, fmt.Errorf("provider not initialized")
  43. }
  44. if credentials == "" {
  45. return nil, fmt.Errorf("credentials cannot be empty")
  46. }
  47. // Parse credentials (username:password format)
  48. parts := strings.SplitN(credentials, ":", 2)
  49. if len(parts) != 2 {
  50. return nil, fmt.Errorf("invalid credentials format (expected username:password)")
  51. }
  52. username, password := parts[0], parts[1]
  53. // Check test credentials
  54. expectedPassword, userExists := m.TestCredentials[username]
  55. if !userExists {
  56. return nil, fmt.Errorf("user not found")
  57. }
  58. if password != expectedPassword {
  59. return nil, fmt.Errorf("invalid credentials")
  60. }
  61. // Return test user identity
  62. if identity, exists := m.TestUsers[username]; exists {
  63. return identity, nil
  64. }
  65. return nil, fmt.Errorf("user identity not found")
  66. }
  67. // GetUserInfo returns test user info
  68. func (m *MockLDAPProvider) GetUserInfo(ctx context.Context, userID string) (*providers.ExternalIdentity, error) {
  69. if !m.initialized {
  70. return nil, fmt.Errorf("provider not initialized")
  71. }
  72. if userID == "" {
  73. return nil, fmt.Errorf("user ID cannot be empty")
  74. }
  75. // Check test users
  76. if identity, exists := m.TestUsers[userID]; exists {
  77. return identity, nil
  78. }
  79. // Return default test user if not found
  80. return &providers.ExternalIdentity{
  81. UserID: userID,
  82. Email: userID + "@test-ldap.com",
  83. DisplayName: "Test LDAP User " + userID,
  84. Groups: []string{"test-group"},
  85. Provider: m.name,
  86. }, nil
  87. }
  88. // ValidateToken validates credentials using test data
  89. func (m *MockLDAPProvider) ValidateToken(ctx context.Context, token string) (*providers.TokenClaims, error) {
  90. if !m.initialized {
  91. return nil, fmt.Errorf("provider not initialized")
  92. }
  93. if token == "" {
  94. return nil, fmt.Errorf("token cannot be empty")
  95. }
  96. // Parse credentials (username:password format)
  97. parts := strings.SplitN(token, ":", 2)
  98. if len(parts) != 2 {
  99. return nil, fmt.Errorf("invalid token format (expected username:password)")
  100. }
  101. username, password := parts[0], parts[1]
  102. // Check test credentials
  103. expectedPassword, userExists := m.TestCredentials[username]
  104. if !userExists {
  105. return nil, fmt.Errorf("user not found")
  106. }
  107. if password != expectedPassword {
  108. return nil, fmt.Errorf("invalid credentials")
  109. }
  110. // Return test claims
  111. identity := m.TestUsers[username]
  112. return &providers.TokenClaims{
  113. Subject: username,
  114. Claims: map[string]interface{}{
  115. "ldap_dn": "CN=" + username + ",DC=test,DC=com",
  116. "email": identity.Email,
  117. "name": identity.DisplayName,
  118. "groups": identity.Groups,
  119. "provider": m.name,
  120. },
  121. }, nil
  122. }
  123. // SetupDefaultTestData configures common test data
  124. func (m *MockLDAPProvider) SetupDefaultTestData() {
  125. // Add default test user
  126. m.AddTestUser("testuser", "testpass", &providers.ExternalIdentity{
  127. UserID: "testuser",
  128. Email: "testuser@ldap-test.com",
  129. DisplayName: "Test LDAP User",
  130. Groups: []string{"developers", "users"},
  131. Provider: m.name,
  132. Attributes: map[string]string{
  133. "department": "Engineering",
  134. "location": "Test City",
  135. },
  136. })
  137. // Add admin test user
  138. m.AddTestUser("admin", "adminpass", &providers.ExternalIdentity{
  139. UserID: "admin",
  140. Email: "admin@ldap-test.com",
  141. DisplayName: "LDAP Administrator",
  142. Groups: []string{"admins", "users"},
  143. Provider: m.name,
  144. Attributes: map[string]string{
  145. "department": "IT",
  146. "role": "administrator",
  147. },
  148. })
  149. // Add readonly user
  150. m.AddTestUser("readonly", "readpass", &providers.ExternalIdentity{
  151. UserID: "readonly",
  152. Email: "readonly@ldap-test.com",
  153. DisplayName: "Read Only User",
  154. Groups: []string{"readonly"},
  155. Provider: m.name,
  156. })
  157. }