copyText (family idea "copyText"): - web/src/lib/utils/clipboard.ts tries the Clipboard API first. - Where the API is missing (plain http on a home network) or refuses, it copies through the copy command on a hidden textarea instead. - If that fails too, it selects the shown value. - All six Copy buttons go through it: an invite token, the needs-a-file list, diagnostics, connection details, a playlist link, and report details. Before, five of them only showed an error over http. SecretField (family idea "SecretField"): - One component for a write-only credential: a password box for a new value, which the card's own Save sends, and a Remove button while one is saved. - Used for the provider API keys, the SMTP password, the backup secret key and the AcoustID key. Only AcoustID had Remove before. - The server gains clear_password (SMTP) and clear_secret_key (backup). An empty value still keeps the stored one. Provider keys already clear when sent empty. - Remove sends the saved settings back with only the credential cleared, so unsaved edits elsewhere on the card stay unsaved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
4.4 KiB
Go
146 lines
4.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/apierror"
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
"github.com/Fabled-Sword/minstrel/internal/mailer"
|
|
"github.com/Fabled-Sword/minstrel/internal/secret"
|
|
)
|
|
|
|
type smtpConfigResp struct {
|
|
Enabled bool `json:"enabled"`
|
|
Host string `json:"host"`
|
|
Port int32 `json:"port"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"` // masked: "***" if non-empty, "" otherwise
|
|
FromAddress string `json:"from_address"`
|
|
FromName string `json:"from_name"`
|
|
UseTLS bool `json:"use_tls"`
|
|
}
|
|
|
|
type updateSMTPConfigReq struct {
|
|
Enabled bool `json:"enabled"`
|
|
Host string `json:"host"`
|
|
Port int32 `json:"port"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"` // empty string = preserve existing
|
|
FromAddress string `json:"from_address"`
|
|
FromName string `json:"from_name"`
|
|
UseTLS bool `json:"use_tls"`
|
|
// ClearPassword removes the stored password. An empty Password alone
|
|
// keeps it, so the admin doesn't retype it on every save.
|
|
ClearPassword bool `json:"clear_password"`
|
|
}
|
|
|
|
func (h *handlers) handleGetSMTPConfig(w http.ResponseWriter, r *http.Request) {
|
|
q := dbq.New(h.pool)
|
|
cfg, err := q.GetSMTPConfig(r.Context())
|
|
if err != nil {
|
|
writeErrWithLog(w, h.logger, "admin smtp: get failed", apierror.Internal(err))
|
|
return
|
|
}
|
|
masked := ""
|
|
if cfg.Password != "" {
|
|
masked = "***"
|
|
}
|
|
writeJSON(w, http.StatusOK, smtpConfigResp{
|
|
Enabled: cfg.Enabled,
|
|
Host: cfg.Host,
|
|
Port: cfg.Port,
|
|
Username: cfg.Username,
|
|
Password: masked,
|
|
FromAddress: cfg.FromAddress,
|
|
FromName: cfg.FromName,
|
|
UseTLS: cfg.UseTls,
|
|
})
|
|
}
|
|
|
|
func (h *handlers) handleUpdateSMTPConfig(w http.ResponseWriter, r *http.Request) {
|
|
var req updateSMTPConfigReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeErr(w, apierror.BadRequest("invalid_body", ""))
|
|
return
|
|
}
|
|
if req.Enabled {
|
|
if req.Host == "" || req.FromAddress == "" {
|
|
writeErr(w, apierror.BadRequest("missing_fields",
|
|
"host and from_address are required when enabled"))
|
|
return
|
|
}
|
|
}
|
|
q := dbq.New(h.pool)
|
|
current, err := q.GetSMTPConfig(r.Context())
|
|
if err != nil {
|
|
writeErrWithLog(w, h.logger, "admin smtp: get failed", apierror.Internal(err))
|
|
return
|
|
}
|
|
// Preserve the stored (sealed) password when the request sends "", so
|
|
// the admin doesn't have to re-enter it on every save.
|
|
password := current.Password
|
|
if req.ClearPassword {
|
|
password = ""
|
|
}
|
|
if req.Password != "" {
|
|
password, err = secret.Seal(secret.SMTPPassword.Purpose(), "", req.Password)
|
|
if err != nil {
|
|
writeErrWithLog(w, h.logger, "admin smtp: seal password", apierror.Internal(err))
|
|
return
|
|
}
|
|
}
|
|
if err := q.UpdateSMTPConfig(r.Context(), dbq.UpdateSMTPConfigParams{
|
|
Enabled: req.Enabled,
|
|
Host: req.Host,
|
|
Port: req.Port,
|
|
Username: req.Username,
|
|
Password: password,
|
|
FromAddress: req.FromAddress,
|
|
FromName: req.FromName,
|
|
UseTls: req.UseTLS,
|
|
}); err != nil {
|
|
writeErrWithLog(w, h.logger, "admin smtp: update failed", apierror.Internal(err))
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *handlers) handleTestSMTPConfig(w http.ResponseWriter, r *http.Request) {
|
|
caller, ok := requireUser(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
if caller.Email == nil || *caller.Email == "" {
|
|
writeErr(w, apierror.BadRequest("no_email_on_file",
|
|
"set your email in /settings before testing SMTP"))
|
|
return
|
|
}
|
|
|
|
// Use a short-circuit context so the SMTP send doesn't hang the
|
|
// HTTP request indefinitely on a misconfigured host.
|
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
sender := mailer.NewSMTPSender(h.pool, h.logger)
|
|
textBody, htmlBody, err := mailer.RenderSMTPTestEmail(h.netSettings.PublicURL())
|
|
if err != nil {
|
|
writeErrWithLog(w, h.logger, "admin smtp: render test email failed", apierror.Internal(err))
|
|
return
|
|
}
|
|
if err := sender.Send(ctx, *caller.Email, mailer.SMTPTestSubject, textBody, htmlBody); err != nil {
|
|
if errors.Is(err, mailer.ErrNotConfigured) {
|
|
writeErr(w, apierror.BadRequest("not_configured",
|
|
"SMTP is not enabled or required fields are empty"))
|
|
return
|
|
}
|
|
h.logger.Error("admin smtp: test send failed", "err", err)
|
|
writeErr(w, &apierror.Error{Status: http.StatusInternalServerError, Code: "send_failed", Message: err.Error()})
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|