indentalUserDB: add mutex for writes and initialize map on UserAdd
PGP Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmSvgfsACgkQO3+8IhRO Y5jBaxAAiyMcuYdqibc6Gxo3IYdadSuqBJSqSZkwtJ0oji2grfHcU1yDMs8voJv7 Vq19id35va1wqQmMrm/B3UIrNp8EPX2FcPV6FULQ1rJ8pykAdrX7PKTyFP1cpZlN laqgoTWmKWWnr4miyerKudwZKxffMPSfMrf4wVLAgiuxxASyB2vj8TkNzfyPlldg mUb9ZGNMYWc6ttrA4F10/Fob/Qzj7p0wyyvv3TgrtN3nFFQq3nEBtf4LedUiD42c qVNg+1s2aYS8/CQyrP3cq1xImKBtXlNJ4WgnXUpW3hcNF3uL/3yslfwVc2fnllzV UjhkbwJXLFIlvydr5ec5FP5XqhJ4gJyx5IxcKjpF+0UHZr/1pC2ZQs6rGa/5x76X QPg8Ul0FbcG9iiqEsz5I7JyRWVlTLKXwJV3o4wjtmwn+r5riYteErrOJDxRpLVj9 r/QWnXg33ZnqZSRiptHZiNzonKEgRfaRO2niBdBIUhQLNr12vFnuxvy9NDF2BHof HXfrbCrE3yx3CjaDzu3g+mT4tVI+1fhQDbHjqXxHtppOKIHKZTqTLwZOOzj88FbW MtnP3RUZ+eue2eM1Q7EdrCp4UBX5r+mRpfqNrOvH3IS7pxMCrV6lRmC3HAgZbM87 1CDkR8bO/jkkY1tODS+NFPJtyhdQ6UlnCFE8ufolp7wNs2xEFdA= =ac33 -----END PGP SIGNATURE-----
jump to
@@ -11,22 +11,20 @@ "os"
"strings" "strconv" "time" + "sync" ) type IndentalUserDB struct { Users map[string]*auth.User Basis string + mtx sync.Mutex } func CreateIndentalUserDB(filePath string) *IndentalUserDB { u, err := readDB(filePath) if err == nil { - uMap := map[string]*auth.User{} - for _, usr := range u { - uMap[usr.Name] = usr - } return &IndentalUserDB{ - Users: uMap, + Users: u, Basis: filePath, } } else {@@ -45,11 +43,13 @@ if bcrypt.CompareHashAndPassword([]byte(self.Users[user].Pass), []byte(password)) != nil {
return "", errors.New("Incorrect password") } sessionId := cookie.GenToken(64) + self.mtx.Lock() self.Users[user].Session = sessionId self.Users[user].LoginTime = time.Now() self.Users[user].LastSeen = time.Now() + self.mtx.Unlock() + // SetData calls Lock & Unlock and writes DB to file self.SetData(user, "token_expiry", strconv.Itoa(ttl)) - writeDB(self.Basis, self.Users) return sessionId, nil }@@ -61,11 +61,13 @@ if bcrypt.CompareHashAndPassword([]byte(self.Users[user].Pass), []byte(password)) != nil {
return "", errors.New("Incorrect password") } sessionId := cookie.GenToken(64) + self.mtx.Lock() self.Users[user].Session = sessionId self.Users[user].LoginTime = time.Now() self.Users[user].LastSeen = time.Now() + self.mtx.Unlock() + // SetData calls Lock & Unlock and writes DB to file self.SetData(user, "token_expiry", strconv.Itoa(ttl)) - writeDB(self.Basis, self.Users) return base64.StdEncoding.EncodeToString([]byte(user + "\n" + sessionId)), nil }@@ -83,6 +85,8 @@ if err3 == nil && err4 == nil && time.Now().After(expiryTime) {
self.EndSession(user) return true, errors.New("Cookie or token expired") } else { + self.mtx.Lock() + defer self.mtx.Unlock() self.Users[user].LastSeen = time.Now() writeDB(self.Basis, self.Users) }@@ -132,6 +136,8 @@ if _, exists := self.Users[user]; !exists {
return errors.New("User not in DB") } + self.mtx.Lock() + defer self.mtx.Unlock() self.Users[user].Session = "" self.Users[user].LastSeen = time.Now() writeDB(self.Basis, self.Users)@@ -143,6 +149,8 @@ if _, exists := self.Users[user]; !exists {
return errors.New("User not in DB") } + self.mtx.Lock() + defer self.mtx.Unlock() delete(self.Users, user) writeDB(self.Basis, self.Users) return nil@@ -157,6 +165,8 @@ return errors.New("Incorrect password")
} hash, _ := bcrypt.GenerateFromPassword([]byte(password), 10) + self.mtx.Lock() + defer self.mtx.Unlock() self.Users[user].Pass = string(hash[:]) writeDB(self.Basis, self.Users) return nil@@ -169,12 +179,15 @@ }
hash, _ := bcrypt.GenerateFromPassword([]byte(password), 10) + self.mtx.Lock() + defer self.mtx.Unlock() self.Users[user] = &auth.User{ Name: user, Pass: string(hash[:]), LastSeen: time.UnixMicro(0), LoginTime: time.UnixMicro(0), Session: "", + Data: map[string]interface{}{}, } writeDB(self.Basis, self.Users) return nil@@ -184,6 +197,7 @@ func (self *IndentalUserDB) GetLastLoginTime(user string) (time.Time, error) {
if usr, exists := self.Users[user]; exists { return usr.LoginTime, nil } + return time.UnixMicro(0), errors.New("User not in DB") }@@ -191,15 +205,20 @@ func (self *IndentalUserDB) GetLastTimeSeen(user string) (time.Time, error) {
if usr, exists := self.Users[user]; exists { return usr.LastSeen, nil } + return time.UnixMicro(0), errors.New("User not in DB") } func (self *IndentalUserDB) SetData(user string, key string, value interface{}) error { + if _, exists := self.Users[user]; !exists { return errors.New("User not in DB") } + self.mtx.Lock() + defer self.mtx.Unlock() self.Users[user].Data[key] = value + writeDB(self.Basis, self.Users) return nil }@@ -207,6 +226,7 @@ func (self *IndentalUserDB) GetData(user string, key string) (interface{}, error) {
if _, usrExists := self.Users[user]; !usrExists { return nil, errors.New("User not in DB") } + data, exists := self.Users[user].Data[key] if !exists { return nil, errors.New("Key not found in user data")