|
|
@@ -8,10 +8,13 @@ import (
|
|
|
"crypto/md5"
|
|
|
"encoding/binary"
|
|
|
"encoding/hex"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "io/ioutil"
|
|
|
"log"
|
|
|
"net"
|
|
|
+ "net/http"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
@@ -36,6 +39,8 @@ type Token struct {
|
|
|
}
|
|
|
|
|
|
type TokenStore struct {
|
|
|
+ storeServiceUrl string
|
|
|
+
|
|
|
name string
|
|
|
lock *sync.RWMutex
|
|
|
tokens map[string]*Token
|
|
|
@@ -47,13 +52,21 @@ type IAuth interface {
|
|
|
|
|
|
var globalTokenStore *TokenStore = nil
|
|
|
|
|
|
-func init() {
|
|
|
- iauthMap = make(map[string]IAuth)
|
|
|
+func Init(c *config.Config) {
|
|
|
globalTokenStore = &TokenStore{name: "sso", lock: new(sync.RWMutex), tokens: make(map[string]*Token)}
|
|
|
- go globalTokenStore.startTokenCheckProcess()
|
|
|
+ if strings.HasPrefix(c.Erp.AuthServer, "http") {
|
|
|
+ globalTokenStore.storeServiceUrl = c.Erp.AuthServer
|
|
|
+ } else {
|
|
|
+ iauthMap = make(map[string]IAuth)
|
|
|
+
|
|
|
+ go globalTokenStore.startTokenCheckProcess()
|
|
|
+
|
|
|
+ lightAuth := &LightAuth{}
|
|
|
+ RegisterAuth("qianqiusoft.com", lightAuth)
|
|
|
|
|
|
- lightAuth := &LightAuth{}
|
|
|
- RegisterAuth("qianqiusoft.com", lightAuth)
|
|
|
+ erpClient := NewTcpClient(c)
|
|
|
+ erpClient.Start()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type LightAuth struct {
|
|
|
@@ -89,14 +102,32 @@ func GetGlobalTokenStore() *TokenStore {
|
|
|
return globalTokenStore
|
|
|
}
|
|
|
|
|
|
-func (t *TokenStore) Get(key string) *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
|
|
|
+func (t *TokenStore) Get(key string) string {
|
|
|
+ if t.storeServiceUrl == "" {
|
|
|
+ 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.UserId
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resp, err := http.Get(t.storeServiceUrl + key)
|
|
|
+ if err != nil {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ b, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ token := &Token{}
|
|
|
+ err = json.Unmarshal(b, &token)
|
|
|
+ if err != nil {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ return token.UserId
|
|
|
}
|
|
|
- return nil
|
|
|
+
|
|
|
+ return ""
|
|
|
}
|
|
|
|
|
|
func (t *TokenStore) Set(key string, v *Token) {
|
|
|
@@ -129,38 +160,6 @@ func (t *TokenStore) Refresh(key string) {
|
|
|
func (t *TokenStore) startTokenCheckProcess() {
|
|
|
}
|
|
|
|
|
|
-func Validate(accessToken, loginId string, domain string) (*Token, error) {
|
|
|
- token := globalTokenStore.Get(loginId + domain)
|
|
|
- if token != nil {
|
|
|
- if strings.EqualFold(token.AccessToken, accessToken) {
|
|
|
- logx.Info("get the token ", accessToken, " of id ", loginId+domain)
|
|
|
- globalTokenStore.Refresh(loginId + domain)
|
|
|
- return token, nil
|
|
|
- } else {
|
|
|
- logx.Error(token.AccessToken, "is not equal to", accessToken)
|
|
|
- return token, errors.New(token.AccessToken + " is not equal to " + accessToken)
|
|
|
- }
|
|
|
- } else {
|
|
|
- logx.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) (*Token, error) {
|
|
|
- user := globalTokenStore.Get(token)
|
|
|
-
|
|
|
- if strings.EqualFold(user.AccessToken, token) {
|
|
|
- logx.Info("get the token ", token, " of id ")
|
|
|
- globalTokenStore.Refresh(token)
|
|
|
- return user, nil
|
|
|
- } else {
|
|
|
- logx.Error(user.AccessToken, "is not equal to", token)
|
|
|
- return user, errors.New(user.AccessToken + " is not equal to " + token)
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
const (
|
|
|
__KEY = "Light#dauth-@*I2"
|
|
|
|