2018-05-03 11:24:07 +00:00
|
|
|
package auth
|
2018-04-14 08:57:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
|
|
|
logging "git.loafle.net/commons/logging-go"
|
|
|
|
crc "git.loafle.net/commons/rpc-go/client"
|
2018-04-18 14:56:13 +00:00
|
|
|
csc "git.loafle.net/commons/server-go/client"
|
2018-04-14 08:57:01 +00:00
|
|
|
cur "git.loafle.net/commons/util-go/reflect"
|
2018-04-26 08:39:32 +00:00
|
|
|
occn "git.loafle.net/overflow/commons-go/config/noauthprobe"
|
2018-04-14 08:57:01 +00:00
|
|
|
"git.loafle.net/overflow/probe/auth/info"
|
2018-05-03 11:24:07 +00:00
|
|
|
"git.loafle.net/overflow/probe/client"
|
2018-04-14 11:04:07 +00:00
|
|
|
"git.loafle.net/overflow/probe/config"
|
2018-04-14 08:57:01 +00:00
|
|
|
)
|
|
|
|
|
2018-05-10 10:18:00 +00:00
|
|
|
func New(responseHandler func(method string, param string), services ...interface{}) (*crc.Client, error) {
|
2018-04-14 11:04:07 +00:00
|
|
|
config := config.GetConfig()
|
2018-04-14 08:57:01 +00:00
|
|
|
if nil == config {
|
|
|
|
return nil, fmt.Errorf("Config is not available")
|
|
|
|
}
|
|
|
|
authConfig := getAuthConfig()
|
|
|
|
if nil == authConfig {
|
|
|
|
return nil, fmt.Errorf("AuthConfig is not available")
|
|
|
|
}
|
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
connector, err := client.NewConnector("Auth", occn.HTTPEntry_Auth)
|
2018-04-14 08:57:01 +00:00
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
connector.RequestHeader = func() http.Header {
|
|
|
|
header := make(map[string][]string)
|
|
|
|
switch authConfig.State() {
|
2018-04-26 08:39:32 +00:00
|
|
|
case occn.AuthStateTypeRegisterd:
|
|
|
|
header[occn.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{occn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect}
|
|
|
|
header[occn.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{*authConfig.TempKey}
|
2018-04-14 08:57:01 +00:00
|
|
|
default:
|
|
|
|
rh, err := info.GetRegistHeader(config.Account.APIKey)
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
return header
|
|
|
|
}
|
2018-04-26 08:39:32 +00:00
|
|
|
header[occn.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{occn.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist}
|
|
|
|
header[occn.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{rh}
|
2018-04-14 08:57:01 +00:00
|
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
connector.ResponseHandler = func(res *http.Response) {
|
2018-05-10 10:18:00 +00:00
|
|
|
responseMethod := res.Header.Get(occn.HTTPResponseHeaderKey_NoAuthProbe_Method)
|
|
|
|
responseMethodParam := res.Header.Get(occn.HTTPResponseHeaderKey_NoAuthProbe_Method_Param)
|
|
|
|
|
|
|
|
if nil != responseHandler {
|
|
|
|
responseHandler(responseMethod, responseMethodParam)
|
2018-04-14 08:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:56:13 +00:00
|
|
|
connector.OnDisconnected = func(connector csc.Connector) {
|
|
|
|
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
|
|
|
|
}
|
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
return client.New("Auth", connector, services), nil
|
2018-04-14 08:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 08:39:32 +00:00
|
|
|
func getAuthConfig() *occn.Auth {
|
2018-04-14 08:57:01 +00:00
|
|
|
_config, err := cdr.GetInstanceByName("AuthConfig")
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-26 08:39:32 +00:00
|
|
|
config, ok := _config.(*occn.Auth)
|
2018-04-14 08:57:01 +00:00
|
|
|
if !ok {
|
|
|
|
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
|
|
|
|
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|