2018-04-14 08:57:01 +00:00
|
|
|
package central
|
|
|
|
|
|
|
|
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"
|
|
|
|
cur "git.loafle.net/commons/util-go/reflect"
|
|
|
|
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
|
|
|
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
|
|
|
|
"git.loafle.net/overflow/probe/auth/info"
|
2018-04-14 11:04:07 +00:00
|
|
|
"git.loafle.net/overflow/probe/config"
|
2018-04-14 08:57:01 +00:00
|
|
|
)
|
|
|
|
|
2018-04-17 10:28:37 +00:00
|
|
|
func NewAuth(tempKeyHandler func(tempKey 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
connector, err := newConnector("Auth", ocncc.HTTPEntry_Auth)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
connector.RequestHeader = func() http.Header {
|
|
|
|
header := make(map[string][]string)
|
|
|
|
switch authConfig.State() {
|
|
|
|
case ocnc.AuthStateTypeRegisterd:
|
|
|
|
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect}
|
|
|
|
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{*authConfig.TempKey}
|
|
|
|
default:
|
|
|
|
rh, err := info.GetRegistHeader(config.Account.APIKey)
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist}
|
|
|
|
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{rh}
|
|
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
connector.ResponseHandler = func(res *http.Response) {
|
|
|
|
switch authConfig.State() {
|
|
|
|
case ocnc.AuthStateTypeNotRegisterd:
|
|
|
|
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
|
2018-04-14 11:04:07 +00:00
|
|
|
if nil != tempKeyHandler {
|
|
|
|
tempKeyHandler(tempProbeKey)
|
2018-04-14 08:57:01 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newClient("Auth", connector, services), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAuthConfig() *ocnc.Auth {
|
|
|
|
_config, err := cdr.GetInstanceByName("AuthConfig")
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
config, ok := _config.(*ocnc.Auth)
|
|
|
|
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
|
|
|
|
}
|