Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Client side draft encrypt #9

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions message_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,10 @@ func (c *Client) CreateDraft(ctx context.Context, addrKR *crypto.KeyRing, req Cr
return Message{}, fmt.Errorf("failed to armor draft: %w", err)
}

type encCreateDraftReq struct {
CreateDraftReq

Body string
}
req.Message.Body = arm

if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(encCreateDraftReq{
CreateDraftReq: req,
Body: arm,
}).SetResult(&res).Post("/mail/v4/messages")
return r.SetBody(req).SetResult(&res).Post("/mail/v4/messages")
}); err != nil {
return Message{}, err
}
Expand All @@ -46,27 +39,22 @@ func (c *Client) UpdateDraft(ctx context.Context, draftID string, addrKR *crypto
Message Message
}

enc, err := addrKR.Encrypt(crypto.NewPlainMessageFromString(req.Message.Body), nil)
if err != nil {
return Message{}, fmt.Errorf("failed to encrypt draft: %w", err)
}

arm, err := enc.GetArmored()
if err != nil {
return Message{}, fmt.Errorf("failed to armor draft: %w", err)
}
if req.Message.Body != "" {
enc, err := addrKR.Encrypt(crypto.NewPlainMessageFromString(req.Message.Body), nil)
if err != nil {
return Message{}, fmt.Errorf("failed to encrypt draft: %w", err)
}

type encUpdateDraftReq struct {
UpdateDraftReq
arm, err := enc.GetArmored()
if err != nil {
return Message{}, fmt.Errorf("failed to armor draft: %w", err)
}

Body string
req.Message.Body = arm
}

if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(encUpdateDraftReq{
UpdateDraftReq: req,
Body: arm,
}).SetResult(&res).Put("/mail/v4/messages/" + draftID)
return r.SetBody(req).SetResult(&res).Put("/mail/v4/messages/" + draftID)
}); err != nil {
return Message{}, err
}
Expand Down