central_api_gateway_initialize/initialize/rpc_impl.go
snoop 6c3b7483db temped
poll interval time
2017-06-07 14:05:42 +09:00

100 lines
1.9 KiB
Go

package initialize
import (
pb "git.loafle.net/overflow/central_api_gateway/initialize"
"context"
"git.loafle.net/overflow/overflow_proxy_service/proxy/apikey"
"git.loafle.net/overflow/overflow_proxy_service/proxy/keystore"
"git.loafle.net/overflow/overflow_proxy_service/proxy/noauthagent"
)
type InitializeServerImpl struct {
}
func testPollInterval(types string) float64 {
var defaultTime float64 = 10
if types == "CheckAuth" {
return defaultTime
}
return defaultTime;
}
func (s *InitializeServerImpl) StartAgent(c context.Context, info *pb.AgentInfo) (*pb.InitResponse, error) {
output := &pb.InitResponse{}
//todo. generating secretkey
agentId := info.GetAgentId()
output.SecretKey = "overflow" + agentId
return output, nil
}
func (s *InitializeServerImpl) RequestTempKey(c context.Context, info *pb.ReqTempKeyInfo) (*pb.TempKeyInfo, error) {
as := apikey.NewApiKeyService()
ti := &pb.TempKeyInfo{}
b, err := as.CheckApikey(info.ApiKey)
if err != nil {
return ti, err
}
if b {
ks := keystore.NewKeyStoreSerivce()
key, err := ks.CreateKey()
if err != nil {
return ti, err
}
ns := noauthagent.NewNoAuthAgentService()
_, err = ns.SaveNoAuthAgent(noauthagent.NewNoAuthAgent(info.ApiKey, info.LocalIp, info.HostName))
if err != nil {
return ti, err
}
ti.TempKey = key
//FIXME:: check Poll intervar
time := testPollInterval("CheckAuth")
ti.PollInterval = time
return ti, nil
}
return ti, nil
}
func (s *InitializeServerImpl) CheckAuth(c context.Context, info *pb.TempKey) (*pb.AuthStatus, error) {
ns := noauthagent.NewNoAuthAgentService()
as := &pb.AuthStatus{}
as.Type = pb.AuthStatus_WAIT
str, err := ns.CheckAuth(info.Value)
if err != nil {
return as, err
}
if str == pb.AuthStatus_REFUSE.String() {
as.Type = pb.AuthStatus_ACCEPT
} else if str == pb.AuthStatus_REFUSE.String() {
as.Type = pb.AuthStatus_REFUSE
}
return as, nil
}