init
This commit is contained in:
23
logic/auth/auth.go
Normal file
23
logic/auth/auth.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package auth
|
||||
|
||||
import "time"
|
||||
|
||||
var execAuth = make(map[string]CreateFunc)
|
||||
|
||||
type CreateFunc func(url string, timeout time.Duration) Auth
|
||||
|
||||
func Register(name string, exec CreateFunc) {
|
||||
execAuth[name] = exec
|
||||
}
|
||||
|
||||
func Load(name string) (CreateFunc, error) {
|
||||
exec, ok := execAuth[name]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
return exec, nil
|
||||
}
|
||||
|
||||
type Auth interface {
|
||||
DoAuth(token string) (string, error)
|
||||
}
|
||||
67
logic/auth/dtalk/auth.go
Normal file
67
logic/auth/dtalk/auth.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package acc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitlab.33.cn/btrade/auto_trade_tools/reqtypes"
|
||||
"gitlab.33.cn/btrade/auto_trade_tools/util"
|
||||
)
|
||||
|
||||
type talkClient struct {
|
||||
url string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (a *talkClient) DoAuth(token string) (uid string, err error) {
|
||||
var (
|
||||
bytes []byte
|
||||
)
|
||||
headers := map[string]string{}
|
||||
headers["FZM-SIGNATURE"] = token
|
||||
bytes, err = util.HttpReq(&reqtypes.HttpParams{
|
||||
Method: "POST",
|
||||
ReqUrl: a.url,
|
||||
HeaderMap: headers,
|
||||
Timeout: a.timeout,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
err = json.Unmarshal(bytes, &res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if e, ok := res["error"]; ok {
|
||||
err = errors.New(e.(string))
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := res["data"]; !ok {
|
||||
err = errors.New("invalid auth res")
|
||||
return
|
||||
}
|
||||
|
||||
data, ok := res["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
err = errors.New("invalid auth data format")
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := data["address"]; !ok {
|
||||
err = errors.New("invalid auth data")
|
||||
return
|
||||
}
|
||||
|
||||
uid, ok = data["address"].(string)
|
||||
if !ok {
|
||||
err = errors.New("invalid auth data id format")
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
17
logic/auth/dtalk/plugin.go
Normal file
17
logic/auth/dtalk/plugin.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package acc
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitlab.33.cn/chat/im/logic/auth"
|
||||
)
|
||||
|
||||
const Name = "dtalk"
|
||||
|
||||
func init() {
|
||||
auth.Register(Name, NewAuth)
|
||||
}
|
||||
|
||||
func NewAuth(url string, timeout time.Duration) auth.Auth {
|
||||
return &talkClient{url: url, timeout: timeout}
|
||||
}
|
||||
67
logic/auth/zhaobi/auth.go
Normal file
67
logic/auth/zhaobi/auth.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package acc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitlab.33.cn/btrade/auto_trade_tools/reqtypes"
|
||||
"gitlab.33.cn/btrade/auto_trade_tools/util"
|
||||
)
|
||||
|
||||
type talkClient struct {
|
||||
url string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (a *talkClient) DoAuth(token string) (uid string, err error) {
|
||||
var (
|
||||
bytes []byte
|
||||
)
|
||||
headers := map[string]string{}
|
||||
headers["Authorization"] = token
|
||||
bytes, err = util.HttpReq(&reqtypes.HttpParams{
|
||||
Method: "GET",
|
||||
ReqUrl: a.url,
|
||||
HeaderMap: headers,
|
||||
Timeout: a.timeout,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
err = json.Unmarshal(bytes, &res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if e, ok := res["error"]; ok {
|
||||
err = errors.New(e.(string))
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := res["data"]; !ok {
|
||||
err = errors.New("invalid auth res")
|
||||
return
|
||||
}
|
||||
|
||||
data, ok := res["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
err = errors.New("invalid auth data format")
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := data["user_id"]; !ok {
|
||||
err = errors.New("invalid auth data")
|
||||
return
|
||||
}
|
||||
|
||||
uid, ok = data["user_id"].(string)
|
||||
if !ok {
|
||||
err = errors.New("invalid auth data id format")
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
17
logic/auth/zhaobi/plugin.go
Normal file
17
logic/auth/zhaobi/plugin.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package acc
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitlab.33.cn/chat/im/logic/auth"
|
||||
)
|
||||
|
||||
const Name = "zb_otc"
|
||||
|
||||
func init() {
|
||||
auth.Register(Name, NewAuth)
|
||||
}
|
||||
|
||||
func NewAuth(url string, timeout time.Duration) auth.Auth {
|
||||
return &talkClient{url: url, timeout: timeout}
|
||||
}
|
||||
Reference in New Issue
Block a user