-
Notifications
You must be signed in to change notification settings - Fork 2
/
dummyUserStore.go
266 lines (235 loc) · 8.2 KB
/
dummyUserStore.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
package authaus
import (
"sync"
"time"
"github.com/google/uuid"
)
// Authenticator/Userstore that simply stores identity/passwords in memory
type dummyUserStore struct {
users map[UserId]*dummyUser
usersLock sync.RWMutex
passswordExpiry time.Duration
}
type dummyUser struct {
userId UserId
email string
username string
firstname string
lastname string
mobilenumber string
telephonenumber string
remarks string
created time.Time
createdby UserId
modified time.Time
modifiedby UserId
password string
passwordResetToken string
archived bool
authUserType AuthUserType
passwordModifiedDate time.Time
accountLocked bool
internalUUID string
}
func newDummyLdap() *dummyLdap {
d := &dummyLdap{}
d.ldapUsers = make([]*dummyLdapUser, 0)
return d
}
func newDummyUserStore() *dummyUserStore {
d := &dummyUserStore{}
d.users = make(map[UserId]*dummyUser)
return d
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (x *dummyUserStore) Authenticate(identity, password string, authTypeCheck AuthCheck) error {
x.usersLock.RLock()
defer x.usersLock.RUnlock()
user := x.getDummyUser(identity)
if user == nil {
return ErrIdentityAuthNotFound
}
if user.accountLocked {
return ErrAccountLocked
}
if user.password != password {
return ErrInvalidPassword
}
return nil
}
func (x *dummyUserStore) Close() {
//Set incrementing user id to 0, for unit test prediction
nextUserId = 0
}
func (x *dummyUserStore) SetConfig(passwordExpiry time.Duration, oldPasswordHistorySize int) error {
x.passswordExpiry = passwordExpiry
return nil
}
func (x *dummyUserStore) SetPassword(userId UserId, password string, enforceTypeCheck PasswordEnforcement) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if user, exists := x.users[userId]; exists && user.authUserType.CanSetPassword() {
if enforceTypeCheck&PasswordEnforcementReuse != 0 && user.password == password {
return ErrInvalidPastPassword
}
user.password = password
} else {
return ErrIdentityAuthNotFound
}
return nil
}
func (x *dummyUserStore) ResetPasswordStart(userId UserId, expires time.Time) (string, error) {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if user, exists := x.users[userId]; exists && !user.archived && user.authUserType.CanSetPassword() {
user.passwordResetToken = generatePasswordResetToken(expires)
return user.passwordResetToken, nil
} else {
return "", ErrIdentityAuthNotFound
}
}
func (x *dummyUserStore) ResetPasswordFinish(userId UserId, token string, password string, enforceTypeCheck PasswordEnforcement) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if user, exists := x.users[userId]; exists && !user.archived && user.authUserType.CanSetPassword() {
if err := verifyPasswordResetToken(token, user.passwordResetToken); err != nil {
return err
}
if enforceTypeCheck&PasswordEnforcementReuse != 0 && user.password == password {
return ErrInvalidPastPassword
}
user.password = password
user.passwordResetToken = ""
return nil
}
return ErrIdentityAuthNotFound
}
func (x *dummyUserStore) CreateIdentity(user *AuthUser, password string) (UserId, error) {
x.usersLock.Lock()
defer x.usersLock.Unlock()
var userD *dummyUser
userD = x.getDummyUser(user.Email)
if userD == nil {
if user.InternalUUID == "" {
uuid, _ := uuid.NewRandom()
user.InternalUUID = uuid.String()
}
userId := x.generateUserId()
x.users[userId] = &dummyUser{userId, user.Email, user.Username, user.Firstname, user.Lastname, user.Mobilenumber, user.Telephonenumber, user.Remarks, user.Created, user.CreatedBy,
user.Modified, user.ModifiedBy, password, "", false, user.Type, user.PasswordModifiedDate, user.AccountLocked, user.InternalUUID}
return userId, nil
} else {
return NullUserId, ErrIdentityExists
}
}
func (x *dummyUserStore) UpdateIdentity(user *AuthUser) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if userD, exists := x.users[user.UserId]; exists && !userD.archived {
userD.email = user.Email
userD.username = user.Username
userD.firstname = user.Firstname
userD.lastname = user.Lastname
userD.mobilenumber = user.Mobilenumber
userD.telephonenumber = user.Telephonenumber
userD.remarks = user.Remarks
userD.modified = user.Modified
userD.modifiedby = user.ModifiedBy
userD.authUserType = user.Type
} else {
return ErrIdentityAuthNotFound
}
return nil
}
func (x *dummyUserStore) ArchiveIdentity(userId UserId) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if user, exists := x.users[userId]; exists {
user.archived = true
} else {
return ErrIdentityAuthNotFound
}
return nil
}
func (x *dummyUserStore) RenameIdentity(oldEmail, newEmail string) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
newKey := CanonicalizeIdentity(newEmail)
oldEmail = CanonicalizeIdentity(oldEmail)
newUser := x.getDummyUser(newKey)
if newUser == nil {
oldUser := x.getDummyUser(oldEmail)
if oldUser != nil && !oldUser.archived && oldUser.authUserType == UserTypeDefault {
x.users[oldUser.userId].email = newEmail
return nil
} else {
return ErrIdentityAuthNotFound
}
} else {
return ErrIdentityExists
}
}
func (x *dummyUserStore) GetIdentities(getIdentitiesFlag GetIdentitiesFlag) ([]AuthUser, error) {
x.usersLock.RLock()
defer x.usersLock.RUnlock()
list := []AuthUser{}
for _, v := range x.users {
if (getIdentitiesFlag&GetIdentitiesFlagDeleted == 0) && v.archived {
continue
}
list = append(list, AuthUser{v.userId, v.email, v.username, v.firstname, v.lastname, v.mobilenumber, v.telephonenumber, v.remarks, v.created, v.createdby, v.modified, v.modifiedby, v.authUserType, v.archived, v.internalUUID, "", v.passwordModifiedDate, v.accountLocked})
}
return list, nil
}
func (x *dummyUserStore) LockAccount(userId UserId) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if user, exists := x.users[userId]; exists && !user.archived {
user.accountLocked = true
}
return nil
}
func (x *dummyUserStore) UnlockAccount(userId UserId) error {
x.usersLock.Lock()
defer x.usersLock.Unlock()
if user, exists := x.users[userId]; exists && !user.archived {
user.accountLocked = false
}
return nil
}
func (x *dummyUserStore) GetUserFromIdentity(identity string) (*AuthUser, error) {
x.usersLock.RLock()
defer x.usersLock.RUnlock()
for _, v := range x.users {
if CanonicalizeIdentity(v.email) == CanonicalizeIdentity(identity) && v.archived == false {
return &AuthUser{UserId: v.userId, Email: v.email, Username: v.username, Firstname: v.firstname, Lastname: v.lastname, Mobilenumber: v.mobilenumber, Type: v.authUserType, PasswordModifiedDate: v.passwordModifiedDate, AccountLocked: v.accountLocked, InternalUUID: v.internalUUID}, nil
} else if CanonicalizeIdentity(v.username) == CanonicalizeIdentity(identity) && v.archived == false {
return &AuthUser{UserId: v.userId, Email: v.email, Username: v.username, Firstname: v.firstname, Lastname: v.lastname, Mobilenumber: v.mobilenumber, Type: v.authUserType, PasswordModifiedDate: v.passwordModifiedDate, AccountLocked: v.accountLocked, InternalUUID: v.internalUUID}, nil
}
}
return nil, ErrIdentityAuthNotFound
}
func (x *dummyUserStore) GetUserFromUserId(userId UserId) (*AuthUser, error) {
x.usersLock.RLock()
defer x.usersLock.RUnlock()
for _, v := range x.users {
if v.userId == userId && v.archived == false {
return &AuthUser{UserId: v.userId, Email: v.email, Username: v.username, Firstname: v.firstname, Lastname: v.lastname, Mobilenumber: v.mobilenumber, Type: v.authUserType, PasswordModifiedDate: v.passwordModifiedDate, InternalUUID: v.internalUUID}, nil
}
}
return nil, ErrIdentityAuthNotFound
}
func (x *dummyUserStore) getDummyUser(identity string) *dummyUser {
for _, v := range x.users {
if CanonicalizeIdentity(v.email) == CanonicalizeIdentity(identity) && v.archived == false {
return v
} else if CanonicalizeIdentity(v.username) == CanonicalizeIdentity(identity) && v.archived == false {
return v
}
}
return nil
}
func (x *dummyUserStore) generateUserId() UserId {
nextUserId = nextUserId + 1
return nextUserId
}