|
|
@@ -2,33 +2,46 @@ package utils
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
|
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
"log"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
+
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
)
|
|
|
|
|
|
-type TokenStore struct {
|
|
|
+type TokenStore interface {
|
|
|
+ Get(string) *entitys.Token
|
|
|
+ Set(string, *entitys.Token)
|
|
|
+ Remove(key string)
|
|
|
+ Refresh(key string)
|
|
|
+}
|
|
|
+
|
|
|
+type MemoryStore struct {
|
|
|
name string
|
|
|
lock *sync.RWMutex
|
|
|
tokens map[string]*entitys.Token
|
|
|
}
|
|
|
|
|
|
-var globalTokenStore *TokenStore = nil
|
|
|
+var globalTokenStore TokenStore
|
|
|
|
|
|
func init() {
|
|
|
- globalTokenStore = &TokenStore{name: "sso", lock: new(sync.RWMutex), tokens: make(map[string]*entitys.Token)}
|
|
|
- go globalTokenStore.startTokenCheckProcess()
|
|
|
+ memoryStore := &MemoryStore{name: "sso", lock: new(sync.RWMutex), tokens: make(map[string]*entitys.Token)}
|
|
|
+ globalTokenStore = memoryStore
|
|
|
+ go memoryStore.startTokenCheckProcess()
|
|
|
}
|
|
|
|
|
|
-func GetGlobalTokenStore() *TokenStore {
|
|
|
+func GetGlobalTokenStore() TokenStore {
|
|
|
return globalTokenStore
|
|
|
}
|
|
|
|
|
|
-func (t *TokenStore) Get(key string) *entitys.Token {
|
|
|
+func SetGlobalTokenStore(store TokenStore) {
|
|
|
+ globalTokenStore = store
|
|
|
+}
|
|
|
+
|
|
|
+func (t *MemoryStore) Get(key string) *entitys.Token {
|
|
|
t.lock.RLock()
|
|
|
defer t.lock.RUnlock()
|
|
|
if val, ok := t.tokens[key]; ok {
|
|
|
@@ -38,7 +51,7 @@ func (t *TokenStore) Get(key string) *entitys.Token {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (t *TokenStore) Set(key string, v *entitys.Token) {
|
|
|
+func (t *MemoryStore) Set(key string, v *entitys.Token) {
|
|
|
t.lock.Lock()
|
|
|
defer t.lock.Unlock()
|
|
|
if val, ok := t.tokens[key]; !ok {
|
|
|
@@ -49,14 +62,14 @@ func (t *TokenStore) Set(key string, v *entitys.Token) {
|
|
|
log.Println(key, "添加Token:", v.AccessToken, v.RefreshToken, v.LoginID)
|
|
|
}
|
|
|
|
|
|
-func (t *TokenStore) Remove(key string) {
|
|
|
+func (t *MemoryStore) Remove(key string) {
|
|
|
t.lock.Lock()
|
|
|
defer t.lock.Unlock()
|
|
|
delete(t.tokens, key)
|
|
|
log.Println(key, "删除Key")
|
|
|
}
|
|
|
|
|
|
-func (t *TokenStore) Refresh(key string) {
|
|
|
+func (t *MemoryStore) Refresh(key string) {
|
|
|
t.lock.Lock()
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
@@ -66,7 +79,7 @@ func (t *TokenStore) Refresh(key string) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (t *TokenStore) startTokenCheckProcess() {
|
|
|
+func (t *MemoryStore) startTokenCheckProcess() {
|
|
|
autoRefresh := config.AppConfig.AutoRefresh
|
|
|
if !autoRefresh {
|
|
|
return
|
|
|
@@ -127,63 +140,3 @@ func TokenValidate(token string) (*entitys.Token, error) {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
-//func ValidateApp(appName, accessToken, timestamp, signature string) (*entitys.Token, error) {
|
|
|
-// fmt.Println(appName, accessToken, timestamp, signature)
|
|
|
-// if appName == "" || timestamp == "" || accessToken == "" || signature == "" {
|
|
|
-// return nil, errors.New("some param is empty")
|
|
|
-// }
|
|
|
-//
|
|
|
-// // check time out
|
|
|
-// timestampi, err := strconv.ParseInt(timestamp, 10, 64)
|
|
|
-// if err != nil {
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-//
|
|
|
-// timestampt := time.Unix(timestampi, 0)
|
|
|
-// subval := time.Now().Sub(timestampt)
|
|
|
-//
|
|
|
-// period := config.ApiConfig.GetInt("period")
|
|
|
-//
|
|
|
-// if subval.Seconds() < 0 || subval.Seconds() > float64(period) {
|
|
|
-// return nil, errors.New("the request is out of time")
|
|
|
-// }
|
|
|
-// // end of check time out
|
|
|
-//
|
|
|
-// // validate accessToken
|
|
|
-// appInfo, err := appManager.GetUamAppByName(appName)
|
|
|
-// if err != nil {
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-// fmt.Println("----------------------------3")
|
|
|
-//
|
|
|
-// signatureOrg := appInfo.Name + "." + timestamp + "." + appInfo.Token // generate the
|
|
|
-// hs := md5.New()
|
|
|
-// hs.Write([]byte(signatureOrg))
|
|
|
-// signatureStr := base64.StdEncoding.EncodeToString(hs.Sum(nil))
|
|
|
-//
|
|
|
-// fmt.Println("-----------------signatureStr", signatureStr)
|
|
|
-//
|
|
|
-// if signatureStr != signature {
|
|
|
-// return nil, errors.New("the signature is invalid")
|
|
|
-// }
|
|
|
-// fmt.Println("----------------------------2")
|
|
|
-//
|
|
|
-// str := appInfo.Token + "." + timestamp // generate accesstoken
|
|
|
-// hs = md5.New()
|
|
|
-// hs.Write([]byte(str))
|
|
|
-// md5Str := base64.StdEncoding.EncodeToString(hs.Sum(nil))
|
|
|
-// if accessToken != md5Str {
|
|
|
-// return nil, errors.New("token is invalid")
|
|
|
-// }
|
|
|
-//
|
|
|
-// fmt.Println("----------------------------1")
|
|
|
-//
|
|
|
-// rt := &entitys.Token{}
|
|
|
-// rt.AccessToken = utils.GenerateToken(accessToken + "." + timestamp)
|
|
|
-// rt.Result = 0
|
|
|
-// rt.UserId = appInfo.Id
|
|
|
-// rt.LoginID = appInfo.Name
|
|
|
-// rt.TimeStamp = uint64(timestampi)
|
|
|
-// return rt, nil
|
|
|
-//}
|