forked from RobinUS2/indispenso
/
users.go
180 lines (157 loc) · 4.29 KB
/
users.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
// @author Robin Verlangen
// User handler
package main
// Imports
import (
"crypto/sha512"
"encoding/json"
"fmt"
"github.com/dgryski/dgoogauth"
"io"
"log"
"math/rand"
"strings"
)
// Defaults
const DEFAULT_ADMIN_USR = "admin"
const DEFUALT_ADMIN_PWD = "indispenso"
const MIN_USR_LEN int = 3
const MIN_PWD_LEN int = 8
// User handler
type UserHandler struct {
}
// User
type User struct {
Id string // Uuid string
Username string
PasswordHash string // Sha 512 hash concat(pwd, salt)
PasswordSalt string
IsAdmin bool // Permission: system management
IsRequester bool // Permission: request task execution
IsApprover bool // Permission: approve task execution
TwoFactorSeed string
}
// Display name
func (u *User) DisplayName() string {
return u.Username
}
// Check two-factor
func (u *User) IsValidTwoFactor(token string) bool {
// Do we have two factor enabled?
if len(u.TwoFactorSeed) == 0 {
// No, OK pass
log.Println(fmt.Sprintf("WARN: User %s login without two-factor enabled", u.DisplayName()))
return true
}
// Configure token
var cotp *dgoogauth.OTPConfig = &dgoogauth.OTPConfig{
Secret: u.TwoFactorSeed,
WindowSize: 3,
}
// Validate token
authRes, authErr := cotp.Authenticate(token)
if authErr != nil || authRes == false {
log.Println(fmt.Sprintf("WARN: User %s failed two-factor challenge, invalid token", u.DisplayName()))
return false
} else {
// OK
return true
}
// By default error
return false
}
// New user
func NewUser(username string, password string, isAdmin bool, isRequester bool, isApprover bool) *User {
// Hash password with random salt
var salt string = HashPassword(fmt.Sprintf("%d", rand.Int63()), "")
hash := HashPassword(password, salt)
// Struct
return &User{
Id: getUuid(),
Username: username,
PasswordHash: hash,
PasswordSalt: salt,
IsAdmin: isAdmin,
IsRequester: isRequester,
IsApprover: isApprover,
}
}
// Hash password
func HashPassword(pwd string, salt string) string {
h := sha512.New()
io.WriteString(h, pwd)
io.WriteString(h, salt)
hash := fmt.Sprintf("%x", h.Sum(nil))
return hash
}
// New user handler
func NewUserHandler() *UserHandler {
return &UserHandler{}
}
// Create user
func (u *UserHandler) CreateUser(username string, password string, isAdmin bool, isRequester bool, isApprover bool) (*User, error) {
// Basic validation
username = strings.TrimSpace(username)
if len(username) < MIN_USR_LEN {
return nil, newErr(fmt.Sprintf("Please provide a username of at least %d characters", MIN_USR_LEN))
}
password = strings.TrimSpace(password)
if len(password) < MIN_PWD_LEN {
return nil, newErr(fmt.Sprintf("Please provide a password of at least %d characters", MIN_PWD_LEN))
}
// Existing user, do NOT use GetUser, this causes recursive StackOverflow
existing := u.GetUserData(username)
if existing != nil {
return nil, newErr(fmt.Sprintf("Username '%s' already taken", username))
}
// Create struct
user := NewUser(username, password, isAdmin, isRequester, isApprover)
// Save in cluster (not async)
k := fmt.Sprintf("user~%s", user.Username)
b, err := json.Marshal(user)
if err != nil {
return nil, newErr(fmt.Sprintf("ERR: Failed to convert user struct to json %s", err))
}
if datastore == nil {
return nil, newErr(fmt.Sprintf("ERR: Datastore not available"))
}
if datastore.PutEntry(k, string(b)) {
log.Println(fmt.Sprintf("INFO: Created user %s", user.Username))
}
// Done
return user, nil
}
// Get user
func (u *UserHandler) GetUser(username string) *User {
// Fetch
entry := u.GetUserData(username)
if entry == nil {
if username == DEFAULT_ADMIN_USR {
log.Println("INFO: Creating default admin user")
newAdmin, newAdminErr := u.CreateUser(DEFAULT_ADMIN_USR, DEFUALT_ADMIN_PWD, true, true, true)
if newAdminErr == nil {
return newAdmin
}
}
return nil
}
// Convert to struct
var user *User
err := json.Unmarshal([]byte(entry.Value), &user)
if err != nil {
log.Println(fmt.Sprintf("ERR: Failed to unmarshal user %s", err))
return nil
}
// Return
return user
}
// Get user data
func (u *UserHandler) GetUserData(username string) *MemEntry {
if datastore == nil {
log.Println(fmt.Sprintf("ERR: Datastore not available"))
return nil
}
k := fmt.Sprintf("user~%s", username)
e, _ := datastore.GetEntry(k)
return e
}