FRE-681: Fix code review findings - body flag, PGP encryption, passphrase handling

- cmd/mail.go: Fix duplicate --body/--body-file flag binding (both used bodyFile)
- internal/mail/client.go: Add PGP encryption to Send via EncryptBody, add passphrase to MoveToTrash and SendDraft
- internal/mail/pgp.go: Store armored private key, add getUnlockedKeyRing helper,
  fix Decrypt/SignData/EncryptAndSign/DecryptAttachment to use passphrase via key.Unlock
- internal/mail/pgp.go: Add EncryptBody method for Send encryption with sender key
- cmd/draft.go: Update SendDraft call to include passphrase parameter
This commit is contained in:
Senior Engineer
2026-04-28 10:08:33 -04:00
committed by Michael Freno
parent af25fd5575
commit e499d16b7c
4 changed files with 112 additions and 37 deletions

View File

@@ -12,8 +12,9 @@ import (
)
type Client struct {
apiClient *api.ProtonMailClient
baseURL string
apiClient *api.ProtonMailClient
baseURL string
pgpService *PGPService
}
func NewClient(apiClient *api.ProtonMailClient) *Client {
@@ -23,6 +24,10 @@ func NewClient(apiClient *api.ProtonMailClient) *Client {
}
}
func (c *Client) SetPGPService(svc *PGPService) {
c.pgpService = svc
}
func (c *Client) ListMessages(req ListMessagesRequest) (*ListMessagesResponse, error) {
body := map[string]interface{}{
"Page": req.Page,
@@ -116,24 +121,35 @@ func (c *Client) GetMessage(messageID string, passphrase string) (*Message, erro
}
func (c *Client) Send(req SendRequest) error {
body := map[string]interface{}{
payload := map[string]interface{}{
"Type": "0",
"Passphrase": req.Passphrase,
"Subject": req.Subject,
"HTML": req.HTML,
"To": req.To,
"Body": req.Body,
}
if req.Body != "" {
if c.pgpService != nil {
encrypted, err := c.pgpService.EncryptBody(req.Body, req.Passphrase)
if err != nil {
return fmt.Errorf("failed to encrypt message body: %w", err)
}
payload["BodyEnc"] = encrypted
} else {
payload["Body"] = req.Body
}
}
if len(req.CC) > 0 {
body["CC"] = req.CC
payload["CC"] = req.CC
}
if len(req.BCC) > 0 {
body["BCC"] = req.BCC
payload["BCC"] = req.BCC
}
jsonBody, err := json.Marshal(body)
jsonBody, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
@@ -159,8 +175,9 @@ func (c *Client) Send(req SendRequest) error {
return nil
}
func (c *Client) MoveToTrash(messageID string) error {
func (c *Client) MoveToTrash(messageID string, passphrase string) error {
formData := url.Values{}
formData.Set("Passphrase", passphrase)
reqURL := fmt.Sprintf("%s/api/messages/%s/movetotrash", c.baseURL, url.QueryEscape(messageID))
httpReq, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(formData.Encode()))
if err != nil {
@@ -293,8 +310,9 @@ func (c *Client) UpdateDraft(messageID string, draft Draft, passphrase string) e
return nil
}
func (c *Client) SendDraft(messageID string) error {
func (c *Client) SendDraft(messageID string, passphrase string) error {
formData := url.Values{}
formData.Set("Passphrase", passphrase)
reqURL := fmt.Sprintf("%s/api/messages/%s/send", c.baseURL, url.QueryEscape(messageID))
httpReq, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(formData.Encode()))
if err != nil {