|
|
@@ -2,7 +2,6 @@ package utils
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
- "log"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
@@ -45,7 +44,6 @@ func (t *MemoryStore) Get(key string) *entitys.Token {
|
|
|
t.lock.RLock()
|
|
|
defer t.lock.RUnlock()
|
|
|
if val, ok := t.tokens[key]; ok {
|
|
|
- //log.Println(key, "获取Token:", val.AccessToken, val.RefreshToken, val.LoginID)
|
|
|
return val
|
|
|
}
|
|
|
return nil
|
|
|
@@ -59,14 +57,12 @@ func (t *MemoryStore) Set(key string, v *entitys.Token) {
|
|
|
} else if val != v {
|
|
|
t.tokens[key] = v
|
|
|
}
|
|
|
- log.Println(key, "添加Token:", v.AccessToken, v.RefreshToken, v.LoginID)
|
|
|
}
|
|
|
|
|
|
func (t *MemoryStore) Remove(key string) {
|
|
|
t.lock.Lock()
|
|
|
defer t.lock.Unlock()
|
|
|
delete(t.tokens, key)
|
|
|
- log.Println(key, "删除Key")
|
|
|
}
|
|
|
|
|
|
func (t *MemoryStore) Refresh(key string) {
|
|
|
@@ -90,12 +86,10 @@ func (t *MemoryStore) startTokenCheckProcess() {
|
|
|
select {
|
|
|
case <-t1.C:
|
|
|
t.lock.Lock()
|
|
|
-
|
|
|
keys := []string{}
|
|
|
for k, v := range t.tokens {
|
|
|
timestampt := time.Unix(int64(v.TimeStamp), 0)
|
|
|
subval := time.Now().Sub(timestampt)
|
|
|
-
|
|
|
if subval.Seconds() < 0 || subval.Seconds() > 3600 {
|
|
|
keys = append(keys, k)
|
|
|
}
|
|
|
@@ -103,7 +97,6 @@ func (t *MemoryStore) startTokenCheckProcess() {
|
|
|
for _, k := range keys {
|
|
|
delete(t.tokens, k)
|
|
|
}
|
|
|
-
|
|
|
t.lock.Unlock()
|
|
|
}
|
|
|
}
|
|
|
@@ -113,7 +106,6 @@ func Validate(accessToken, loginId string, domain string) (*entitys.Token, error
|
|
|
token := globalTokenStore.Get(loginId + domain)
|
|
|
if token != nil {
|
|
|
if strings.EqualFold(token.AccessToken, accessToken) {
|
|
|
- logs.Info("get the token ", accessToken, " of id ", loginId+domain)
|
|
|
globalTokenStore.Refresh(loginId + domain)
|
|
|
return token, nil
|
|
|
} else {
|
|
|
@@ -124,19 +116,15 @@ func Validate(accessToken, loginId string, domain string) (*entitys.Token, error
|
|
|
logs.Error("can not get the token of", loginId+domain)
|
|
|
return token, errors.New("can not get the token of " + loginId + domain)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
func TokenValidate(token string) (*entitys.Token, error) {
|
|
|
- user := globalTokenStore.Get(token)
|
|
|
-
|
|
|
- if strings.EqualFold(user.AccessToken, token) {
|
|
|
- logs.Info("get the token ", token, " of id ")
|
|
|
+ tokenRaw := globalTokenStore.Get(token)
|
|
|
+ if strings.EqualFold(tokenRaw.AccessToken, token) {
|
|
|
globalTokenStore.Refresh(token)
|
|
|
- return user, nil
|
|
|
+ return tokenRaw, nil
|
|
|
} else {
|
|
|
- logs.Error(user.AccessToken, "is not equal to", token)
|
|
|
- return user, errors.New(user.AccessToken + " is not equal to " + token)
|
|
|
+ logs.Error(tokenRaw.AccessToken, "is not equal to", token)
|
|
|
+ return tokenRaw, errors.New(tokenRaw.AccessToken + " is not equal to " + token)
|
|
|
}
|
|
|
-
|
|
|
}
|