diff --git a/servlet/data/gateway_rpc_handlers.go b/servlet/data/gateway_rpc_handlers.go index 151f1dd..2ba374c 100644 --- a/servlet/data/gateway_rpc_handlers.go +++ b/servlet/data/gateway_rpc_handlers.go @@ -1,13 +1,14 @@ package data import ( - "fmt" + "context" + "encoding/json" - "git.loafle.net/commons_go/logging" cwf "git.loafle.net/commons_go/websocket_fasthttp" - "git.loafle.net/overflow/overflow_gateway_websocket/config" + oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe" + probeM "git.loafle.net/overflow/overflow_commons_go/modules/probe/model" + oogweg "git.loafle.net/overflow/overflow_gateway_websocket/external/grpc" "git.loafle.net/overflow/overflow_gateway_websocket/rpc" - jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" ) @@ -26,30 +27,36 @@ func (sh *GatewayRPCHandlers) Init(serverCTX cwf.ServerContext) error { } func (sh *GatewayRPCHandlers) Handshake(socketCTX cwf.SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { - var ok bool - // tokenString := string(soc.Conn().Headers().Cookie("AuthToken")) - tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJvdmVyRmxvdyIsImlhdCI6MTUwNDU5NTExOSwiZXhwIjoxNTM2MTMxMTE5LCJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.-WQi3OykPlJ9x8RcZGhWXEtGw4GhU6wmyJ_AWh2rMeUatQylfPzvmum2Xfp6pwKLMmcP76XoDPNyq06i7RKWNQ" - token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { - // Don't forget to validate the alg is what you expect: - if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"]) - } + bMethod := ctx.Request.Header.Peek(oocmp.HTTPRequestHeaderKey_Probe_Method) + if nil == bMethod { + return "", nil + } + method := string(bMethod) - // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") - return []byte(config.Config.Auth.SigningKey), nil - }) + switch method { + case oocmp.HTTPRequestHeaderValue_Probe_Method_Connect: + default: + return "", nil + } + bProbeKey := ctx.Request.Header.Peek(oocmp.HTTPRequestHeaderKey_Probe_ProbeKey) + if nil == bProbeKey { + return "", nil + } + probeKey := string(bProbeKey) + + grpcCTX := context.Background() + r, err := oogweg.Exec(grpcCTX, "ProbeService.readByProbeKey", []string{probeKey}) + if nil != err { + return "", nil + } + probe := &probeM.Probe{} + err = json.Unmarshal([]byte(r), probe) if nil != err { return "", nil } - var claims jwt.MapClaims - if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid { - logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token)) - return "", nil - } - - return claims["sub"].(string), nil + return probeKey, nil } func (sh *GatewayRPCHandlers) Validate() { diff --git a/servlet/noauth/gateway_rpc_handlers.go b/servlet/noauth/gateway_rpc_handlers.go index 03e5fad..3dd8869 100644 --- a/servlet/noauth/gateway_rpc_handlers.go +++ b/servlet/noauth/gateway_rpc_handlers.go @@ -1,13 +1,15 @@ package noauth import ( - "fmt" + "context" + "encoding/base64" + "encoding/json" - "git.loafle.net/commons_go/logging" cwf "git.loafle.net/commons_go/websocket_fasthttp" - "git.loafle.net/overflow/overflow_gateway_websocket/config" + oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe" + noauthprobeM "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe/model" + oogweg "git.loafle.net/overflow/overflow_gateway_websocket/external/grpc" "git.loafle.net/overflow/overflow_gateway_websocket/rpc" - jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" ) @@ -26,30 +28,60 @@ func (sh *GatewayRPCHandlers) Init(serverCTX cwf.ServerContext) error { } func (sh *GatewayRPCHandlers) Handshake(socketCTX cwf.SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { - var ok bool - // tokenString := string(soc.Conn().Headers().Cookie("AuthToken")) - tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJvdmVyRmxvdyIsImlhdCI6MTUwNDU5NTExOSwiZXhwIjoxNTM2MTMxMTE5LCJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.-WQi3OykPlJ9x8RcZGhWXEtGw4GhU6wmyJ_AWh2rMeUatQylfPzvmum2Xfp6pwKLMmcP76XoDPNyq06i7RKWNQ" - token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { - // Don't forget to validate the alg is what you expect: - if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"]) + bMethod := ctx.Request.Header.Peek(oocmn.HTTPRequestHeaderKey_NoAuthProbe_Method) + if nil == bMethod { + return "", nil + } + method := string(bMethod) + + switch method { + case oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist: + bInfo := ctx.Request.Header.Peek(oocmn.HTTPRequestHeaderKey_NoAuthProbe_Info) + if nil == bInfo { + return "", nil + } + rJSON := make([]byte, base64.StdEncoding.DecodedLen(len(bInfo))) + _, err := base64.StdEncoding.Decode(rJSON, bInfo) + if nil != err { + return "", nil + } + grpcCTX := context.Background() + r, err := oogweg.Exec(grpcCTX, "NoAuthProbeService.regist", []string{string(rJSON)}) + if nil != err { + return "", nil + } + nap := &noauthprobeM.NoAuthProbe{} + err = json.Unmarshal([]byte(r), nap) + if nil != err { + return "", nil } - // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") - return []byte(config.Config.Auth.SigningKey), nil - }) + id = nap.TempProbeKey + extensionsHeader = &fasthttp.ResponseHeader{} + extensionsHeader.Add(oocmn.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey, nap.TempProbeKey) + return + case oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect: + bTempProbeKey := ctx.Request.Header.Peek(oocmn.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey) + if nil == bTempProbeKey { + return "", nil + } + grpcCTX := context.Background() + r, err := oogweg.Exec(grpcCTX, "NoAuthProbeService.readByTempKey", []string{string(bTempProbeKey)}) + if nil != err { + return "", nil + } + nap := &noauthprobeM.NoAuthProbe{} + err = json.Unmarshal([]byte(r), nap) + if nil != err { + return "", nil + } - if nil != err { + id = string(bTempProbeKey) + return + default: return "", nil } - var claims jwt.MapClaims - if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid { - logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token)) - return "", nil - } - - return claims["sub"].(string), nil } func (sh *GatewayRPCHandlers) Validate() { diff --git a/servlet/probe/gateway_rpc_handlers.go b/servlet/probe/gateway_rpc_handlers.go index 36a20a0..9c0c503 100644 --- a/servlet/probe/gateway_rpc_handlers.go +++ b/servlet/probe/gateway_rpc_handlers.go @@ -1,13 +1,15 @@ package probe import ( - "fmt" + "context" + "encoding/json" - "git.loafle.net/commons_go/logging" cwf "git.loafle.net/commons_go/websocket_fasthttp" - "git.loafle.net/overflow/overflow_gateway_websocket/config" + oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe" + probeM "git.loafle.net/overflow/overflow_commons_go/modules/probe/model" + oogweg "git.loafle.net/overflow/overflow_gateway_websocket/external/grpc" "git.loafle.net/overflow/overflow_gateway_websocket/rpc" - jwt "github.com/dgrijalva/jwt-go" + // jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" ) @@ -26,30 +28,61 @@ func (sh *GatewayRPCHandlers) Init(serverCTX cwf.ServerContext) error { } func (sh *GatewayRPCHandlers) Handshake(socketCTX cwf.SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { - var ok bool - // tokenString := string(soc.Conn().Headers().Cookie("AuthToken")) - tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJvdmVyRmxvdyIsImlhdCI6MTUwNDU5NTExOSwiZXhwIjoxNTM2MTMxMTE5LCJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.-WQi3OykPlJ9x8RcZGhWXEtGw4GhU6wmyJ_AWh2rMeUatQylfPzvmum2Xfp6pwKLMmcP76XoDPNyq06i7RKWNQ" - token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { - // Don't forget to validate the alg is what you expect: - if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"]) - } + bMethod := ctx.Request.Header.Peek(oocmp.HTTPRequestHeaderKey_Probe_Method) + if nil == bMethod { + return "", nil + } + method := string(bMethod) - // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") - return []byte(config.Config.Auth.SigningKey), nil - }) + switch method { + case oocmp.HTTPRequestHeaderValue_Probe_Method_Connect: + default: + return "", nil + } + // var ok bool + // // tokenString := string(soc.Conn().Headers().Cookie("AuthToken")) + // tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJvdmVyRmxvdyIsImlhdCI6MTUwNDU5NTExOSwiZXhwIjoxNTM2MTMxMTE5LCJhdWQiOiJ3d3cub3ZlcmZsb3cuY2xvdWQiLCJzdWIiOiJvdmVyZmxvd0Bsb2FmbGUuY29tIn0.-WQi3OykPlJ9x8RcZGhWXEtGw4GhU6wmyJ_AWh2rMeUatQylfPzvmum2Xfp6pwKLMmcP76XoDPNyq06i7RKWNQ" + // token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + // // Don't forget to validate the alg is what you expect: + // if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + // return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"]) + // } + + // // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") + // return []byte(config.Config.Auth.SigningKey), nil + // }) + + // if nil != err { + // return "", nil + // } + + // var claims jwt.MapClaims + // if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid { + // logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token)) + // return "", nil + // } + bProbeKey := ctx.Request.Header.Peek(oocmp.HTTPRequestHeaderKey_Probe_ProbeKey) + if nil == bProbeKey { + return "", nil + } + probeKey := string(bProbeKey) + + grpcCTX := context.Background() + r, err := oogweg.Exec(grpcCTX, "ProbeService.readByProbeKey", []string{probeKey}) + if nil != err { + return "", nil + } + probe := &probeM.Probe{} + err = json.Unmarshal([]byte(r), probe) if nil != err { return "", nil } - var claims jwt.MapClaims - if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid { - logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token)) - return "", nil - } + extensionsHeader = &fasthttp.ResponseHeader{} + extensionsHeader.Add(oocmp.HTTPResponseHeaderKey_Probe_SetEncryptionKey, probe.EncryptionKey) - return claims["sub"].(string), nil + return probeKey, extensionsHeader } func (sh *GatewayRPCHandlers) Validate() {