deprecated_overflow_gateway.../server/auth_servlet.go
crusader aac31c8218 ing
2017-09-22 18:20:17 +09:00

101 lines
2.8 KiB
Go

package server
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_probe/grpc"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
"github.com/valyala/fasthttp"
)
const (
noAuthHeaderNoAuthID = "overFlow-NoAuth-ID"
noAuthHeaderNoAuthRegist = "overFlow-NoAuth-Regist"
noAuthHeaderSetNoAuthID = "overFlow-Set-NoAuth-ID"
)
func newAuthServlet() Servlet {
s := &authServlet{}
return s
}
type authServlet struct {
}
func (s *authServlet) IsCanConnect(ctx *fasthttp.RequestCtx) bool {
var buf []byte
if buf = ctx.Request.Header.Peek(noAuthHeaderNoAuthID); nil != buf {
return s.noAuthConnect(ctx, string(buf))
}
if buf = ctx.Request.Header.Peek(noAuthHeaderNoAuthRegist); nil != buf {
return s.noAuthRegist(ctx, string(buf))
}
return false
}
func (s *authServlet) noAuthConnect(ctx *fasthttp.RequestCtx, tempKey string) bool {
var err error
gctx := context.Background()
params := []string{tempKey}
var result string
if result, err = grpc.Exec(gctx, "NoAuthProbeService.readByTempKey", params); nil != err {
logging.Logger.Warn(fmt.Sprintf("Auth: Invalid connect tempKey[%s] ip[%s]", tempKey, ctx.RemoteAddr().String()))
return false
}
var nap NoAuthProbe
if err = json.Unmarshal([]byte(result), &nap); nil != err {
logging.Logger.Warn(fmt.Sprintf("Auth: Cannot conver json[%s] ip[%s]", result, ctx.RemoteAddr().String()))
return false
}
return true
}
func (s *authServlet) noAuthRegist(ctx *fasthttp.RequestCtx, encInfo string) bool {
var buf []byte
var err error
if buf, err = base64.StdEncoding.DecodeString(encInfo); nil != err {
logging.Logger.Warn(fmt.Sprintf("Auth: Invalid noAuth regist base64[%s] ip[%s]", encInfo, ctx.RemoteAddr().String()))
return false
}
noauthJSON := string(buf)
gctx := context.Background()
params := []string{noauthJSON}
var result string
if result, err = grpc.Exec(gctx, "NoAuthProbeService.regist", params); nil != err {
logging.Logger.Warn(fmt.Sprintf("Auth: Invalid noAuth regist info[%s] ip[%s]", noauthJSON, ctx.RemoteAddr().String()))
return false
}
var nap NoAuthProbe
if err = json.Unmarshal([]byte(result), &nap); nil != err {
logging.Logger.Warn(fmt.Sprintf("Auth: Invalid noAuth regist result[%s] ip[%s]", result, ctx.RemoteAddr().String()))
return false
}
ctx.Request.Header.Set(noAuthHeaderNoAuthID, nap.TempProbeKey)
ctx.Response.Header.Set(noAuthHeaderSetNoAuthID, nap.TempProbeKey)
return true
}
func (s *authServlet) SessionUID(soc ogw.Socket) string {
var buf []byte
if buf = soc.Conn().Headers().Peek(noAuthHeaderNoAuthID); nil != buf {
return string(buf)
}
return ""
}
type NoAuthProbe struct {
ID uint64 `json:"id"`
Description string `json:"description"`
TempProbeKey string `json:"tempProbeKey"`
}