tokens_api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package httpapi
  2. import (
  3. "net/http"
  4. "github.com/gnacho/netpulse/server-go/internal/auth"
  5. )
  6. func (s *server) handleTokensList(w http.ResponseWriter, r *http.Request) {
  7. if s.tokenStore == nil {
  8. writeError(w, http.StatusServiceUnavailable, "tokens_disabled")
  9. return
  10. }
  11. user := auth.UserFromContext(r.Context())
  12. if user == nil {
  13. writeError(w, http.StatusUnauthorized, "unauthorized")
  14. return
  15. }
  16. tokens, err := s.tokenStore.List(user.ID, user.Role == "admin")
  17. if err != nil {
  18. writeError(w, http.StatusInternalServerError, "db_error", err.Error())
  19. return
  20. }
  21. type item struct {
  22. ID string `json:"id"`
  23. Name string `json:"name"`
  24. Scope string `json:"scope"`
  25. UserID int64 `json:"userId"`
  26. CreatedAt int64 `json:"createdAt"`
  27. ExpiresAt int64 `json:"expiresAt"`
  28. LastUsedAt int64 `json:"lastUsedAt"`
  29. }
  30. out := make([]item, 0, len(tokens))
  31. for _, t := range tokens {
  32. out = append(out, item{
  33. ID: t.ID, Name: t.Name, Scope: t.Scope, UserID: t.UserID,
  34. CreatedAt: t.CreatedAt, ExpiresAt: t.ExpiresAt, LastUsedAt: t.LastUsedAt,
  35. })
  36. }
  37. writeJSON(w, http.StatusOK, map[string]any{"tokens": out})
  38. }
  39. func (s *server) handleTokensCreate(w http.ResponseWriter, r *http.Request) {
  40. if s.tokenStore == nil {
  41. writeError(w, http.StatusServiceUnavailable, "tokens_disabled")
  42. return
  43. }
  44. user := auth.UserFromContext(r.Context())
  45. if user == nil {
  46. writeError(w, http.StatusUnauthorized, "unauthorized")
  47. return
  48. }
  49. var body struct {
  50. Name string `json:"name"`
  51. Scope string `json:"scope"`
  52. ExpiresIn int `json:"expiresInDays"`
  53. }
  54. if status := readJSONBody(w, r, &body); status != 0 {
  55. writeBodyError(w, status, "invalid_body", "")
  56. return
  57. }
  58. if body.Name == "" || body.Scope == "" {
  59. writeError(w, http.StatusBadRequest, "invalid_input", "name and scope required")
  60. return
  61. }
  62. id, raw, err := s.tokenStore.Create(body.Name, body.Scope, user.ID, body.ExpiresIn)
  63. if err != nil {
  64. writeError(w, http.StatusBadRequest, "create_failed", err.Error())
  65. return
  66. }
  67. writeJSON(w, http.StatusCreated, map[string]any{
  68. "id": id, "name": body.Name, "scope": body.Scope, "token": raw,
  69. })
  70. }
  71. func (s *server) handleTokensDelete(w http.ResponseWriter, r *http.Request) {
  72. if s.tokenStore == nil {
  73. writeError(w, http.StatusServiceUnavailable, "tokens_disabled")
  74. return
  75. }
  76. user := auth.UserFromContext(r.Context())
  77. if user == nil {
  78. writeError(w, http.StatusUnauthorized, "unauthorized")
  79. return
  80. }
  81. id := r.PathValue("id")
  82. if id == "" {
  83. writeError(w, http.StatusBadRequest, "invalid_input")
  84. return
  85. }
  86. if err := s.tokenStore.Delete(id, user.ID, user.Role == "admin"); err != nil {
  87. writeError(w, http.StatusInternalServerError, "db_error", err.Error())
  88. return
  89. }
  90. w.WriteHeader(http.StatusNoContent)
  91. }