94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package session
|
|
|
|
import (
|
|
"q5"
|
|
"main/common"
|
|
"github.com/google/uuid"
|
|
"sync"
|
|
)
|
|
|
|
type sessionMgr struct {
|
|
lock sync.Mutex
|
|
addressHash *q5.ConcurrentMap[string, *session]
|
|
sessionHash *q5.ConcurrentMap[string, *session]
|
|
}
|
|
|
|
func (this *sessionMgr) Init() {
|
|
this.addressHash = new(q5.ConcurrentMap[string, *session])
|
|
this.sessionHash = new(q5.ConcurrentMap[string, *session])
|
|
}
|
|
|
|
func (this *sessionMgr) UnInit() {
|
|
|
|
}
|
|
|
|
func (this *sessionMgr) GenNonce() string {
|
|
uuidV4 := uuid.New()
|
|
return uuidV4.String()
|
|
}
|
|
|
|
func (this *sessionMgr) IsValidNonce(string) bool {
|
|
return true
|
|
}
|
|
|
|
func (this *sessionMgr) AddSession(accountAddress string, token string) common.Session {
|
|
this.lock.Lock()
|
|
defer this.lock.Unlock()
|
|
|
|
{
|
|
s := this.GetSessionByToken(token)
|
|
if s != nil {
|
|
this.internalRemoveSession(s)
|
|
}
|
|
}
|
|
{
|
|
s := this.GetSessionByAddress(accountAddress)
|
|
if s != nil {
|
|
this.internalRemoveSession(s)
|
|
}
|
|
}
|
|
s := newSession(accountAddress, token)
|
|
this.addressHash.Store(accountAddress, s)
|
|
this.sessionHash.Store(token, s)
|
|
return s
|
|
}
|
|
|
|
func (this *sessionMgr) RemoveSssionWithToken(token string) {
|
|
this.lock.Lock()
|
|
defer this.lock.Unlock()
|
|
|
|
s := this.GetSessionByToken(token)
|
|
if s != nil {
|
|
this.internalRemoveSession(s)
|
|
}
|
|
}
|
|
|
|
func (this *sessionMgr) RemoveSssionWithAddress(accountAddress string) {
|
|
this.lock.Lock()
|
|
defer this.lock.Unlock()
|
|
|
|
s := this.GetSessionByAddress(accountAddress)
|
|
if s != nil {
|
|
this.internalRemoveSession(s)
|
|
}
|
|
}
|
|
|
|
func (this *sessionMgr) GetSessionByToken(token string) common.Session {
|
|
if s, ok := this.sessionHash.Load(token); ok {
|
|
return *s
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *sessionMgr) GetSessionByAddress(accountAddress string) common.Session {
|
|
if s, ok := this.addressHash.Load(accountAddress); ok {
|
|
return *s
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *sessionMgr) internalRemoveSession(s common.Session) {
|
|
this.sessionHash.Delete(s.GetToken())
|
|
this.addressHash.Delete(s.GetAccountAddress())
|
|
}
|