This commit is contained in:
joy.zhou
2019-07-16 14:49:25 +08:00
parent 33af1a59c4
commit f2b8f88ad4
12 changed files with 1560 additions and 7 deletions

View File

@@ -58,6 +58,16 @@ func Init() {
//CheckAuth check mqtt connect
func CheckAuth(clientID, username, password string) bool {
action := "connect"
{
aCache := checkCache(action, clientID, username, password, "")
if aCache != nil {
if aCache.password == password && aCache.username == username && aCache.action == action {
return true
}
}
}
data := url.Values{}
data.Add("username", username)
data.Add("clientid", clientID)
@@ -79,6 +89,7 @@ func CheckAuth(clientID, username, password string) bool {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
addCache(action, clientID, username, password, "")
return true
}
return false
@@ -86,6 +97,16 @@ func CheckAuth(clientID, username, password string) bool {
//CheckSuper check mqtt connect
func CheckSuper(clientID, username, password string) bool {
action := "connect"
{
aCache := checkCache(action, clientID, username, password, "")
if aCache != nil {
if aCache.password == password && aCache.username == username && aCache.action == action {
return true
}
}
}
data := url.Values{}
data.Add("username", username)
data.Add("clientid", clientID)
@@ -114,6 +135,16 @@ func CheckSuper(clientID, username, password string) bool {
//CheckACL check mqtt connect
func CheckACL(username, access, topic string) bool {
action := access
{
aCache := checkCache(action, "", username, "", topic)
if aCache != nil {
if aCache.topic == topic && aCache.action == action {
return true
}
}
}
req, err := http.NewRequest("GET", config.ACLURL, nil)
if err != nil {
log.Error("get acl: ", zap.Error(err))
@@ -135,6 +166,7 @@ func CheckACL(username, access, topic string) bool {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
addCache(action, "", username, "", topic)
return true
}
return false

32
plugins/authhttp/cache.go Normal file
View File

@@ -0,0 +1,32 @@
package authhttp
import (
"time"
"github.com/patrickmn/go-cache"
)
type authCache struct {
action string
username string
clientID string
password string
topic string
}
var (
// cache = make(map[string]authCache)
c = cache.New(5*time.Minute, 10*time.Minute)
)
func checkCache(action, clientID, username, password, topic string) *authCache {
authc, found := c.Get(username)
if found {
return authc.(*authCache)
}
return nil
}
func addCache(action, clientID, username, password, topic string) {
c.Set(username, &authCache{action: action, username: username, clientID: clientID, password: password, topic: topic}, cache.DefaultExpiration)
}