OnHostChanged event and add tokens to session.

This commit is contained in:
Miroslav Šedivý 2021-03-13 22:17:49 +01:00
parent 453a6905f7
commit 3fcca6ab52
6 changed files with 43 additions and 12 deletions

View File

@ -68,7 +68,7 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
// TODO: Refactor. // TODO: Refactor.
// data.ID // data.ID
session, err := h.sessions.Create(*data.MemberProfile) session, _, err := h.sessions.Create(*data.MemberProfile)
if err != nil { if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return

View File

@ -31,7 +31,7 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
} }
// TODO: Proper login. // TODO: Proper login.
session, err := api.sessions.Create(types.MemberProfile{ session, token, err := api.sessions.Create(types.MemberProfile{
Name: data.ID, Name: data.ID,
IsAdmin: true, IsAdmin: true,
CanLogin: true, CanLogin: true,
@ -53,7 +53,7 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: "NEKO_SESSION", Name: "NEKO_SESSION",
Value: session.ID(), Value: token,
Expires: CookieExpirationDate, Expires: CookieExpirationDate,
Secure: !UnsecureCookies, Secure: !UnsecureCookies,
SameSite: sameSite, SameSite: sameSite,

View File

@ -14,7 +14,7 @@ func (manager *SessionManagerCtx) Authenticate(r *http.Request) (types.Session,
return nil, fmt.Errorf("no authentication provided") return nil, fmt.Errorf("no authentication provided")
} }
session, ok := manager.Get(token) session, ok := manager.GetByToken(token)
if !ok { if !ok {
return nil, fmt.Errorf("session not found") return nil, fmt.Errorf("session not found")
} }

View File

@ -19,6 +19,7 @@ func New(config *config.Session) *SessionManagerCtx {
config: config, config: config,
host: nil, host: nil,
hostMu: sync.Mutex{}, hostMu: sync.Mutex{},
tokens: make(map[string]string),
sessions: make(map[string]*SessionCtx), sessions: make(map[string]*SessionCtx),
sessionsMu: sync.Mutex{}, sessionsMu: sync.Mutex{},
emmiter: events.New(), emmiter: events.New(),
@ -30,36 +31,48 @@ type SessionManagerCtx struct {
config *config.Session config *config.Session
host types.Session host types.Session
hostMu sync.Mutex hostMu sync.Mutex
tokens map[string]string
sessions map[string]*SessionCtx sessions map[string]*SessionCtx
sessionsMu sync.Mutex sessionsMu sync.Mutex
emmiter events.EventEmmiter emmiter events.EventEmmiter
} }
func (manager *SessionManagerCtx) Create(profile types.MemberProfile) (types.Session, error) { func (manager *SessionManagerCtx) Create(profile types.MemberProfile) (types.Session, string, error) {
id, err := utils.NewUID(32) id, err := utils.NewUID(32)
if err != nil { if err != nil {
return nil, err return nil, "", err
}
token, err := utils.NewUID(64)
if err != nil {
return nil, "", err
} }
manager.sessionsMu.Lock() manager.sessionsMu.Lock()
_, ok := manager.sessions[id] if _, ok := manager.sessions[id]; ok {
if ok {
manager.sessionsMu.Unlock() manager.sessionsMu.Unlock()
return nil, fmt.Errorf("Session id already exists.") return nil, "", fmt.Errorf("Session id already exists.")
}
if _, ok := manager.tokens[token]; ok {
manager.sessionsMu.Unlock()
return nil, "", fmt.Errorf("Session token already exists.")
} }
session := &SessionCtx{ session := &SessionCtx{
id: id, id: id,
token: token,
manager: manager, manager: manager,
logger: manager.logger.With().Str("id", id).Logger(), logger: manager.logger.With().Str("id", id).Logger(),
profile: profile, profile: profile,
} }
manager.tokens[token] = id
manager.sessions[id] = session manager.sessions[id] = session
manager.sessionsMu.Unlock() manager.sessionsMu.Unlock()
manager.emmiter.Emit("created", session) manager.emmiter.Emit("created", session)
return session, nil return session, token, nil
} }
func (manager *SessionManagerCtx) Update(id string, profile types.MemberProfile) error { func (manager *SessionManagerCtx) Update(id string, profile types.MemberProfile) error {
@ -87,6 +100,10 @@ func (manager *SessionManagerCtx) Delete(id string) error {
return fmt.Errorf("Session id not found.") return fmt.Errorf("Session id not found.")
} }
if _, ok := manager.tokens[session.token]; ok {
delete(manager.tokens, session.token)
}
delete(manager.sessions, id) delete(manager.sessions, id)
manager.sessionsMu.Unlock() manager.sessionsMu.Unlock()
@ -107,6 +124,18 @@ func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
return session, ok return session, ok
} }
func (manager *SessionManagerCtx) GetByToken(token string) (types.Session, bool) {
manager.sessionsMu.Lock()
id, ok := manager.tokens[token]
manager.sessionsMu.Unlock()
if !ok {
return nil, false
}
return manager.Get(id)
}
func (manager *SessionManagerCtx) List() []types.Session { func (manager *SessionManagerCtx) List() []types.Session {
manager.sessionsMu.Lock() manager.sessionsMu.Lock()
defer manager.sessionsMu.Unlock() defer manager.sessionsMu.Unlock()

View File

@ -10,6 +10,7 @@ import (
type SessionCtx struct { type SessionCtx struct {
id string id string
token string
logger zerolog.Logger logger zerolog.Logger
manager *SessionManagerCtx manager *SessionManagerCtx
profile types.MemberProfile profile types.MemberProfile

View File

@ -59,10 +59,11 @@ type Session interface {
} }
type SessionManager interface { type SessionManager interface {
Create(profile MemberProfile) (Session, error) Create(profile MemberProfile) (Session, string, error)
Update(id string, profile MemberProfile) error Update(id string, profile MemberProfile) error
Get(id string) (Session, bool)
Delete(id string) error Delete(id string) error
Get(id string) (Session, bool)
GetByToken(token string) (Session, bool)
List() []Session List() []Session
SetHost(host Session) SetHost(host Session)