api_group.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2019-2022 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. "github.com/go-chi/render"
  20. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  21. "github.com/drakkan/sftpgo/v2/internal/util"
  22. )
  23. func getGroups(w http.ResponseWriter, r *http.Request) {
  24. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  25. limit, offset, order, err := getSearchFilters(w, r)
  26. if err != nil {
  27. return
  28. }
  29. groups, err := dataprovider.GetGroups(limit, offset, order, false)
  30. if err != nil {
  31. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  32. return
  33. }
  34. render.JSON(w, r, groups)
  35. }
  36. func addGroup(w http.ResponseWriter, r *http.Request) {
  37. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  38. claims, err := getTokenClaims(r)
  39. if err != nil || claims.Username == "" {
  40. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  41. return
  42. }
  43. var group dataprovider.Group
  44. err = render.DecodeJSON(r.Body, &group)
  45. if err != nil {
  46. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  47. return
  48. }
  49. err = dataprovider.AddGroup(&group, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  50. if err != nil {
  51. sendAPIResponse(w, r, err, "", getRespStatus(err))
  52. return
  53. }
  54. w.Header().Add("Location", fmt.Sprintf("%s/%s", groupPath, group.Name))
  55. renderGroup(w, r, group.Name, http.StatusCreated)
  56. }
  57. func updateGroup(w http.ResponseWriter, r *http.Request) {
  58. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  59. claims, err := getTokenClaims(r)
  60. if err != nil || claims.Username == "" {
  61. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  62. return
  63. }
  64. name := getURLParam(r, "name")
  65. group, err := dataprovider.GroupExists(name)
  66. if err != nil {
  67. sendAPIResponse(w, r, err, "", getRespStatus(err))
  68. return
  69. }
  70. currentS3AccessSecret := group.UserSettings.FsConfig.S3Config.AccessSecret
  71. currentAzAccountKey := group.UserSettings.FsConfig.AzBlobConfig.AccountKey
  72. currentAzSASUrl := group.UserSettings.FsConfig.AzBlobConfig.SASURL
  73. currentGCSCredentials := group.UserSettings.FsConfig.GCSConfig.Credentials
  74. currentCryptoPassphrase := group.UserSettings.FsConfig.CryptConfig.Passphrase
  75. currentSFTPPassword := group.UserSettings.FsConfig.SFTPConfig.Password
  76. currentSFTPKey := group.UserSettings.FsConfig.SFTPConfig.PrivateKey
  77. currentSFTPKeyPassphrase := group.UserSettings.FsConfig.SFTPConfig.KeyPassphrase
  78. currentHTTPPassword := group.UserSettings.FsConfig.HTTPConfig.Password
  79. currentHTTPAPIKey := group.UserSettings.FsConfig.HTTPConfig.APIKey
  80. var updatedGroup dataprovider.Group
  81. err = render.DecodeJSON(r.Body, &updatedGroup)
  82. if err != nil {
  83. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  84. return
  85. }
  86. updatedGroup.ID = group.ID
  87. updatedGroup.Name = group.Name
  88. updatedGroup.UserSettings.FsConfig.SetEmptySecretsIfNil()
  89. updateEncryptedSecrets(&updatedGroup.UserSettings.FsConfig, currentS3AccessSecret, currentAzAccountKey, currentAzSASUrl,
  90. currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey, currentSFTPKeyPassphrase,
  91. currentHTTPPassword, currentHTTPAPIKey)
  92. err = dataprovider.UpdateGroup(&updatedGroup, group.Users, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr),
  93. claims.Role)
  94. if err != nil {
  95. sendAPIResponse(w, r, err, "", getRespStatus(err))
  96. return
  97. }
  98. sendAPIResponse(w, r, nil, "Group updated", http.StatusOK)
  99. }
  100. func renderGroup(w http.ResponseWriter, r *http.Request, name string, status int) {
  101. group, err := dataprovider.GroupExists(name)
  102. if err != nil {
  103. sendAPIResponse(w, r, err, "", getRespStatus(err))
  104. return
  105. }
  106. group.PrepareForRendering()
  107. if status != http.StatusOK {
  108. ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
  109. render.JSON(w, r.WithContext(ctx), group)
  110. } else {
  111. render.JSON(w, r, group)
  112. }
  113. }
  114. func getGroupByName(w http.ResponseWriter, r *http.Request) {
  115. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  116. name := getURLParam(r, "name")
  117. renderGroup(w, r, name, http.StatusOK)
  118. }
  119. func deleteGroup(w http.ResponseWriter, r *http.Request) {
  120. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  121. claims, err := getTokenClaims(r)
  122. if err != nil || claims.Username == "" {
  123. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  124. return
  125. }
  126. name := getURLParam(r, "name")
  127. err = dataprovider.DeleteGroup(name, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
  128. if err != nil {
  129. sendAPIResponse(w, r, err, "", getRespStatus(err))
  130. return
  131. }
  132. sendAPIResponse(w, r, err, "Group deleted", http.StatusOK)
  133. }