/
user.go
146 lines (116 loc) · 3.69 KB
/
user.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
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package fakeapi
import (
"context"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/v2/pkg/pmapi"
)
func (api *FakePMAPI) GetMailSettings(context.Context) (pmapi.MailSettings, error) {
if err := api.checkAndRecordCall(GET, "/mail/v4/settings", nil); err != nil {
return pmapi.MailSettings{}, err
}
return pmapi.MailSettings{}, nil
}
func (api *FakePMAPI) IsUnlocked() bool {
return api.userKeyRing != nil
}
func (api *FakePMAPI) Unlock(_ context.Context, passphrase []byte) (err error) {
if api.userKeyRing != nil {
return
}
if api.userKeyRing, err = api.user.Keys.UnlockAll(passphrase, nil); err != nil {
return
}
for _, a := range *api.addresses {
if a.HasKeys == pmapi.MissingKeys {
continue
}
if api.addrKeyRing[a.ID] != nil {
continue
}
var kr *crypto.KeyRing
if kr, err = a.Keys.UnlockAll(passphrase, api.userKeyRing); err != nil {
return
}
api.addrKeyRing[a.ID] = kr
}
return nil
}
func (api *FakePMAPI) ReloadKeys(ctx context.Context, passphrase []byte) (err error) {
if _, err = api.UpdateUser(ctx); err != nil {
return
}
return api.Unlock(ctx, passphrase)
}
func (api *FakePMAPI) CurrentUser(ctx context.Context) (*pmapi.User, error) {
return api.UpdateUser(ctx)
}
func (api *FakePMAPI) UpdateUser(context.Context) (*pmapi.User, error) {
if err := api.checkAndRecordCall(GET, "/users", nil); err != nil {
return nil, err
}
if err := api.checkAndRecordCall(GET, "/addresses", nil); err != nil {
return nil, err
}
return api.user, nil
}
func (api *FakePMAPI) GetUser(ctx context.Context) (*pmapi.User, error) {
if err := api.checkAndRecordCall(GET, "/users", nil); err != nil {
return nil, err
}
return api.user, nil
}
func (api *FakePMAPI) GetAddresses(context.Context) (pmapi.AddressList, error) {
if err := api.checkAndRecordCall(GET, "/addresses", nil); err != nil {
return nil, err
}
return *api.addresses, nil
}
func (api *FakePMAPI) ReorderAddresses(_ context.Context, addressIDs []string) error {
if err := api.checkAndRecordCall(PUT, "/addresses/order", nil); err != nil {
return err
}
for wantedIndex, addressID := range addressIDs {
var currentIndex int
for i, v := range *api.addresses {
if v.ID == addressID {
currentIndex = i
break
}
}
(*api.addresses)[wantedIndex], (*api.addresses)[currentIndex] = (*api.addresses)[currentIndex], (*api.addresses)[wantedIndex]
(*api.addresses)[wantedIndex].Order = wantedIndex + 1 // Starts counting from 1.
api.addEventAddress(pmapi.EventUpdate, (*api.addresses)[wantedIndex])
}
return nil
}
func (api *FakePMAPI) Addresses() pmapi.AddressList {
if api.controller.noInternetConnection {
return nil
}
if api.addresses == nil {
return pmapi.AddressList{}
}
return *api.addresses
}
func (api *FakePMAPI) GetUserKeyRing() (*crypto.KeyRing, error) {
return api.userKeyRing, nil
}
func (api *FakePMAPI) KeyRingForAddressID(addrID string) (*crypto.KeyRing, error) {
return api.addrKeyRing[addrID], nil
}