70 lines
No EOL
1.5 KiB
Go
70 lines
No EOL
1.5 KiB
Go
package user
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
)
|
|
|
|
type UserClient interface {
|
|
register(username, password, email string) (bool, error)
|
|
login(username, password string, requiredMailValidation bool) (bool, error)
|
|
getMailValidationToken(forceRecreate bool) (string, error)
|
|
}
|
|
|
|
type UserClientMemory struct {
|
|
users map[string]string
|
|
}
|
|
|
|
var u *UserClientMemory
|
|
|
|
func GetUserClient() *UserClientMemory {
|
|
if u == nil {
|
|
uc := UserClientMemory{}
|
|
uc.users = make(map[string]string)
|
|
uc.users["admin"]="password"
|
|
u = &uc
|
|
}
|
|
|
|
return u
|
|
}
|
|
|
|
func (uc *UserClientMemory) register(username, password, email string) (bool, error) {
|
|
if _, ok := uc.users[username]; ok {
|
|
return false, errors.New("Username already used")
|
|
}
|
|
|
|
uc.users[username] = password
|
|
return true, nil
|
|
}
|
|
|
|
func (uc UserClientMemory) login(username, password string, requiredMailValidation bool) (bool, error) {
|
|
if requiredMailValidation == true {
|
|
return false, errors.New("In memmory User Client cant validate email addresses")
|
|
}
|
|
if val, ok := uc.users[username]; ok {
|
|
fmt.Println("Login for valide user")
|
|
if val == password {
|
|
return true, nil
|
|
}
|
|
} else {
|
|
fmt.Printf("User %v not found", username)
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func (uc UserClientMemory) getMailValidationToken(forceRecreate bool) (string, error) {
|
|
token := randomString(5)
|
|
return token, nil
|
|
}
|
|
|
|
|
|
func randomString(n int) string {
|
|
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
}
|
|
return string(b)
|
|
} |