auth: add SetData, GetData to UserStore interface, add Login/Logout wrappers

This commit is contained in:
Iris Lightshard 2022-01-10 23:30:20 -07:00
parent 2f6c88c7f3
commit 26dcce986f
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
2 changed files with 43 additions and 6 deletions

View file

@ -2,6 +2,8 @@ package auth
import ( import (
"time" "time"
"net/http"
"nilfm.cc/git/quartzgun/cookie"
) )
type User struct { type User struct {
@ -21,12 +23,26 @@ type UserStore interface {
AddUser(user string, password string) error AddUser(user string, password string) error
DeleteUser(user string) error DeleteUser(user string) error
ChangePassword(user string, oldPassword string, newPassword string) error ChangePassword(user string, oldPassword string, newPassword string) error
SetData(user string, key string, value interface{}) error
GetData(user string, key string) (interface{}, error)
} }
func Login(user string, password string, userStore UserStore) (string, error) { func Login(user string, password string, userStore UserStore, w http.ResponseWriter, t int) error {
//ValidateUser (check user exists, hash and compare password) session, loginErr := userStore.InitiateSession(user, password)
//InitiateUserSession (generate token and assign it to the user) if loginErr == nil {
//set username in cookie cookie.StoreToken("user", user, w, t)
//return token, nil cookie.StoreToken("session", session, w, t)
return "", nil return nil
}
return loginErr
}
func Logout(user string, userStore UserStore, w http.ResponseWriter) error {
logoutErr := userStore.EndSession(user)
if logoutErr == nil {
cookie.StoreToken("user", "", w, 0)
cookie.StoreToken("session", "", w, 0)
return nil
}
return logoutErr
} }

View file

@ -117,6 +117,27 @@ func (self *IndentalUserDB) AddUser(user string, password string) error{
return nil; return nil;
} }
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.Users[user].Data[key] = value;
return nil;
}
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("No data key for user")
}
return data, nil
}
const timeFmt = "2006-01-02T15:04Z" const timeFmt = "2006-01-02T15:04Z"
func readDB(filePath string) (map[string]*auth.User, error) { func readDB(filePath string) (map[string]*auth.User, error) {