-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
send_message.go
272 lines (245 loc) · 8.16 KB
/
send_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2019 Finobo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handlers
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/mailchain/mailchain/cmd/mailchain/internal/http/params"
"github.com/mailchain/mailchain/crypto"
"github.com/mailchain/mailchain/crypto/cipher/aes256cbc"
"github.com/mailchain/mailchain/crypto/secp256k1"
"github.com/mailchain/mailchain/errs"
"github.com/mailchain/mailchain/internal/address"
"github.com/mailchain/mailchain/internal/envelope"
"github.com/mailchain/mailchain/internal/keystore"
"github.com/mailchain/mailchain/internal/keystore/kdf/multi"
"github.com/mailchain/mailchain/internal/mail"
"github.com/mailchain/mailchain/internal/mailbox"
"github.com/mailchain/mailchain/sender"
"github.com/mailchain/mailchain/stores"
"github.com/pkg/errors"
)
// SendMessage handler http
func SendMessage(sent stores.Sent, senders map[string]sender.Message, ks keystore.Store,
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) { // nolint: funlen
encrypter := aes256cbc.NewEncrypter()
// Post swagger:route POST /messages Send SendMessage
//
// Send message.
//
// Securely send message on the protocol and network specified in the query string to the address.
// Only the private key holder for the recipient address can decrypted any encrypted contents.
//
// - Create mailchain message
// - Encrypt content with public key
// - Store message
// - Encrypt location
// - Store encrypted location on the blockchain.
//
// Responses:
// 200: StatusOK
// 404: NotFoundError
// 422: ValidationError
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := parsePostRequest(r)
if err != nil {
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.WithStack(err))
return
}
sender, ok := senders[fmt.Sprintf("%s/%s", req.Protocol, req.Network)]
if !ok {
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.Errorf("sender not supported on \"%s/%s\"", req.Protocol, req.Network))
return
}
if sender == nil {
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.Errorf("no sender configured for \"%s/%s\"", req.Protocol, req.Network))
return
}
from, err := address.DecodeByProtocol(req.Body.from.ChainAddress, req.Protocol)
if err != nil {
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithMessage(err, "failed to decode address"))
return
}
if !ks.HasAddress(from) {
errs.JSONWriter(w, http.StatusNotAcceptable, errors.Errorf("no private key found for `%s` from address", req.Body.Message.Headers.From))
return
}
msg, err := mail.NewMessage(time.Now(), *req.Body.from, *req.Body.to, req.Body.replyTo, req.Body.Message.Subject, []byte(req.Body.Message.Body))
if err != nil {
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.WithStack(err))
return
}
signer, err := ks.GetSigner(from, req.Protocol, deriveKeyOptions)
if err != nil {
errs.JSONWriter(w, http.StatusUnprocessableEntity, errors.WithStack(errors.WithMessage(err, "could not get `signer`")))
return
}
if err := mailbox.SendMessage(ctx, req.Protocol, req.Network,
msg, req.Body.publicKey,
encrypter, sender, sent, signer, envelope.Kind0x01); err != nil {
errs.JSONWriter(w, http.StatusInternalServerError, errors.WithMessage(err, "could not send message"))
return
}
w.WriteHeader(http.StatusOK)
}
}
// PostRequest get mailchain inputs
// swagger:parameters SendMessage
type PostRequest struct {
// Network to use when sending a message.
//
// enum: mainnet,goerli,ropsten,rinkeby,local
// in: query
// required: true
// example: goerli
Network string `json:"network"`
// Protocol to use when sending a message.
//
// enum: ethereum
// in: query
// required: true
// example: ethereum
Protocol string `json:"protocol"`
// Message to send
// in: body
// required: true
Body PostRequestBody
}
// parsePostRequest post all the details for the message
func parsePostRequest(r *http.Request) (*PostRequest, error) {
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
protocol, err := params.QueryRequireProtocol(r)
if err != nil {
return nil, err
}
network, err := params.QueryRequireNetwork(r)
if err != nil {
return nil, err
}
var body PostRequestBody
if err := decoder.Decode(&body); err != nil {
return nil, errors.WithMessage(err, "'message' is invalid")
}
if err := isValid(&body, protocol, network); err != nil {
return nil, err
}
return &PostRequest{
Network: network,
Protocol: protocol,
Body: body,
}, nil
}
// swagger:model PostMessagesResponseHeaders
type PostHeaders struct {
// The sender of the message
// required: true
// example: Charlotte <5602ea95540bee46d03ba335eed6f49d117eab95c8ab8b71bae2cdd1e564a761@ropsten.ethereum>
From string `json:"from"`
// The recipient of the message
// required: true
// To: <4cb0a77b76667dac586c40cc9523ace73b5d772bd503c63ed0ca596eae1658b2@ropsten.ethereum>
To string `json:"to"`
// Reply to if the reply address is different to the from address.
ReplyTo string `json:"reply-to"`
}
// swagger:model PostMessagesResponseMessage
type PostMessage struct {
// Headers
// required: true
// in: body
Headers *PostHeaders `json:"headers"`
// Body of the mail message
// required: true
// example: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur maximus metus ante,
// sit amet ullamcorper dui hendrerit ac. Sed vestibulum dui lectus, quis eleifend urna mollis eu.
// Integer dictum metus ut sem rutrum aliquet.
Body string `json:"body"`
// Subject of the mail message
// required: true
// example: Hello world
Subject string `json:"subject"`
// Public key of the recipient to encrypt with
// required: true
PublicKey string `json:"public-key"`
}
// swagger:model SendMessageRequestBody
type PostRequestBody struct {
// required: true
Message PostMessage `json:"message"`
to *mail.Address
from *mail.Address
replyTo *mail.Address
publicKey crypto.PublicKey
}
func checkForEmpties(msg PostMessage) error {
if msg.Headers == nil {
return errors.Errorf("headers must not be nil")
}
if msg.Body == "" {
return errors.Errorf("`body` can not be empty")
}
if msg.Subject == "" {
return errors.Errorf("`subject` can not be empty")
}
if msg.PublicKey == "" {
return errors.Errorf("`public-key` can not be empty")
}
return nil
}
func isValid(p *PostRequestBody, protocol, network string) error {
if p == nil {
return errors.New("PostRequestBody must not be nil")
}
if err := checkForEmpties(p.Message); err != nil {
return err
}
var err error
p.to, err = mail.ParseAddress(p.Message.Headers.To, protocol, network)
if err != nil {
return errors.WithMessage(err, "`to` is invalid")
}
// TODO: figure this out
// if !ethereup.IsAddressValid(p.to.ChainAddress) {
// return errors.Errorf("'address' is invalid")
// }
p.from, err = mail.ParseAddress(p.Message.Headers.From, protocol, network)
if err != nil {
return errors.WithMessage(err, "`from` is invalid")
}
if p.Message.Headers.ReplyTo != "" {
p.replyTo, err = mail.ParseAddress(p.Message.Headers.ReplyTo, protocol, network)
if err != nil {
return errors.WithMessage(err, "`reply-to` is invalid")
}
}
// TODO: be more general when getting key from hex
p.publicKey, err = secp256k1.PublicKeyFromHex(p.Message.PublicKey)
if err != nil {
return errors.WithMessage(err, "invalid `public-key`")
}
pkAddress := p.publicKey.Address()
toAddress, err := address.DecodeByProtocol(p.to.ChainAddress, protocol)
if err != nil {
return errors.WithMessage(err, "failed to decode address")
}
if !bytes.Equal(pkAddress, toAddress) {
return errors.Errorf("`public-key` does not match to address")
}
return nil
}