Debian Codegraph @

calendar_types.go

raw
1 package proton
2
3 import (
4 "errors"
5
6 "github.com/ProtonMail/gopenpgp/v2/crypto"
7 )
8
9 type Calendar struct {
10 ID string
11 Name string
12 Description string
13 Color string
14 Display Bool
15
16 Type CalendarType
17 Flags CalendarFlag
18 }
19
20 type CalendarFlag int64
21
22 const (
23 CalendarFlagActive CalendarFlag = 1 << iota
24 CalendarFlagUpdatePassphrase
25 CalendarFlagResetNeeded
26 CalendarFlagIncompleteSetup
27 CalendarFlagLostAccess
28 )
29
30 type CalendarType int
31
32 const (
33 CalendarTypeNormal CalendarType = iota
34 CalendarTypeSubscribed
35 )
36
37 type CalendarKey struct {
38 ID string
39 CalendarID string
40 PassphraseID string
41 PrivateKey string
42 Flags CalendarKeyFlag
43 }
44
45 func (key CalendarKey) Unlock(passphrase []byte) (*crypto.Key, error) {
46 lockedKey, err := crypto.NewKeyFromArmored(key.PrivateKey)
47 if err != nil {
48 return nil, err
49 }
50
51 return lockedKey.Unlock(passphrase)
52 }
53
54 type CalendarKeys []CalendarKey
55
56 func (keys CalendarKeys) Unlock(passphrase []byte) (*crypto.KeyRing, error) {
57 kr, err := crypto.NewKeyRing(nil)
58 if err != nil {
59 return nil, err
60 }
61
62 for _, key := range keys {
63 if k, err := key.Unlock(passphrase); err != nil {
64 continue
65 } else if err := kr.AddKey(k); err != nil {
66 return nil, err
67 }
68 }
69
70 return kr, nil
71 }
72
73 // TODO: What is this?
74 type CalendarKeyFlag int64
75
76 const (
77 CalendarKeyFlagActive CalendarKeyFlag = 1 << iota
78 CalendarKeyFlagPrimary
79 )
80
81 type CalendarMember struct {
82 ID string
83 Permissions CalendarPermissions
84 Email string
85 Color string
86 Display Bool
87 CalendarID string
88 }
89
90 // TODO: What is this?
91 type CalendarPermissions int
92
93 // TODO: Support invitations.
94 type CalendarPassphrase struct {
95 ID string
96 Flags CalendarPassphraseFlag
97 MemberPassphrases []MemberPassphrase
98 }
99
100 func (passphrase CalendarPassphrase) Decrypt(memberID string, addrKR *crypto.KeyRing) ([]byte, error) {
101 for _, passphrase := range passphrase.MemberPassphrases {
102 if passphrase.MemberID == memberID {
103 return passphrase.decrypt(addrKR)
104 }
105 }
106
107 return nil, errors.New("no such member passphrase")
108 }
109
110 // TODO: What is this?
111 type CalendarPassphraseFlag int64
112
113 type MemberPassphrase struct {
114 MemberID string
115 Passphrase string
116 Signature string
117 }
118
119 func (passphrase MemberPassphrase) decrypt(addrKR *crypto.KeyRing) ([]byte, error) {
120 msg, err := crypto.NewPGPMessageFromArmored(passphrase.Passphrase)
121 if err != nil {
122 return nil, err
123 }
124
125 sig, err := crypto.NewPGPSignatureFromArmored(passphrase.Signature)
126 if err != nil {
127 return nil, err
128 }
129
130 dec, err := addrKR.Decrypt(msg, nil, crypto.GetUnixTime())
131 if err != nil {
132 return nil, err
133 }
134
135 if err := addrKR.VerifyDetached(dec, sig, crypto.GetUnixTime()); err != nil {
136 return nil, err
137 }
138
139 return dec.GetBinary(), nil
140 }