-
Notifications
You must be signed in to change notification settings - Fork 2
/
users.go
148 lines (124 loc) · 3.12 KB
/
users.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
// Copyright (c) 2022 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail 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.
//
// ProtonMail 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 ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package pmapi
import (
"context"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
)
// Role values.
const (
FreeUserRole = iota
PaidMemberRole
PaidAdminRole
)
// User status.
const (
DeletedUser = 0
DisabledUser = 1
ActiveUser = 2
VPNAdminUser = 3
AdminUser = 4
SuperUser = 5
)
// Delinquent values.
const (
CurrentUser = iota
AvailableUser
OverdueUser
DelinquentUser
NoReceiveUser
)
// PMSignature values.
const (
PMSignatureDisabled = iota
PMSignatureEnabled
PMSignatureLocked
)
// User holds the user details.
type User struct {
ID string
Name string
UsedSpace *int64
Currency string
Credit int
MaxSpace *int64
MaxUpload int64
Role int
Private int
Subscribed int
Services int
Deliquent int
Keys PMKeys
VPN struct {
Status int
ExpirationTime int
PlanName string
MaxConnect int
MaxTier int
}
}
func (c *client) GetUser(ctx context.Context) (user *User, err error) {
var res struct {
User *User
}
if _, err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).Get("/users")
}); err != nil {
return nil, err
}
return res.User, nil
}
// unlockUser unlocks all the client's user keys using the given passphrase.
func (c *client) unlockUser(passphrase []byte) (err error) {
if c.user == nil {
return errors.New("user data is not loaded")
}
if c.userKeyRing, err = c.user.Keys.UnlockAll(passphrase, nil); err != nil {
return errors.Wrap(err, "failed to unlock user keys")
}
return
}
// UpdateUser retrieves details about user and loads its addresses.
func (c *client) UpdateUser(ctx context.Context) (*User, error) {
user, err := c.GetUser(ctx)
if err != nil {
return nil, err
}
addresses, err := c.GetAddresses(ctx)
if err != nil {
return nil, err
}
c.user = user
c.addresses = addresses
return user, err
}
// CurrentUser returns currently active user or user will be updated.
func (c *client) CurrentUser(ctx context.Context) (*User, error) {
if c.user != nil && len(c.addresses) != 0 {
return c.user, nil
}
return c.UpdateUser(ctx)
}
// CurrentUser returns currently active user or user will be updated.
func (c *client) GetUserKeyRing() (*crypto.KeyRing, error) {
if c.userKeyRing == nil {
return nil, errors.New("user keyring is not available")
}
return c.userKeyRing, nil
}