forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
346 lines (315 loc) · 7.8 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
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
337
338
339
340
341
342
343
344
345
346
// Copyright 2014 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth
import (
"bytes"
"code.google.com/p/go.crypto/bcrypt"
stderrors "errors"
"fmt"
"github.com/globocom/tsuru/db"
"github.com/globocom/tsuru/errors"
"github.com/globocom/tsuru/log"
"github.com/globocom/tsuru/quota"
"github.com/globocom/tsuru/repository"
"github.com/globocom/tsuru/validation"
"github.com/tsuru/config"
"github.com/tsuru/go-gandalfclient"
"labix.org/v2/mgo/bson"
"math/rand"
"net"
"net/smtp"
"strings"
"time"
)
const (
defaultExpiration = 7 * 24 * time.Hour
emailError = "Invalid email."
passwordError = "Password length should be least 6 characters and at most 50 characters."
passwordMinLen = 6
passwordMaxLen = 50
)
var ErrUserNotFound = stderrors.New("User not found")
var tokenExpire time.Duration
var cost int
func loadConfig() error {
if cost == 0 && tokenExpire == 0 {
var err error
if days, err := config.GetInt("auth:token-expire-days"); err == nil {
tokenExpire = time.Duration(int64(days) * 24 * int64(time.Hour))
} else {
tokenExpire = defaultExpiration
}
if cost, err = config.GetInt("auth:hash-cost"); err != nil {
cost = bcrypt.DefaultCost
}
if cost < bcrypt.MinCost || cost > bcrypt.MaxCost {
return fmt.Errorf("Invalid value for setting %q: it must be between %d and %d.", "auth:hash-cost", bcrypt.MinCost, bcrypt.MaxCost)
}
}
return nil
}
type Key struct {
Name string
Content string
}
type User struct {
Email string
Password string
Keys []Key
quota.Quota
}
func GetUserByEmail(email string) (*User, error) {
if !validation.ValidateEmail(email) {
return nil, &errors.ValidationError{Message: emailError}
}
var u User
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
err = conn.Users().Find(bson.M{"email": email}).One(&u)
if err != nil {
return nil, ErrUserNotFound
}
return &u, nil
}
func (u *User) Create() error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
u.HashPassword()
return conn.Users().Insert(u)
}
func (u *User) Update() error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
return conn.Users().Update(bson.M{"email": u.Email}, u)
}
func (u *User) HashPassword() {
if passwd, err := bcrypt.GenerateFromPassword([]byte(u.Password), cost); err == nil {
u.Password = string(passwd)
}
}
func (u *User) CheckPassword(password string) error {
if !validation.ValidateLength(password, passwordMinLen, passwordMaxLen) {
return &errors.ValidationError{Message: passwordError}
}
if bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) == nil {
return nil
}
return AuthenticationFailure{}
}
func (u *User) CreateToken(password string) (*Token, error) {
if u.Email == "" {
return nil, stderrors.New("User does not have an email")
}
if err := u.CheckPassword(password); err != nil {
return nil, err
}
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
t, err := newUserToken(u)
if err != nil {
return nil, err
}
err = conn.Tokens().Insert(t)
go removeOldTokens(u.Email)
return t, err
}
// Teams returns a slice containing all teams that the user is member of.
func (u *User) Teams() ([]Team, error) {
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
var teams []Team
err = conn.Teams().Find(bson.M{"users": u.Email}).All(&teams)
if err != nil {
return nil, err
}
return teams, nil
}
func (u *User) FindKey(key Key) (Key, int) {
for i, k := range u.Keys {
if k.Content == key.Content {
return k, i
}
}
return Key{}, -1
}
func (u *User) HasKey(key Key) bool {
_, index := u.FindKey(key)
return index > -1
}
func (u *User) AddKey(key Key) error {
u.Keys = append(u.Keys, key)
return nil
}
func (u *User) RemoveKey(key Key) error {
_, index := u.FindKey(key)
if index < 0 {
return stderrors.New("Key not found")
}
copy(u.Keys[index:], u.Keys[index+1:])
u.Keys = u.Keys[:len(u.Keys)-1]
return nil
}
func (u *User) IsAdmin() bool {
adminTeamName, err := config.GetString("admin-team")
if err != nil {
return false
}
teams, err := u.Teams()
if err != nil {
return false
}
for _, t := range teams {
if t.Name == adminTeamName {
return true
}
}
return false
}
func (u *User) AllowedApps() ([]string, error) {
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
var alwdApps []map[string]string
teams, err := u.Teams()
if err != nil {
return nil, err
}
teamNames := GetTeamsNames(teams)
q := bson.M{"teams": bson.M{"$in": teamNames}}
if err := conn.Apps().Find(q).Select(bson.M{"name": 1}).All(&alwdApps); err != nil {
return nil, err
}
appNames := make([]string, len(alwdApps))
for i, v := range alwdApps {
appNames[i] = v["name"]
}
return appNames, nil
}
// StartPasswordReset starts the password reset process, creating a new token
// and mailing it to the user.
//
// The token should then be used to finish the process, through the
// ResetPassword function.
func (u *User) StartPasswordReset() error {
t, err := createPasswordToken(u)
if err != nil {
return err
}
go u.sendResetPassword(t)
return nil
}
func (u *User) sendResetPassword(t *passwordToken) {
var body bytes.Buffer
err := resetEmailData.Execute(&body, t)
if err != nil {
log.Errorf("Failed to send password token to user %q: %s", u.Email, err)
return
}
err = sendEmail(u.Email, body.Bytes())
if err != nil {
log.Errorf("Failed to send password token for user %q: %s", u.Email, err)
}
}
// ResetPassword actually resets the password of the user. It needs the token
// string. The new password will be a random string, that will be then sent to
// the user email.
func (u *User) ResetPassword(token string) error {
if token == "" {
return ErrInvalidToken
}
conn, err := db.Conn()
if err != nil {
return err
}
t, err := getPasswordToken(token)
if err != nil {
return err
}
if t.UserEmail != u.Email {
return ErrInvalidToken
}
password := generatePassword(12)
u.Password = password
u.HashPassword()
go u.sendNewPassword(password)
t.Used = true
conn.PasswordTokens().UpdateId(t.Token, t)
return u.Update()
}
func (u *User) sendNewPassword(password string) {
m := map[string]string{
"password": password,
"email": u.Email,
}
var body bytes.Buffer
err := passwordResetConfirm.Execute(&body, m)
if err != nil {
log.Errorf("Failed to send new password to user %q: %s", u.Email, err)
return
}
err = sendEmail(u.Email, body.Bytes())
if err != nil {
log.Errorf("Failed to send new password to user %q: %s", u.Email, err)
}
}
func (u *User) ListKeys() (map[string]string, error) {
gURL := repository.ServerURL()
c := gandalf.Client{Endpoint: gURL}
return c.ListKeys(u.Email)
}
type AuthenticationFailure struct{}
func (AuthenticationFailure) Error() string {
return "Authentication failed, wrong password."
}
func generatePassword(length int) string {
password := make([]byte, length)
for i := range password {
password[i] = passwordChars[rand.Int()%len(passwordChars)]
}
return string(password)
}
func sendEmail(email string, data []byte) error {
addr, err := smtpServer()
if err != nil {
return err
}
var auth smtp.Auth
user, err := config.GetString("smtp:user")
if err != nil {
return stderrors.New(`Setting "smtp:user" is not defined`)
}
password, _ := config.GetString("smtp:password")
if password != "" {
host, _, _ := net.SplitHostPort(addr)
auth = smtp.PlainAuth("", user, password, host)
}
return smtp.SendMail(addr, auth, user, []string{email}, data)
}
func smtpServer() (string, error) {
server, _ := config.GetString("smtp:server")
if server == "" {
return "", stderrors.New(`Setting "smtp:server" is not defined`)
}
if !strings.Contains(server, ":") {
server += ":25"
}
return server, nil
}