forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
contact_types.go
336 lines (286 loc) · 6.85 KB
/
contact_types.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package proton
import (
"encoding/base64"
"errors"
"strconv"
"strings"
"github.com/ProtonMail/gluon/rfc822"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/emersion/go-vcard"
)
type RecipientType int
const (
RecipientTypeInternal RecipientType = iota + 1
RecipientTypeExternal
)
type ContactSettings struct {
MIMEType *rfc822.MIMEType
Scheme *EncryptionScheme
Sign *bool
Encrypt *bool
EncryptUntrusted *bool
Keys []*crypto.Key
}
type Contact struct {
ContactMetadata
ContactCards
}
func (cs *ContactSettings) SetMimeType(mimeType rfc822.MIMEType) {
if cs.MIMEType == nil {
cs.MIMEType = new(rfc822.MIMEType)
}
*cs.MIMEType = mimeType
}
func (cs *ContactSettings) SetScheme(scheme EncryptionScheme) {
if cs.Scheme == nil {
cs.Scheme = new(EncryptionScheme)
}
*cs.Scheme = scheme
}
func (cs *ContactSettings) SetSign(enabled bool) {
if cs.Sign == nil {
cs.Sign = new(bool)
}
*cs.Sign = enabled
}
func (cs *ContactSettings) SetEncrypt(enabled bool) {
if cs.Encrypt == nil {
cs.Encrypt = new(bool)
}
*cs.Encrypt = enabled
}
func (cs *ContactSettings) SetEncryptUntrusted(enabled bool) {
if cs.EncryptUntrusted == nil {
cs.EncryptUntrusted = new(bool)
}
*cs.EncryptUntrusted = enabled
}
func (cs *ContactSettings) AddKey(key *crypto.Key) {
cs.Keys = append(cs.Keys, key)
}
func (c *Contact) GetSettings(kr *crypto.KeyRing, email string, cardType CardType) (ContactSettings, error) {
signedCard, ok := c.Cards.Get(cardType)
if !ok {
return ContactSettings{}, nil
}
group, err := signedCard.GetGroup(kr, vcard.FieldEmail, email)
if err != nil {
return ContactSettings{}, nil
}
var settings ContactSettings
scheme, err := group.Get(FieldPMScheme)
if err != nil {
return ContactSettings{}, err
}
if len(scheme) > 0 {
switch scheme[0] {
case "pgp-inline":
settings.Scheme = newPtr(PGPInlineScheme)
case "pgp-mime":
settings.Scheme = newPtr(PGPMIMEScheme)
}
}
mimeType, err := group.Get(FieldPMMIMEType)
if err != nil {
return ContactSettings{}, err
}
if len(mimeType) > 0 {
settings.MIMEType = newPtr(rfc822.MIMEType(mimeType[0]))
}
sign, err := group.Get(FieldPMSign)
if err != nil {
return ContactSettings{}, err
}
if len(sign) > 0 {
sign, err := strconv.ParseBool(sign[0])
if err != nil {
return ContactSettings{}, err
}
settings.Sign = newPtr(sign)
}
encrypt, err := group.Get(FieldPMEncrypt)
if err != nil {
return ContactSettings{}, err
}
if len(encrypt) > 0 {
encrypt, err := strconv.ParseBool(encrypt[0])
if err != nil {
return ContactSettings{}, err
}
settings.Encrypt = newPtr(encrypt)
}
encryptUntrusted, err := group.Get(FieldPMEncryptUntrusted)
if err != nil {
return ContactSettings{}, err
}
if len(encryptUntrusted) > 0 {
b, err := strconv.ParseBool(encryptUntrusted[0])
if err != nil {
return ContactSettings{}, err
}
settings.EncryptUntrusted = newPtr(b)
}
keys, err := group.Get(vcard.FieldKey)
if err != nil {
return ContactSettings{}, err
}
if len(keys) > 0 {
for _, key := range keys {
dec, err := base64.StdEncoding.DecodeString(strings.SplitN(key, ",", 2)[1])
if err != nil {
return ContactSettings{}, err
}
pubKey, err := crypto.NewKey(dec)
if err != nil {
return ContactSettings{}, err
}
settings.Keys = append(settings.Keys, pubKey)
}
}
return settings, nil
}
func (c *Contact) SetSettings(kr *crypto.KeyRing, email string, cardType CardType, settings ContactSettings) error {
signedCard, ok := c.Cards.Get(cardType)
if !ok {
return errors.New("cannot get contact card for " + email)
}
group, err := signedCard.GetGroup(kr, vcard.FieldEmail, email)
if err != nil {
return nil
}
// X-PM-SCHEME
if settings.Scheme != nil {
switch *settings.Scheme {
case PGPInlineScheme:
if err := group.Set(FieldPMScheme, "pgp-inline", vcard.Params{}); err != nil {
return err
}
case PGPMIMEScheme:
if err := group.Set(FieldPMScheme, "pgp-mime", vcard.Params{}); err != nil {
return err
}
}
}
// X-PM-MIMETYPE
if settings.MIMEType != nil {
switch *settings.MIMEType {
case rfc822.TextPlain:
if err := group.Set(FieldPMMIMEType, string(rfc822.TextPlain), vcard.Params{}); err != nil {
return err
}
case rfc822.TextHTML:
if err := group.Set(FieldPMMIMEType, string(rfc822.TextHTML), vcard.Params{}); err != nil {
return err
}
case rfc822.MultipartMixed:
if err := group.Set(FieldPMMIMEType, string(rfc822.MultipartMixed), vcard.Params{}); err != nil {
return err
}
case rfc822.MultipartRelated:
if err := group.Set(FieldPMMIMEType, string(rfc822.MultipartRelated), vcard.Params{}); err != nil {
return err
}
case rfc822.MessageRFC822:
if err := group.Set(FieldPMMIMEType, string(rfc822.MessageRFC822), vcard.Params{}); err != nil {
return err
}
}
}
// X-PM-SIGN
if settings.Sign != nil {
if *settings.Sign {
if err := group.Set(FieldPMSign, "true", vcard.Params{}); err != nil {
return err
}
} else {
if err := group.Set(FieldPMSign, "false", vcard.Params{}); err != nil {
return err
}
}
}
// X-PM-ENCRYPT
if settings.Encrypt != nil {
if *settings.Encrypt {
if err := group.Set(FieldPMEncrypt, "true", vcard.Params{}); err != nil {
return err
}
} else {
if err := group.Set(FieldPMEncrypt, "false", vcard.Params{}); err != nil {
return err
}
}
}
// X-PM-ENCRYPT-UNTRUSTED:
if settings.EncryptUntrusted != nil {
if *settings.EncryptUntrusted {
if err := group.Set(FieldPMEncryptUntrusted, "true", vcard.Params{}); err != nil {
return err
}
} else {
if err := group.Set(FieldPMEncryptUntrusted, "false", vcard.Params{}); err != nil {
return err
}
}
}
// KEY
if settings.Keys != nil && len(settings.Keys) > 0 {
var keys = ""
for i, key := range settings.Keys {
if i > 0 {
keys += ","
}
if dec, err := key.Serialize(); err == nil {
keys += string(dec)
}
}
enc := base64.StdEncoding.EncodeToString([]byte(keys))
if err := group.Set(vcard.FieldKey, "base64,"+enc, vcard.Params{}); err != nil {
return err
}
}
*signedCard = group.Card
return nil
}
type ContactMetadata struct {
ID string
Name string
UID string
Size int64
CreateTime int64
ModifyTime int64
ContactEmails []ContactEmail
LabelIDs []string
}
type ContactCards struct {
Cards Cards
}
type ContactEmail struct {
ID string
Name string
Email string
Type []string
ContactID string
LabelIDs []string
}
type CreateContactsReq struct {
Contacts []ContactCards
Overwrite int
Labels int
}
type CreateContactResp struct {
APIError
Contact Contact
}
type CreateContactsRes struct {
Index int
Response CreateContactResp
}
type UpdateContactReq struct {
Cards Cards
}
type DeleteContactsReq struct {
IDs []string
}
func newPtr[T any](v T) *T {
return &v
}