-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccount.go
245 lines (211 loc) · 5.61 KB
/
account.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
// SPDX-FileCopyrightText: © 2020 Etebase Authors
// SPDX-License-Identifier: BSD-3-Clause
package etebase
import (
"log"
"github.com/etesync/etebase-go/internal/codec"
"github.com/etesync/etebase-go/internal/crypto"
)
// Account represents a user account and is the main object for all user
// interactions and data manipulation.
type Account struct {
client *Client
session *LoginResponse
salt []byte
mainKey, accountKey []byte
authPub, authPriv []byte
idPub, idPriv []byte
}
// newAccount returns a new Account object.
func newAccount(c *Client) *Account {
acc := &Account{
client: c,
}
return acc
}
func (acc *Account) initKeys(salt []byte, password string) (err error) {
acc.salt = salt
acc.mainKey = crypto.DeriveKey(acc.salt, password)
acc.accountKey, err = crypto.Rand(32)
if err != nil {
return err
}
acc.authPub, acc.authPriv = crypto.GenrateKeyPair(acc.mainKey)
r, err := crypto.Rand(32)
if err != nil {
return err
}
acc.idPub, acc.idPriv = crypto.GenrateKeyPair(r)
return nil
}
// IsEtebaseServer checks whether the Client is pointing to a valid Etebase
// server.
func (acc *Account) IsEtebaseServer() (bool, error) {
resp, err := acc.client.Get("/authentication/is_etebase/")
if err != nil {
return false, err
}
if err := resp.Body.Close(); err != nil {
return false, err
}
return resp.StatusCode != 404, nil
}
func (acc *Account) signup(user User, password string) error {
r, err := crypto.Rand(32)
if err != nil {
return err
}
err = acc.initKeys(r, password)
if err != nil {
return err
}
encrypedContent, err := crypto.Encrypt(acc.mainKey, append(acc.accountKey, acc.idPriv...))
if err != nil {
return err
}
body := SignupRequest{
User: user,
Salt: acc.salt,
LoginPubkey: acc.authPub,
PubKey: acc.idPub,
EncryptedContent: encrypedContent,
}
resp, err := acc.client.Post("/authentication/signup", body)
if err != nil {
return err
}
defer resp.Body.Close()
var loginResponse LoginResponse
if err := codec.NewDecoder(resp.Body).Decode(&loginResponse); err != nil {
return err
}
acc.session = &loginResponse
return nil
}
func (acc *Account) login(username, password string) error {
lc, err := acc.loginChallenge(username)
if err != nil {
return err
}
err = acc.initKeys(lc.Salt, password)
if err != nil {
return err
}
req := LoginRequest{
Username: username,
Challenge: lc.Challenge,
Host: acc.client.Host(),
Action: "login",
}
buf, err := codec.Marshal(req)
if err != nil {
return err
}
sig := crypto.Sign(acc.authPriv, buf)
resp, err := acc.client.Post("/authentication/login", struct {
Response []byte `msgpack:"response"`
Signature []byte `msgpack:"signature"`
}{buf, sig})
if err != nil {
return err
}
defer resp.Body.Close()
var loginResponse LoginResponse
if err := codec.NewDecoder(resp.Body).Decode(&loginResponse); err != nil {
return err
}
acc.session = &loginResponse
return nil
}
func (acc *Account) loginChallenge(username string) (*LoginChallengeResponse, error) {
resp, err := acc.client.Post("/authentication/login_challenge", &LoginChallengeRequest{
Username: username,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var challenge LoginChallengeResponse
if err := codec.NewDecoder(resp.Body).Decode(&challenge); err != nil {
return nil, err
}
return &challenge, nil
}
// PasswordChange changes the password of an active session.
func (acc *Account) PasswordChange(newPassword string) error {
lc, err := acc.loginChallenge(acc.session.User.Username)
if err != nil {
return err
}
priv := acc.authPriv
err = acc.initKeys(lc.Salt, newPassword)
if err != nil {
return err
}
encrypedContent, err := crypto.Encrypt(acc.mainKey, append(acc.accountKey, acc.idPriv...))
if err != nil {
return err
}
req := LoginRequest{
Username: acc.session.User.Username,
Challenge: lc.Challenge,
Host: acc.client.Host(),
Action: "changePassword",
LoginPubKey: acc.authPub,
EncryptedContent: encrypedContent,
}
buf, err := codec.Marshal(req)
if err != nil {
return err
}
sig := crypto.Sign(priv, buf)
resp, err := acc.client.WithToken(acc.session.Token).Post("/authentication/change_password", struct {
Response []byte `msgpack:"response"`
Signature []byte `msgpack:"signature"`
}{buf, sig})
if err != nil {
return err
}
// We don't expect any content from the server.
return resp.Body.Close()
}
// Logout the user from the current session and invalidate the authentication
// token.
func (acc *Account) Logout() error {
resp, err := acc.client.WithToken(acc.session.Token).Post("/authentication/logout/", nil)
if err != nil {
return err
}
return resp.Body.Close()
}
// Collection is not implemented yet.
func (acc *Account) Collection() error {
resp, err := acc.client.WithToken(acc.session.Token).Post("/collection/", nil)
if err != nil {
return err
}
defer resp.Body.Close()
log.Printf("resp.Status = %+v\n", resp.Status)
var body interface{}
if err := codec.NewDecoder(resp.Body).Decode(&body); err != nil {
return err
}
log.Printf("body = %+v\n", body)
return err
}
// Signup a new user account and returns an Account instance.
func Signup(c *Client, user User, password string) (*Account, error) {
acc := newAccount(c)
if err := acc.signup(user, password); err != nil {
return nil, err
}
return acc, nil
}
// Login a user and returns a handle to an Account instance.
func Login(c *Client, username, password string) (*Account, error) {
acc := newAccount(c)
if err := acc.login(username, password); err != nil {
return nil, err
}
return acc, nil
}