From 1d838d813d840dfc2b2f081e75757770a47c50fb Mon Sep 17 00:00:00 2001 From: snoop Date: Tue, 6 Jun 2017 17:04:50 +0900 Subject: [PATCH] added auth --- initialize/rpc_impl.go | 76 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/initialize/rpc_impl.go b/initialize/rpc_impl.go index afbe0fe..ee9cd30 100644 --- a/initialize/rpc_impl.go +++ b/initialize/rpc_impl.go @@ -3,20 +3,92 @@ 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" + "encoding/json" ) type InitializeServerImpl struct { } -func (s *InitializeServerImpl) GetSecretKey(c context.Context, in *pb.AgentInfo) (*pb.InitResponse, error) { +func (s *InitializeServerImpl) StartAgent(c context.Context, info *pb.AgentInfo) (*pb.InitResponse, error) { output := &pb.InitResponse{} //todo. generating secretkey - agentId := in.GetAgentId() + 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 + //ti.PollInterval + + 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_AUTH_WAIT + + str, err := ns.CheckAuth(info.Value) + + if err != nil { + return as, err + } + + //FIXME auth type + res := "" + + err = json.Unmarshal([]byte(str), &res) + if err != nil { + return as , nil + } + + if res == "ACCEPT" { + as.Type = pb.AuthStatus_AUTH_ACCEPT + } else if res == "REFUSE" { + as.Type = pb.AuthStatus_AUTH_REFUSE + } + + return as, nil + +} + +