79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
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"
|
|
"git.loafle.net/overflow/probe/config"
|
|
)
|
|
|
|
func NewAuth(tempKeyHandler func(tempKey string), services ...interface{}) (*crc.Client, error) {
|
|
config := config.GetConfig()
|
|
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)
|
|
if nil != tempKeyHandler {
|
|
tempKeyHandler(tempProbeKey)
|
|
}
|
|
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
|
|
}
|