79bd55aad8
Pasted app passwords (e.g. mail.ru) often carry a trailing space/newline that the IMAP server rejects; the CSV path already trims, so make manual add match. Source and destination passwords remain fully independent (src_pass_enc / dst_pass_enc), verified end-to-end — this only strips surrounding whitespace. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MMHQTtnQtQqL8muAXHr9kd
79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/vasyansk/imap-copier/internal/crypto"
|
|
"github.com/vasyansk/imap-copier/internal/store"
|
|
)
|
|
|
|
type AccountView struct {
|
|
ID int64 `json:"id"`
|
|
SrcLogin string `json:"src_login"`
|
|
DstLogin string `json:"dst_login"`
|
|
TestSrcStatus string `json:"test_src_status"`
|
|
TestDstStatus string `json:"test_dst_status"`
|
|
Status string `json:"status"`
|
|
Copied int64 `json:"copied"`
|
|
Skipped int64 `json:"skipped"`
|
|
Errors int64 `json:"errors"`
|
|
}
|
|
|
|
func accountDTO(a store.Account) AccountView {
|
|
return AccountView{
|
|
ID: a.ID, SrcLogin: a.SrcLogin, DstLogin: a.DstLogin,
|
|
TestSrcStatus: a.TestSrcStatus, TestDstStatus: a.TestDstStatus,
|
|
Status: a.Status, Copied: a.Copied, Skipped: a.Skipped, Errors: a.Errors,
|
|
}
|
|
}
|
|
|
|
func pathID(r *http.Request, name string) (int64, error) {
|
|
return strconv.ParseInt(r.PathValue(name), 10, 64)
|
|
}
|
|
|
|
func (s *Server) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
|
taskID, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
var body struct {
|
|
SrcLogin string `json:"src_login"`
|
|
SrcPass string `json:"src_pass"`
|
|
DstLogin string `json:"dst_login"`
|
|
DstPass string `json:"dst_pass"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
http.Error(w, "bad json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// Trim surrounding whitespace — pasted app passwords / logins often carry a
|
|
// stray trailing space or newline that the IMAP server rejects.
|
|
body.SrcLogin = strings.TrimSpace(body.SrcLogin)
|
|
body.SrcPass = strings.TrimSpace(body.SrcPass)
|
|
body.DstLogin = strings.TrimSpace(body.DstLogin)
|
|
body.DstPass = strings.TrimSpace(body.DstPass)
|
|
srcEnc, err := crypto.Encrypt(s.cfg.EncKey, []byte(body.SrcPass))
|
|
if err != nil {
|
|
http.Error(w, "encrypt", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
dstEnc, err := crypto.Encrypt(s.cfg.EncKey, []byte(body.DstPass))
|
|
if err != nil {
|
|
http.Error(w, "encrypt", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
id, err := s.store.CreateAccount(r.Context(), store.Account{
|
|
TaskID: taskID, SrcLogin: body.SrcLogin, SrcPassEnc: srcEnc,
|
|
DstLogin: body.DstLogin, DstPassEnc: dstEnc,
|
|
})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, map[string]int64{"id": id})
|
|
}
|