Refactor session code
This commit is contained in:
@ -167,7 +167,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
storeSession := session.GlobalSessionStore.Create()
|
||||
storeSession := session.Create()
|
||||
storeSession.Values["user"] = types.User{
|
||||
UserID: user.UserID,
|
||||
Email: oauthUser.Email,
|
||||
|
@ -130,7 +130,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
delete(SetupUser, code)
|
||||
|
||||
storeSession := session.GlobalSessionStore.Create()
|
||||
storeSession := session.Create()
|
||||
storeSession.Values["user"] = types.User{
|
||||
UserID: userID,
|
||||
Email: unregisteredUser.Email,
|
||||
|
@ -4,25 +4,18 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/fossyy/filekeeper/logger"
|
||||
"github.com/fossyy/filekeeper/session"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
)
|
||||
|
||||
var log *logger.AggregatedLogger
|
||||
|
||||
func init() {
|
||||
log = logger.Logger()
|
||||
}
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("Session")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
|
||||
storeSession, err := session.Get(cookie.Value)
|
||||
if err != nil {
|
||||
if errors.Is(err, &session.SessionNotFoundError{}) {
|
||||
storeSession.Destroy(w)
|
||||
@ -31,7 +24,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
session.GlobalSessionStore.Delete(cookie.Value)
|
||||
storeSession.Delete()
|
||||
session.RemoveSessionInfo(storeSession.Values["user"].(types.User).Email, cookie.Value)
|
||||
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
|
@ -58,7 +58,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if email == userData.Email && utils.CheckPasswordHash(password, userData.Password) {
|
||||
storeSession := session.GlobalSessionStore.Create()
|
||||
storeSession := session.Create()
|
||||
storeSession.Values["user"] = types.User{
|
||||
UserID: userData.UserID,
|
||||
Email: email,
|
||||
|
@ -1,7 +1,6 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -14,10 +13,6 @@ import (
|
||||
type Session struct {
|
||||
ID string
|
||||
Values map[string]interface{}
|
||||
}
|
||||
|
||||
type SessionStore struct {
|
||||
Sessions map[string]*Session
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
@ -40,7 +35,7 @@ const (
|
||||
InvalidSession UserStatus = "invalid_session"
|
||||
)
|
||||
|
||||
var GlobalSessionStore = SessionStore{Sessions: make(map[string]*Session)}
|
||||
var GlobalSessionStore = make(map[string]*Session)
|
||||
var UserSessionInfoList = make(map[string]map[string]*SessionInfo)
|
||||
|
||||
type SessionNotFoundError struct{}
|
||||
@ -49,29 +44,27 @@ func (e *SessionNotFoundError) Error() string {
|
||||
return "session not found"
|
||||
}
|
||||
|
||||
func (s *SessionStore) Get(id string) (*Session, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if session, ok := s.Sessions[id]; ok {
|
||||
func Get(id string) (*Session, error) {
|
||||
if session, ok := GlobalSessionStore[id]; ok {
|
||||
return session, nil
|
||||
}
|
||||
return nil, &SessionNotFoundError{}
|
||||
}
|
||||
|
||||
func (s *SessionStore) Create() *Session {
|
||||
func Create() *Session {
|
||||
id := utils.GenerateRandomString(128)
|
||||
session := &Session{
|
||||
ID: id,
|
||||
Values: make(map[string]interface{}),
|
||||
}
|
||||
s.Sessions[id] = session
|
||||
GlobalSessionStore[id] = session
|
||||
return session
|
||||
}
|
||||
|
||||
func (s *SessionStore) Delete(id string) {
|
||||
func (s *Session) Delete() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.Sessions, id)
|
||||
delete(GlobalSessionStore, s.ID)
|
||||
}
|
||||
|
||||
func (s *Session) Save(w http.ResponseWriter) {
|
||||
@ -114,7 +107,7 @@ func RemoveSessionInfo(email string, id string) {
|
||||
func RemoveAllSessions(email string) {
|
||||
sessionInfos := UserSessionInfoList[email]
|
||||
for _, sessionInfo := range sessionInfos {
|
||||
delete(GlobalSessionStore.Sessions, sessionInfo.SessionID)
|
||||
delete(GlobalSessionStore, sessionInfo.SessionID)
|
||||
}
|
||||
delete(UserSessionInfoList, email)
|
||||
}
|
||||
@ -140,17 +133,14 @@ func GetSession(r *http.Request) (UserStatus, types.User, string) {
|
||||
return Unauthorized, types.User{}, ""
|
||||
}
|
||||
|
||||
storeSession, err := GlobalSessionStore.Get(cookie.Value)
|
||||
if err != nil {
|
||||
if errors.Is(err, &SessionNotFoundError{}) {
|
||||
storeSession, ok := GlobalSessionStore[cookie.Value]
|
||||
if !ok {
|
||||
return InvalidSession, types.User{}, ""
|
||||
}
|
||||
return Unauthorized, types.User{}, ""
|
||||
}
|
||||
|
||||
val := storeSession.Values["user"]
|
||||
var userSession = types.User{}
|
||||
userSession, ok := val.(types.User)
|
||||
userSession, ok = val.(types.User)
|
||||
if !ok {
|
||||
return Unauthorized, types.User{}, ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user