api_group.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "net/url"
  20. "github.com/go-chi/render"
  21. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  22. "github.com/drakkan/sftpgo/v2/internal/util"
  23. )
  24. func getGroups(w http.ResponseWriter, r *http.Request) {
  25. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  26. limit, offset, order, err := getSearchFilters(w, r)
  27. if err != nil {
  28. return
  29. }
  30. groups, err := dataprovider.GetGroups(limit, offset, order, false)
  31. if err != nil {
  32. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  33. return
  34. }
  35. render.JSON(w, r, groups)
  36. }
  37. func addGroup(w http.ResponseWriter, r *http.Request) {
  38. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  39. claims, err := getTokenClaims(r)
  40. if err != nil || claims.Username == "" {
  41. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  42. return
  43. }
  44. var group dataprovider.Group
  45. err = render.DecodeJSON(r.Body, &group)
  46. if err != nil {
  47. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  48. return
  49. }
  50. err = dataprovider.AddGroup(&group, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  51. if err != nil {
  52. sendAPIResponse(w, r, err, "", getRespStatus(err))
  53. return
  54. }
  55. w.Header().Add("Location", fmt.Sprintf("%s/%s", groupPath, url.PathEscape(group.Name)))
  56. renderGroup(w, r, group.Name, &claims, http.StatusCreated)
  57. }
  58. func updateGroup(w http.ResponseWriter, r *http.Request) {
  59. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  60. claims, err := getTokenClaims(r)
  61. if err != nil || claims.Username == "" {
  62. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  63. return
  64. }
  65. name := getURLParam(r, "name")
  66. group, err := dataprovider.GroupExists(name)
  67. if err != nil {
  68. sendAPIResponse(w, r, err, "", getRespStatus(err))
  69. return
  70. }
  71. currentS3AccessSecret := group.UserSettings.FsConfig.S3Config.AccessSecret
  72. currentAzAccountKey := group.UserSettings.FsConfig.AzBlobConfig.AccountKey
  73. currentAzSASUrl := group.UserSettings.FsConfig.AzBlobConfig.SASURL
  74. currentGCSCredentials := group.UserSettings.FsConfig.GCSConfig.Credentials
  75. currentCryptoPassphrase := group.UserSettings.FsConfig.CryptConfig.Passphrase
  76. currentSFTPPassword := group.UserSettings.FsConfig.SFTPConfig.Password
  77. currentSFTPKey := group.UserSettings.FsConfig.SFTPConfig.PrivateKey
  78. currentSFTPKeyPassphrase := group.UserSettings.FsConfig.SFTPConfig.KeyPassphrase
  79. currentHTTPPassword := group.UserSettings.FsConfig.HTTPConfig.Password
  80. currentHTTPAPIKey := group.UserSettings.FsConfig.HTTPConfig.APIKey
  81. var updatedGroup dataprovider.Group
  82. err = render.DecodeJSON(r.Body, &updatedGroup)
  83. if err != nil {
  84. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  85. return
  86. }
  87. updatedGroup.ID = group.ID
  88. updatedGroup.Name = group.Name
  89. updatedGroup.UserSettings.FsConfig.SetEmptySecretsIfNil()
  90. updateEncryptedSecrets(&updatedGroup.UserSettings.FsConfig, currentS3AccessSecret, currentAzAccountKey, currentAzSASUrl,
  91. currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey, currentSFTPKeyPassphrase,
  92. currentHTTPPassword, currentHTTPAPIKey)
  93. err = dataprovider.UpdateGroup(&updatedGroup, group.Users, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr),
  94. claims.Role)
  95. if err != nil {
  96. sendAPIResponse(w, r, err, "", getRespStatus(err))
  97. return
  98. }
  99. sendAPIResponse(w, r, nil, "Group updated", http.StatusOK)
  100. }
  101. func renderGroup(w http.ResponseWriter, r *http.Request, name string, claims *jwtTokenClaims, status int) {
  102. group, err := dataprovider.GroupExists(name)
  103. if err != nil {
  104. sendAPIResponse(w, r, err, "", getRespStatus(err))
  105. return
  106. }
  107. if hideConfidentialData(claims, r) {
  108. group.PrepareForRendering()
  109. }
  110. if status != http.StatusOK {
  111. ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
  112. render.JSON(w, r.WithContext(ctx), group)
  113. } else {
  114. render.JSON(w, r, group)
  115. }
  116. }
  117. func getGroupByName(w http.ResponseWriter, r *http.Request) {
  118. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  119. claims, err := getTokenClaims(r)
  120. if err != nil || claims.Username == "" {
  121. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  122. return
  123. }
  124. name := getURLParam(r, "name")
  125. renderGroup(w, r, name, &claims, http.StatusOK)
  126. }
  127. func deleteGroup(w http.ResponseWriter, r *http.Request) {
  128. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  129. claims, err := getTokenClaims(r)
  130. if err != nil || claims.Username == "" {
  131. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  132. return
  133. }
  134. name := getURLParam(r, "name")
  135. err = dataprovider.DeleteGroup(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  136. if err != nil {
  137. sendAPIResponse(w, r, err, "", getRespStatus(err))
  138. return
  139. }
  140. sendAPIResponse(w, r, err, "Group deleted", http.StatusOK)
  141. }