forked from xyproto/permissions2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserstate.go
734 lines (629 loc) · 25 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package permissions
import (
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"strings"
"time"
"github.com/xyproto/cookie"
"github.com/xyproto/pinterface"
"github.com/xyproto/simpleredis"
)
const (
defaultRedisServer = ":6379"
)
var (
minConfirmationCodeLength = 20 // minimum length of the confirmation code
)
// Used for dealing with the user state, users and passwords.
// Can also be used for retrieving the underlying Redis connection pool.
type UserState struct {
// see: http://redis.io/topics/data-types
users *simpleredis.HashMap // Hash map of users, with several different fields per user ("loggedin", "confirmed", "email" etc)
usernames *simpleredis.Set // A list of all usernames, for easy enumeration
unconfirmed *simpleredis.Set // A list of unconfirmed usernames, for easy enumeration
pool *simpleredis.ConnectionPool // A connection pool for Redis
dbindex int // Redis database index
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 connection pool for the local Redis server (dbindex 0) will be created.
// Calls log.Fatal if things go wrong.
func NewUserStateSimple() *UserState {
// db index 0, initialize random generator after generating the cookie secret
return NewUserState(0, true, defaultRedisServer)
}
// Create a new *UserState that can be used for managing users.
// The random number generator will be seeded after generating the cookie secret.
// A connection pool for the local Redis server (dbindex 0) will be created.
// Returns an error if things go wrong.
func NewUserStateSimple2() (*UserState, error) {
// db index 0, initialize random generator after generating the cookie secret
return NewUserState2(0, true, defaultRedisServer)
}
// Same as NewUserStateSimple, but takes a hostname and a password.
// Use NewUserState for control over the database index and port number.
// Calls log.Fatal if things go wrong.
func NewUserStateWithPassword(hostname, password string) *UserState {
// db index 0, initialize random generator after generating the cookie secret, password
connectTo := hostname
if (password == "") && (strings.Count(hostname, ":") == 0) {
connectTo = hostname + ":6379"
} else if strings.Count(hostname, ":") > 0 {
connectTo = password + "@" + hostname
} else {
connectTo = password + "@" + hostname + ":6379"
}
// Create a new UserState with database index 0, "true" for seeding the
// random number generator and host string
return NewUserState(0, true, connectTo)
}
// Same as NewUserStateSimple, but takes a hostname and a password.
// Use NewUserState for control over the database index and port number.
// Returns an error if things go wrong.
func NewUserStateWithPassword2(hostname, password string) (*UserState, error) {
// db index 0, initialize random generator after generating the cookie secret, password
connectTo := hostname
if (password == "") && (strings.Count(hostname, ":") == 0) {
connectTo = hostname + ":6379"
} else if strings.Count(hostname, ":") > 0 {
connectTo = password + "@" + hostname
} else {
connectTo = password + "@" + hostname + ":6379"
}
// Create a new UserState with database index 0, "true" for seeding the
// random number generator and host string
return NewUserState2(0, true, connectTo)
}
// Create a new *UserState that can be used for managing users.
// dbindex is the Redis database index (0 is a good default value).
// If randomseed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
// redisHostPort is host:port for the desired Redis server (can be blank for localhost)
// Also creates a new ConnectionPool.
// Calls log.Fatal if things go wrong.
func NewUserState(dbindex int, randomseed bool, redisHostPort string) *UserState {
var pool *simpleredis.ConnectionPool
// Connect to the default redis server if redisHostPort is empty
if redisHostPort == "" {
redisHostPort = defaultRedisServer
}
// Test connection (the client can do this test before creating a new userstate)
if err := simpleredis.TestConnectionHost(redisHostPort); err != nil {
errorMessage := err.Error()
if errorMessage == "dial tcp :6379: getsockopt: connection refused" {
log.Fatalln("Fatal: Unable to connect to Redis server on port 6379.")
}
log.Fatalln(errorMessage)
}
// Acquire connection pool
pool = simpleredis.NewConnectionPoolHost(redisHostPort)
state := new(UserState)
state.users = simpleredis.NewHashMap(pool, "users")
state.users.SelectDatabase(dbindex)
state.usernames = simpleredis.NewSet(pool, "usernames")
state.usernames.SelectDatabase(dbindex)
state.unconfirmed = simpleredis.NewSet(pool, "unconfirmed")
state.unconfirmed.SelectDatabase(dbindex)
state.pool = pool
state.dbindex = dbindex
// 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 = cookie.DefaultCookieTime
// 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 pool.Ping() != nil {
defer pool.Close()
log.Fatalf("Error, wrong hostname, port or password. (%s does not reply to PING)\n", redisHostPort)
}
return state
}
// Create a new *UserState that can be used for managing users.
// dbindex is the Redis database index (0 is a good default value).
// If randomseed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
// redisHostPort is host:port for the desired Redis server (can be blank for localhost)
// Also creates a new ConnectionPool.
// Returns an error if things go wrong.
func NewUserState2(dbindex int, randomseed bool, redisHostPort string) (*UserState, error) {
var pool *simpleredis.ConnectionPool
// Connect to the default redis server if redisHostPort is empty
if redisHostPort == "" {
redisHostPort = defaultRedisServer
}
// Test connection (the client can do this test before creating a new userstate)
if err := simpleredis.TestConnectionHost(redisHostPort); err != nil {
errorMessage := err.Error()
if errorMessage == "dial tcp :6379: getsockopt: connection refused" {
return nil, errors.New("Unable to connect to Redis server on port 6379.")
}
return nil, err
}
// Acquire connection pool
pool = simpleredis.NewConnectionPoolHost(redisHostPort)
state := new(UserState)
state.users = simpleredis.NewHashMap(pool, "users")
state.users.SelectDatabase(dbindex)
state.usernames = simpleredis.NewSet(pool, "usernames")
state.usernames.SelectDatabase(dbindex)
state.unconfirmed = simpleredis.NewSet(pool, "unconfirmed")
state.unconfirmed.SelectDatabase(dbindex)
state.pool = pool
state.dbindex = dbindex
// 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 = cookie.DefaultCookieTime
// 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 pool.Ping() != nil {
defer pool.Close()
return nil, fmt.Errorf("Wrong hostname, port or password. (%s does not reply to PING)\n", redisHostPort)
}
return state, nil
}
// Get the Host (for qualifying for the IUserState interface)
func (state *UserState) Host() pinterface.IHost {
return state.pool
}
// Get the Redis database index.
func (state *UserState) DatabaseIndex() int {
return state.dbindex
}
// Get the Redis connection pool.
func (state *UserState) Pool() *simpleredis.ConnectionPool {
return state.pool
}
// Close the Redis connection pool.
func (state *UserState) Close() {
state.pool.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 Redis?")
}
return val
}
// Check if the given username exists.
func (state *UserState) HasUser2(username string) (bool, error) {
val, err := state.usernames.Has(username)
if err != nil {
// This happened at concurrent connections before introducing the connection pool
return false, errors.New("Lost connection to Redis?")
}
return val, nil
}
// Return the boolean value for a given username and field name.
// 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 "", errors.New("Could not retrieve the username from browser cookie")
}
// Store the given username in a cookie in the browser, if possible.
// The user must exist.
// There are two cookie flags (ref RFC6265: https://tools.ietf.org/html/rfc6265#section-5.2.5):
// - secure is for only allowing cookies to be set over HTTPS
// - httponly is for only allowing cookies for the same server
func (state *UserState) setUsernameCookieWithFlags(w http.ResponseWriter, username string, secure, httponly bool) error {
if username == "" {
return errors.New("Can't set cookie for empty username")
}
if !state.HasUser(username) {
return errors.New("Can't store cookie for non-existing user")
}
// Create a cookie that lasts for a while ("timeout" seconds),
// this is the equivalent of a session for a given username.
cookie.SetSecureCookiePathWithFlags(w, "user", username, state.cookieTime, "/", state.cookieSecret, false, true)
return nil
}
/*SetUsernameCookie tries to store the given username in a cookie in the browser.
*
* The user must exist. Returns an error if the username is empty or does not exist.
* Returns nil if the cookie has been attempted to be set.
* To check if the cookie has actually been set, one must try to read it.
*/
func (state *UserState) SetUsernameCookie(w http.ResponseWriter, username string) error {
// These cookie flags are set (ref RFC6265)
// "secure" is set to false (only allow cookies to be set over HTTPS)
// "httponly" is set to true (only allow cookies being set/read from the same server)
return state.setUsernameCookieWithFlags(w, username, false, true)
}
/*SetUsernameCookieOnlyHTTPS tries to store the given username in a cookie in the browser.
* This function will not set the cookie if over plain HTTP.
*
* The user must exist. Returns an error if the username is empty or does not exist.
* Returns nil if the cookie has been attempted to be set.
* To check if the cookie has actually been set, one must try to read it.
*/
func (state *UserState) SetUsernameCookieOnlyHTTPS(w http.ResponseWriter, username string) error {
// These cookie flags are set (ref RFC6265)
// "secure" is set to true (only allow cookies to be set over HTTPS)
// "httponly" is set to true (only allow cookies being set/read from the same server)
return state.setUsernameCookieWithFlags(w, username, true, true)
}
// AllUsernames retrieves a list of all usernames.
func (state *UserState) AllUsernames() ([]string, error) {
return state.usernames.GetAll()
}
// Email returns the email address for the given username.
func (state *UserState) Email(username string) (string, error) {
return state.users.Get(username, "email")
}
// PasswordHash returns the password hash for the given username.
func (state *UserState) PasswordHash(username string) (string, error) {
return state.users.Get(username, "password")
}
// AllUnconfirmedUsernames returns a list of 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
// TODO: Ideally, remove all keys belonging to the user.
state.users.DelKey(username, "loggedin")
}
// 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)
// Additional 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
}
// Get cookie secret
func (state *UserState) CookieSecret() string {
return state.cookieSecret
}
// Set cookie secret
func (state *UserState) SetCookieSecret(cookieSecret string) {
state.cookieSecret = cookieSecret
}
// Get 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(hashSha256(state.cookieSecret, username, password))
case "bcrypt", "bcrypt+":
return string(hashBcrypt(password))
}
// Only valid password algorithms should be allowed to set
return ""
}
// SetPassword sets 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) storedHash(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.storedHash(username)
if len(hash) == 0 {
return false
}
// Check the password with the right password algorithm
switch state.passwordAlgorithm {
case "sha256":
return correctSha256(hash, state.cookieSecret, username, password)
case "bcrypt":
return correctBcrypt(hash, password)
case "bcrypt+": // for backwards compatibility with sha256
if isSha256(hash) && correctSha256(hash, state.cookieSecret, username, password) {
return true
}
return correctBcrypt(hash, password)
}
return false
}
// AlreadyHasConfirmationCode runs through all confirmation codes of all unconfirmed
// users and checks if this confirmationCode is already 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 "", errors.New("All existing users are already confirmed.")
}
// 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, errors.New("The confirmation code is no longer valid.")
}
hasUser := state.HasUser(username)
if !hasUser {
return username, errors.New("The user that is to be confirmed no longer exists.")
}
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, errors.New("Too many generated confirmation codes are not unique!")
}
}
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 allAllowedLetters = "abcdefghijklmnopqrstuvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ_0123456789"
NEXT:
for _, letter := range username {
for _, allowedLetter := range allAllowedLetters {
if letter == allowedLetter {
continue NEXT // check the next letter in the username
}
}
return errors.New("Only letters, numbers and underscore are allowed in usernames.")
}
if username == password {
return errors.New("Username and password must be different, try another password.")
}
return nil
}
// Return a struct for creating data structures with
func (state *UserState) Creator() pinterface.ICreator {
return simpleredis.NewCreator(state.pool, state.dbindex)
}