quartzgun/auth/auth.go

49 lines
1.2 KiB
Go
Raw Normal View History

package auth
import (
"net/http"
"nilfm.cc/git/quartzgun/cookie"
"time"
)
type User struct {
Name string
Pass string
Session string
LoginTime time.Time
LastSeen time.Time
Data map[string]interface{}
}
type UserStore interface {
InitiateSession(user string, password string) (string, error)
ValidateUser(user string, sessionId string) (bool, error)
EndSession(user string) error
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, 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
}