user.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package handler
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "aws-sts-mock/internal/kvdb"
  7. )
  8. // UserHandler handles user management operations
  9. type UserHandler struct {
  10. db kvdb.KVDB
  11. }
  12. // NewUserHandler creates a new user handler
  13. func NewUserHandler(db kvdb.KVDB) *UserHandler {
  14. return &UserHandler{
  15. db: db,
  16. }
  17. }
  18. // CreateUser creates a new user
  19. func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
  20. if r.Method != "POST" {
  21. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  22. return
  23. }
  24. var user kvdb.User
  25. if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
  26. http.Error(w, "Invalid request body", http.StatusBadRequest)
  27. return
  28. }
  29. // Validate required fields
  30. if user.AccessKeyID == "" || user.SecretAccessKey == "" || user.AccountID == "" {
  31. http.Error(w, "Missing required fields: access_key_id, secret_access_key, account_id", http.StatusBadRequest)
  32. return
  33. }
  34. if err := h.db.CreateUser(&user); err != nil {
  35. if err == kvdb.ErrUserAlreadyExists {
  36. http.Error(w, "User already exists", http.StatusConflict)
  37. return
  38. }
  39. log.Printf("Error creating user: %v", err)
  40. http.Error(w, "Internal server error", http.StatusInternalServerError)
  41. return
  42. }
  43. log.Printf("Created user: %s (Account: %s)", user.Username, user.AccountID)
  44. w.Header().Set("Content-Type", "application/json")
  45. w.WriteHeader(http.StatusCreated)
  46. json.NewEncoder(w).Encode(map[string]string{
  47. "message": "User created successfully",
  48. "access_key_id": user.AccessKeyID,
  49. "account_id": user.AccountID,
  50. })
  51. }
  52. // GetUser retrieves a user by access key ID
  53. func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request) {
  54. if r.Method != "GET" {
  55. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  56. return
  57. }
  58. accessKeyID := r.URL.Query().Get("access_key_id")
  59. if accessKeyID == "" {
  60. http.Error(w, "Missing access_key_id query parameter", http.StatusBadRequest)
  61. return
  62. }
  63. user, err := h.db.GetUser(accessKeyID)
  64. if err != nil {
  65. if err == kvdb.ErrUserNotFound {
  66. http.Error(w, "User not found", http.StatusNotFound)
  67. return
  68. }
  69. log.Printf("Error getting user: %v", err)
  70. http.Error(w, "Internal server error", http.StatusInternalServerError)
  71. return
  72. }
  73. // Don't send secret key in response
  74. response := map[string]string{
  75. "access_key_id": user.AccessKeyID,
  76. "account_id": user.AccountID,
  77. "username": user.Username,
  78. "email": user.Email,
  79. }
  80. w.Header().Set("Content-Type", "application/json")
  81. json.NewEncoder(w).Encode(response)
  82. }
  83. // ListUsers lists all users
  84. func (h *UserHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
  85. if r.Method != "GET" {
  86. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  87. return
  88. }
  89. users, err := h.db.ListUsers()
  90. if err != nil {
  91. log.Printf("Error listing users: %v", err)
  92. http.Error(w, "Internal server error", http.StatusInternalServerError)
  93. return
  94. }
  95. // Don't send secret keys in response
  96. var response []map[string]string
  97. for _, user := range users {
  98. response = append(response, map[string]string{
  99. "access_key_id": user.AccessKeyID,
  100. "account_id": user.AccountID,
  101. "username": user.Username,
  102. "email": user.Email,
  103. })
  104. }
  105. w.Header().Set("Content-Type", "application/json")
  106. json.NewEncoder(w).Encode(response)
  107. }
  108. // DeleteUser deletes a user
  109. func (h *UserHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {
  110. if r.Method != "DELETE" {
  111. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  112. return
  113. }
  114. accessKeyID := r.URL.Query().Get("access_key_id")
  115. if accessKeyID == "" {
  116. http.Error(w, "Missing access_key_id query parameter", http.StatusBadRequest)
  117. return
  118. }
  119. if err := h.db.DeleteUser(accessKeyID); err != nil {
  120. if err == kvdb.ErrUserNotFound {
  121. http.Error(w, "User not found", http.StatusNotFound)
  122. return
  123. }
  124. log.Printf("Error deleting user: %v", err)
  125. http.Error(w, "Internal server error", http.StatusInternalServerError)
  126. return
  127. }
  128. log.Printf("Deleted user: %s", accessKeyID)
  129. w.Header().Set("Content-Type", "application/json")
  130. json.NewEncoder(w).Encode(map[string]string{
  131. "message": "User deleted successfully",
  132. })
  133. }
  134. // UpdateUser updates a user
  135. func (h *UserHandler) UpdateUser(w http.ResponseWriter, r *http.Request) {
  136. if r.Method != "PUT" {
  137. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  138. return
  139. }
  140. var user kvdb.User
  141. if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
  142. http.Error(w, "Invalid request body", http.StatusBadRequest)
  143. return
  144. }
  145. if user.AccessKeyID == "" {
  146. http.Error(w, "Missing access_key_id", http.StatusBadRequest)
  147. return
  148. }
  149. if err := h.db.UpdateUser(&user); err != nil {
  150. if err == kvdb.ErrUserNotFound {
  151. http.Error(w, "User not found", http.StatusNotFound)
  152. return
  153. }
  154. log.Printf("Error updating user: %v", err)
  155. http.Error(w, "Internal server error", http.StatusInternalServerError)
  156. return
  157. }
  158. log.Printf("Updated user: %s", user.AccessKeyID)
  159. w.Header().Set("Content-Type", "application/json")
  160. json.NewEncoder(w).Encode(map[string]string{
  161. "message": "User updated successfully",
  162. })
  163. }