api_configs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "net/http"
  17. "github.com/go-chi/render"
  18. "github.com/rs/xid"
  19. "golang.org/x/oauth2"
  20. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  21. "github.com/drakkan/sftpgo/v2/internal/kms"
  22. "github.com/drakkan/sftpgo/v2/internal/smtp"
  23. "github.com/drakkan/sftpgo/v2/internal/util"
  24. )
  25. type smtpTestRequest struct {
  26. smtp.Config
  27. Recipient string `json:"recipient"`
  28. }
  29. func (r *smtpTestRequest) hasRedactedSecret() bool {
  30. return r.Password == redactedSecret || r.OAuth2.ClientSecret == redactedSecret || r.OAuth2.RefreshToken == redactedSecret
  31. }
  32. func testSMTPConfig(w http.ResponseWriter, r *http.Request) {
  33. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  34. var req smtpTestRequest
  35. err := render.DecodeJSON(r.Body, &req)
  36. if err != nil {
  37. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  38. return
  39. }
  40. if req.hasRedactedSecret() {
  41. configs, err := dataprovider.GetConfigs()
  42. if err != nil {
  43. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  44. return
  45. }
  46. configs.SetNilsToEmpty()
  47. if err := configs.SMTP.TryDecrypt(); err == nil {
  48. if req.Password == redactedSecret {
  49. req.Password = configs.SMTP.Password.GetPayload()
  50. }
  51. if req.OAuth2.ClientSecret == redactedSecret {
  52. req.OAuth2.ClientSecret = configs.SMTP.OAuth2.ClientSecret.GetPayload()
  53. }
  54. if req.OAuth2.RefreshToken == redactedSecret {
  55. req.OAuth2.RefreshToken = configs.SMTP.OAuth2.RefreshToken.GetPayload()
  56. }
  57. }
  58. }
  59. if req.AuthType == 3 {
  60. if err := req.Config.OAuth2.Validate(); err != nil {
  61. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  62. return
  63. }
  64. }
  65. if err := req.SendEmail([]string{req.Recipient}, nil, "SFTPGo - Testing Email Settings",
  66. "It appears your SFTPGo email is setup correctly!", smtp.EmailContentTypeTextPlain); err != nil {
  67. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  68. return
  69. }
  70. sendAPIResponse(w, r, nil, "SMTP connection OK", http.StatusOK)
  71. }
  72. type oauth2TokenRequest struct {
  73. smtp.OAuth2Config
  74. BaseRedirectURL string `json:"base_redirect_url"`
  75. }
  76. func handleSMTPOAuth2TokenRequestPost(w http.ResponseWriter, r *http.Request) {
  77. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  78. var req oauth2TokenRequest
  79. err := render.DecodeJSON(r.Body, &req)
  80. if err != nil {
  81. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  82. return
  83. }
  84. if req.BaseRedirectURL == "" {
  85. sendAPIResponse(w, r, nil, "base redirect url is required", http.StatusBadRequest)
  86. return
  87. }
  88. if req.ClientSecret == redactedSecret {
  89. configs, err := dataprovider.GetConfigs()
  90. if err != nil {
  91. sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
  92. return
  93. }
  94. configs.SetNilsToEmpty()
  95. if err := configs.SMTP.TryDecrypt(); err == nil {
  96. req.OAuth2Config.ClientSecret = configs.SMTP.OAuth2.ClientSecret.GetPayload()
  97. }
  98. }
  99. cfg := req.OAuth2Config.GetOAuth2()
  100. cfg.RedirectURL = req.BaseRedirectURL + webOAuth2RedirectPath
  101. clientSecret := kms.NewPlainSecret(cfg.ClientSecret)
  102. clientSecret.SetAdditionalData(xid.New().String())
  103. pendingAuth := newOAuth2PendingAuth(req.Provider, cfg.RedirectURL, cfg.ClientID, clientSecret)
  104. oauth2Mgr.addPendingAuth(pendingAuth)
  105. stateToken := createOAuth2Token(pendingAuth.State, util.GetIPFromRemoteAddress(r.RemoteAddr))
  106. if stateToken == "" {
  107. sendAPIResponse(w, r, nil, "unable to create state token", http.StatusInternalServerError)
  108. return
  109. }
  110. u := cfg.AuthCodeURL(stateToken, oauth2.AccessTypeOffline)
  111. sendAPIResponse(w, r, nil, u, http.StatusOK)
  112. }