diff options
author | Derek Stevens <nilix@nilfm.cc> | 2022-01-10 23:30:20 -0700 |
---|---|---|
committer | Derek Stevens <nilix@nilfm.cc> | 2022-01-10 23:30:20 -0700 |
commit | 26dcce986f2e9937d27d6a009d35e364eccf61d6 (patch) | |
tree | 4219f284b5b0c23db00731fdf6f020daebb0dd46 | |
parent | 2f6c88c7f3b1332ff754c4c32707b3209052cf53 (diff) |
auth: add SetData, GetData to UserStore interface, add Login/Logout wrappers
-rw-r--r-- | auth/auth.go | 28 | ||||
-rw-r--r-- | indentalUserDB/indentalUserDB.go | 21 |
2 files changed, 43 insertions, 6 deletions
diff --git a/auth/auth.go b/auth/auth.go index b23119c..00e8e69 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -2,6 +2,8 @@ package auth import ( "time" + "net/http" + "nilfm.cc/git/quartzgun/cookie" ) type User struct { @@ -21,12 +23,26 @@ type UserStore interface { AddUser(user string, password string) error DeleteUser(user 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) { - //ValidateUser (check user exists, hash and compare password) - //InitiateUserSession (generate token and assign it to the user) - //set username in cookie - //return token, nil - return "", nil +func Login(user string, password string, userStore UserStore, w http.ResponseWriter, t int) error { + session, loginErr := userStore.InitiateSession(user, password) + if loginErr == nil { + cookie.StoreToken("user", user, w, t) + cookie.StoreToken("session", session, w, t) + 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 } diff --git a/indentalUserDB/indentalUserDB.go b/indentalUserDB/indentalUserDB.go index a3784d0..3ab3587 100644 --- a/indentalUserDB/indentalUserDB.go +++ b/indentalUserDB/indentalUserDB.go @@ -117,6 +117,27 @@ func (self *IndentalUserDB) AddUser(user string, password string) error{ 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" func readDB(filePath string) (map[string]*auth.User, error) { |