-
Notifications
You must be signed in to change notification settings - Fork 9
/
userstate.go
620 lines (531 loc) · 19.6 KB
/
userstate.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package permissionsql
import (
"errors"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/xyproto/cookie/v2" // For cookies
"github.com/xyproto/pinterface" // For interfaces
db "github.com/xyproto/simplemaria" // MariaDB/MySQL database wrapper
)
const (
// username:password@host:port/database
defaultConnectionString = "localhost:3306/"
)
var (
minConfirmationCodeLength = 20 // minimum length of the confirmation code
ErrCookieGetUsername = errors.New("Could not retrieve the username from browser cookie")
ErrCookieEmptyUsername = errors.New("Can't set cookie for empty username")
ErrCookieUserMissing = errors.New("Can't store cookie for non-existsing user")
ErrOutOfConfirmationCodes = errors.New("Too many generated confirmation codes are not unique")
ErrAllUsersConfirmedAlready = errors.New("All existing users are already confirmed")
ErrConfirmationCodeExpired = errors.New("The confirmation code is no longer valid")
ErrMissingUserAtConfirm = errors.New("The user that is to be confirmed no longer exists")
ErrInvalidCharacters = errors.New("Only letters, numbers and underscore are allowed in usernames")
ErrUsernameAsPassword = errors.New("Username and password must be different, try another password")
)
type UserState struct {
users *db.HashMap // Hash map of users, with several different fields per user ("loggedin", "confirmed", "email" etc)
usernames *db.Set // A list of all usernames, for easy enumeration
unconfirmed *db.Set // A list of unconfirmed usernames, for easy enumeration
host *db.Host // A database host
cookieSecret string // Secret for storing secure cookies
cookieTime int64 // How long a cookie should last, in seconds
passwordAlgorithm string // The hashing algorithm to utilize default: "bcrypt+" allowed: ("sha256", "bcrypt", "bcrypt+")
}
// Create a new *UserState that can be used for managing users.
// The random number generator will be seeded after generating the cookie secret.
// A Host* for the local MariaDB/MySQL server will be created.
func NewUserStateSimple() (*UserState, error) {
// connection string | initialize random generator after generating the cookie secret
return NewUserState(defaultConnectionString, true)
}
// Create a new *UserState that can be used for managing users.
// connectionString may be on the form "username:password@host:port/database".
// If randomseed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
func NewUserStateWithDSN(connectionString string, database_name string, randomseed bool) (*UserState, error) {
// Test connection
if err := db.TestConnectionHostWithDSN(connectionString); err != nil {
return nil, err
}
host := db.NewHostWithDSN(connectionString, database_name)
state := new(UserState)
var err error
state.users, err = db.NewHashMap(host, "users")
if err != nil {
return nil, err
}
state.usernames, err = db.NewSet(host, "usernames")
if err != nil {
return nil, err
}
state.unconfirmed, err = db.NewSet(host, "unconfirmed")
if err != nil {
return nil, err
}
state.host = host
// For the secure cookies
// This must happen before the random seeding, or
// else people will have to log in again after every server restart
state.cookieSecret = cookie.RandomCookieFriendlyString(30)
// Seed the random number generator
if randomseed {
rand.Seed(time.Now().UnixNano())
}
// Cookies lasts for 24 hours by default. Specified in seconds.
state.cookieTime = 3600 * 24
// Default password hashing algorithm is "bcrypt+", which is the same as
// "bcrypt", but with backwards compatibility for checking sha256 hashes.
state.passwordAlgorithm = "bcrypt+" // "bcrypt+", "bcrypt" or "sha256"
if err := host.Ping(); err != nil {
defer host.Close()
return nil, fmt.Errorf("Error when pinging %s: %s", connectionString, err)
}
return state, nil
}
// Create a new *UserState that can be used for managing users.
// connectionString may be on the form "username:password@host:port/database".
// If randomseed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
func NewUserState(connectionString string, randomseed bool) (*UserState, error) {
// Test connection
if err := db.TestConnectionHost(connectionString); err != nil {
return nil, err
}
host := db.NewHost(connectionString)
state := new(UserState)
var err error
state.users, err = db.NewHashMap(host, "users")
if err != nil {
return nil, err
}
state.usernames, err = db.NewSet(host, "usernames")
if err != nil {
return nil, err
}
state.unconfirmed, err = db.NewSet(host, "unconfirmed")
if err != nil {
return nil, err
}
state.host = host
// For the secure cookies
// This must happen before the random seeding, or
// else people will have to log in again after every server restart
state.cookieSecret = cookie.RandomCookieFriendlyString(30)
// Seed the random number generator
if randomseed {
rand.Seed(time.Now().UnixNano())
}
// Cookies lasts for 24 hours by default. Specified in seconds.
state.cookieTime = 3600 * 24
// Default password hashing algorithm is "bcrypt+", which is the same as
// "bcrypt", but with backwards compatibility for checking sha256 hashes.
state.passwordAlgorithm = "bcrypt+" // "bcrypt+", "bcrypt" or "sha256"
if err := host.Ping(); err != nil {
defer host.Close()
return nil, fmt.Errorf("Error when pinging %s: %s", connectionString, err)
}
return state, nil
}
// Get the database host
func (state *UserState) Host() pinterface.IHost {
return state.host
}
// Close the connection to the database host
func (state *UserState) Close() {
state.host.Close()
}
// Check if the current user is logged in and has user rights.
func (state *UserState) UserRights(req *http.Request) bool {
username, err := state.UsernameCookie(req)
if err != nil {
return false
}
return state.IsLoggedIn(username)
}
// Check if the given username exists.
func (state *UserState) HasUser(username string) bool {
val, err := state.usernames.Has(username)
if err != nil {
// This happened at concurrent connections before introducing the connection pool
panic("ERROR: Lost connection to database?")
}
return val
}
// Return the boolean value for a given username and fieldname.
// If the user or field is missing, false will be returned.
// Useful for states where it makes sense that the returned value is not true
// unless everything is in order.
func (state *UserState) BooleanField(username, fieldname string) bool {
hasUser := state.HasUser(username)
if !hasUser {
return false
}
value, err := state.users.Get(username, fieldname)
if err != nil {
return false
}
return value == "true"
}
// Store a boolean value for the given username and custom fieldname.
func (state *UserState) SetBooleanField(username, fieldname string, val bool) {
strval := "false"
if val {
strval = "true"
}
state.users.Set(username, fieldname, strval)
}
// Check if the given username is confirmed.
func (state *UserState) IsConfirmed(username string) bool {
return state.BooleanField(username, "confirmed")
}
// Checks if the given username is logged in.
func (state *UserState) IsLoggedIn(username string) bool {
if !state.HasUser(username) {
return false
}
status, err := state.users.Get(username, "loggedin")
if err != nil {
// Returns "no" if the status can not be retrieved
return false
}
return status == "true"
}
// Check if the current user is logged in and has administrator rights.
func (state *UserState) AdminRights(req *http.Request) bool {
username, err := state.UsernameCookie(req)
if err != nil {
return false
}
return state.IsLoggedIn(username) && state.IsAdmin(username)
}
// Check if the given username is an administrator.
func (state *UserState) IsAdmin(username string) bool {
if !state.HasUser(username) {
return false
}
status, err := state.users.Get(username, "admin")
if err != nil {
return false
}
return status == "true"
}
// Retrieve the username that is stored in a cookie in the browser, if available.
func (state *UserState) UsernameCookie(req *http.Request) (string, error) {
username, ok := cookie.SecureCookie(req, "user", state.cookieSecret)
if ok && (username != "") {
return username, nil
}
return "", ErrCookieGetUsername
}
// Store the given username in a cookie in the browser, if possible.
// The user must exist.
func (state *UserState) SetUsernameCookie(w http.ResponseWriter, username string) error {
if username == "" {
return ErrCookieEmptyUsername
}
if !state.HasUser(username) {
return ErrCookieUserMissing
}
// Create a cookie that lasts for a while ("timeout" seconds),
// this is the equivivalent of a session for a given username.
cookie.SetSecureCookiePath(w, "user", username, state.cookieTime, "/", state.cookieSecret)
return nil
}
// Get a list of all usernames.
func (state *UserState) AllUsernames() ([]string, error) {
return state.usernames.GetAll()
}
// Get the email for the given username.
func (state *UserState) Email(username string) (string, error) {
return state.users.Get(username, "email")
}
// Get the password hash for the given username.
func (state *UserState) PasswordHash(username string) (string, error) {
return state.users.Get(username, "password")
}
// Get all registered users that are not yet confirmed.
func (state *UserState) AllUnconfirmedUsernames() ([]string, error) {
return state.unconfirmed.GetAll()
}
// Get the confirmation code for a specific user.
func (state *UserState) ConfirmationCode(username string) (string, error) {
return state.users.Get(username, "confirmationCode")
}
// Get the users HashMap.
func (state *UserState) Users() pinterface.IHashMap {
return state.users
}
// Add a user that is registered but not confirmed.
func (state *UserState) AddUnconfirmed(username, confirmationCode string) {
state.unconfirmed.Add(username)
state.users.Set(username, "confirmationCode", confirmationCode)
}
// Remove a user that is registered but not confirmed.
func (state *UserState) RemoveUnconfirmed(username string) {
state.unconfirmed.Del(username)
state.users.DelKey(username, "confirmationCode")
}
// Mark a user as confirmed.
func (state *UserState) MarkConfirmed(username string) {
state.users.Set(username, "confirmed", "true")
}
// Remove user and login status.
func (state *UserState) RemoveUser(username string) {
state.usernames.Del(username)
// Remove additional data as well
//state.users.DelKey(username, "loggedin")
state.users.Del(username)
}
// Mark user as an administrator.
func (state *UserState) SetAdminStatus(username string) {
state.users.Set(username, "admin", "true")
}
// Mark user as a regular user.
func (state *UserState) RemoveAdminStatus(username string) {
state.users.Set(username, "admin", "false")
}
// Creates a user from the username and password hash, does not check for rights.
func (state *UserState) addUserUnchecked(username, passwordHash, email string) {
// Add the user
state.usernames.Add(username)
// Add password and email
state.users.Set(username, "password", passwordHash)
state.users.Set(username, "email", email)
// Addditional fields
additionalfields := []string{"loggedin", "confirmed", "admin"}
for _, fieldname := range additionalfields {
state.users.Set(username, fieldname, "false")
}
}
// Creates a user and hashes the password, does not check for rights.
// The given data must be valid.
func (state *UserState) AddUser(username, password, email string) {
passwordHash := state.HashPassword(username, password)
state.addUserUnchecked(username, passwordHash, email)
}
// Mark the user as logged in. Use the Login function instead, unless cookies are not involved.
func (state *UserState) SetLoggedIn(username string) {
state.users.Set(username, "loggedin", "true")
}
// Mark the user as logged out.
func (state *UserState) SetLoggedOut(username string) {
state.users.Set(username, "loggedin", "false")
}
// Convenience function for logging a user in and storing the username in a cookie.
// Returns an error if the cookie could not be set.
func (state *UserState) Login(w http.ResponseWriter, username string) error {
state.SetLoggedIn(username)
return state.SetUsernameCookie(w, username)
}
// Try to clear the user cookie by setting it to expired.
// Some browsers *may* be configured to keep cookies even after this.
func (state *UserState) ClearCookie(w http.ResponseWriter) {
cookie.ClearCookie(w, "user", "/")
}
// Convenience function for logging a user out.
func (state *UserState) Logout(username string) {
state.SetLoggedOut(username)
}
// Convenience function that will return a username (from the browser cookie) or an empty string.
func (state *UserState) Username(req *http.Request) string {
username, err := state.UsernameCookie(req)
if err != nil {
return ""
}
return username
}
// Get how long a login cookie should last, in seconds.
func (state *UserState) CookieTimeout(username string) int64 {
return state.cookieTime
}
// Set how long a login cookie should last, in seconds.
func (state *UserState) SetCookieTimeout(cookieTime int64) {
state.cookieTime = cookieTime
}
// CookieSecret returns the current cookie secret
func (state *UserState) CookieSecret() string {
return state.cookieSecret
}
// SetCookieSecret sets the current cookie secret
func (state *UserState) SetCookieSecret(cookieSecret string) {
state.cookieSecret = cookieSecret
}
// PasswordAlgo returns the current password hashing algorithm.
func (state *UserState) PasswordAlgo() string {
return state.passwordAlgorithm
}
// Set the password hashing algorithm that should be used.
// The default is "bcrypt+".
// Possible values are:
//
// bcrypt -> Store and check passwords with the bcrypt hash.
// sha256 -> Store and check passwords with the sha256 hash.
// bcrypt+ -> Store passwords with bcrypt, but check with both
// bcrypt and sha256, for backwards compatibility
// with old passwords that has been stored as sha256.
func (state *UserState) SetPasswordAlgo(algorithm string) error {
switch algorithm {
case "sha256", "bcrypt", "bcrypt+":
state.passwordAlgorithm = algorithm
default:
return errors.New("Permissions: " + algorithm + " is an unsupported encryption algorithm")
}
return nil
}
// Hash the password (takes a username as well, it can be used for salting).
func (state *UserState) HashPassword(username, password string) string {
switch state.passwordAlgorithm {
case "sha256":
return string(hash_sha256(state.cookieSecret, username, password))
case "bcrypt", "bcrypt+":
return string(hash_bcrypt(password))
}
// Only valid password algorithms should be allowed to set
return ""
}
// SetPassword sets/changes the password for a user.
// Does not take a password hash, will hash the password string.
func (state *UserState) SetPassword(username, password string) {
state.users.Set(username, "password", state.HashPassword(username, password))
}
// Return the stored hash, or an empty byte slice.
func (state *UserState) stored_hash(username string) []byte {
hashString, err := state.PasswordHash(username)
if err != nil {
return []byte{}
}
return []byte(hashString)
}
// Check if a password is correct. username is needed because it is part of the hash.
func (state *UserState) CorrectPassword(username, password string) bool {
if !state.HasUser(username) {
return false
}
// Retrieve the stored password hash
hash := state.stored_hash(username)
if len(hash) == 0 {
return false
}
// Check the password with the right password algorithm
switch state.passwordAlgorithm {
case "sha256":
return correct_sha256(hash, state.cookieSecret, username, password)
case "bcrypt":
return correct_bcrypt(hash, password)
case "bcrypt+": // for backwards compatibility with sha256
if is_sha256(hash) && correct_sha256(hash, state.cookieSecret, username, password) {
return true
}
return correct_bcrypt(hash, password)
}
return false
}
// Goes through all the confirmationCodes of all the unconfirmed users
// and checks if this confirmationCode already is in use.
func (state *UserState) AlreadyHasConfirmationCode(confirmationCode string) bool {
unconfirmedUsernames, err := state.AllUnconfirmedUsernames()
if err != nil {
return false
}
for _, aUsername := range unconfirmedUsernames {
aConfirmationCode, err := state.ConfirmationCode(aUsername)
if err != nil {
// If the confirmation code can not be found, that's okay too
return false
}
if confirmationCode == aConfirmationCode {
// Found it
return true
}
}
return false
}
// Given a unique confirmation code, find the corresponding username.
func (state *UserState) FindUserByConfirmationCode(confirmationcode string) (string, error) {
unconfirmedUsernames, err := state.AllUnconfirmedUsernames()
if err != nil {
return "", ErrAllUsersConfirmedAlready
}
// Find the username by looking up the confirmationcode on unconfirmed users
username := ""
for _, aUsername := range unconfirmedUsernames {
aConfirmationCode, err := state.ConfirmationCode(aUsername)
if err != nil {
// If the confirmation code can not be found, just skip this one
continue
}
if confirmationcode == aConfirmationCode {
// Found the right user
username = aUsername
break
}
}
// Check that the user is there
if username == "" {
return username, ErrConfirmationCodeExpired
}
if !state.HasUser(username) {
return username, ErrMissingUserAtConfirm
}
return username, nil
}
// Remove the username from the list of unconfirmed users and mark the user as confirmed.
func (state *UserState) Confirm(username string) {
// Remove from the list of unconfirmed usernames
state.RemoveUnconfirmed(username)
// Mark user as confirmed
state.MarkConfirmed(username)
}
// Take a confirmation code and mark the corresponding unconfirmed user as confirmed.
func (state *UserState) ConfirmUserByConfirmationCode(confirmationcode string) error {
username, err := state.FindUserByConfirmationCode(confirmationcode)
if err != nil {
return err
}
state.Confirm(username)
return nil
}
// Set the minimum length of the user confirmation code. The default is 20.
func (state *UserState) SetMinimumConfirmationCodeLength(length int) {
minConfirmationCodeLength = length
}
// Generate a unique confirmation code that can be used for confirming users.
func (state *UserState) GenerateUniqueConfirmationCode() (string, error) {
const maxConfirmationCodeLength = 100 // when are the generated confirmation codes unreasonably long
length := minConfirmationCodeLength
confirmationCode := cookie.RandomHumanFriendlyString(length)
for state.AlreadyHasConfirmationCode(confirmationCode) {
// Increase the length of the confirmationCode random string every time there is a collision
length++
confirmationCode = cookie.RandomHumanFriendlyString(length)
if length > maxConfirmationCodeLength {
// This should never happen
return confirmationCode, ErrOutOfConfirmationCodes
}
}
return confirmationCode, nil
}
// Check that the given username and password are different.
// Also check if the chosen username only contains letters, numbers and/or underscore.
// Use the "CorrectPassword" function for checking if the password is correct.
func ValidUsernamePassword(username, password string) error {
const allowed_letters = "abcdefghijklmnopqrstuvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ_0123456789"
NEXT:
for _, letter := range username {
for _, allowedLetter := range allowed_letters {
if letter == allowedLetter {
continue NEXT // check the next letter in the username
}
}
return ErrInvalidCharacters
}
if username == password {
return ErrUsernameAsPassword
}
return nil
}
// Return a struct for creating datastructures
func (state *UserState) Creator() pinterface.ICreator {
return db.NewCreator(state.host)
}