Skip to content

Commit

Permalink
feat: middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
huaxiabuluo committed May 11, 2022
1 parent b8957b2 commit f80ca90
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 261 deletions.
33 changes: 33 additions & 0 deletions server-v2/api/studio/internal/handler/gateway/disonnecthandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions server-v2/api/studio/internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions server-v2/api/studio/internal/logic/gateway/disonnectlogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gateway

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

type DisonnectLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewDisonnectLogic(ctx context.Context, svcCtx *svc.ServiceContext) DisonnectLogic {
return DisonnectLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *DisonnectLogic) Disonnect(req types.DisconnectDBParams) (*types.AnyResponse, error) {
return service.NewGatewayService(l.ctx, l.svcCtx).DisconnectDB(&req)
}
49 changes: 9 additions & 40 deletions server-v2/api/studio/internal/service/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package service

import (
"context"
"encoding/base64"
"fmt"
"strings"

"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/gateway/dao"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
"github.com/vesoft-inc/nebula-studio/server/api/studio/pkg/auth"
"github.com/vesoft-inc/nebula-studio/server/api/studio/pkg/ecode"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -21,6 +16,7 @@ type (
GatewayService interface {
GetExec(request *types.ExecNGQLParams) (*types.AnyResponse, error)
ConnectDB(request *types.ConnectDBParams) (*types.ConnectDBResult, error)
DisconnectDB(request *types.DisconnectDBParams) (*types.AnyResponse, error)
}

gatewayService struct {
Expand All @@ -47,41 +43,14 @@ func (s *gatewayService) GetExec(request *types.ExecNGQLParams) (*types.AnyRespo
}

func (s *gatewayService) ConnectDB(request *types.ConnectDBParams) (*types.ConnectDBResult, error) {
fmt.Println("======request999", request)
tokenSplit := strings.Split(request.Authorization, " ")
if len(tokenSplit) != 2 {
return nil, ecode.WithCode(ecode.ErrParam, nil, "invalid authorization")
}

decode, err := base64.StdEncoding.DecodeString(tokenSplit[1])
if err != nil {
return nil, ecode.WithCode(ecode.ErrParam, err)
}

loginInfo := strings.Split(string(decode), ":")
if len(loginInfo) < 2 {
return nil, ecode.WithCode(ecode.ErrParam, nil, "len of account is less than two")
}

username, password := loginInfo[0], loginInfo[1]
clientInfo, err := dao.Connect(request.Address, request.Port, username, password)

tokenString, err := auth.CreateToken(
&auth.AuthData{
NebulaAddress: request.Address,
Username: username,
ClientID: clientInfo.ClientID,
},
&s.svcCtx.Config,
)

fmt.Println("=====tokenString", tokenString)

if err != nil {
return nil, ecode.WithInternalServer(err, "connect db failed")
}

return &types.ConnectDBResult{
Version: string(clientInfo.NebulaVersion),
Version: string(request.NebulaVersion),
}, nil
}

func (s *gatewayService) DisconnectDB(request *types.DisconnectDBParams) (*types.AnyResponse, error) {
if request.Nsid != "" {
dao.Disconnect(request.Nsid)
}
return nil, nil
}
5 changes: 5 additions & 0 deletions server-v2/api/studio/internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 79 additions & 39 deletions server-v2/api/studio/pkg/auth/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (

"github.com/golang-jwt/jwt/v4"
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/gateway/dao"
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/gateway/pool"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/config"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
"github.com/vesoft-inc/nebula-studio/server/api/studio/pkg/ecode"
"github.com/vesoft-inc/nebula-studio/server/api/studio/pkg/utils"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
)
Expand All @@ -32,6 +35,38 @@ type (

var globalConfig = new(config.Config)

func parseConnectDBParams(params *types.ConnectDBParams, config *config.Config) (string, *pool.ClientInfo, error) {
tokenSplit := strings.Split(params.Authorization, " ")
if len(tokenSplit) != 2 {
return "", nil, ecode.WithCode(ecode.ErrParam, nil, "invalid authorization")
}

decode, err := base64.StdEncoding.DecodeString(tokenSplit[1])
if err != nil {
return "", nil, ecode.WithCode(ecode.ErrParam, err)
}

loginInfo := strings.Split(string(decode), ":")
if len(loginInfo) < 2 {
return "", nil, ecode.WithCode(ecode.ErrParam, nil, "len of account is less than two")
}

username, password := loginInfo[0], loginInfo[1]
clientInfo, err := dao.Connect(params.Address, params.Port, username, password)
if err != nil {
return "", nil, ecode.WithCode(ecode.ErrInternalServer, err)
}

tokenString, err := CreateToken(
&AuthData{
NebulaAddress: params.Address,
Username: username,
},
config,
)
return tokenString, clientInfo, err
}

func CreateToken(authData *AuthData, config *config.Config) (string, error) {
now := time.Now()
expiresAt := now.Add(time.Duration(config.Auth.AccessExpire) * time.Second).Unix()
Expand All @@ -52,52 +87,57 @@ func AuthMiddlewareWithConfig(config *config.Config) rest.Middleware {
return func(w http.ResponseWriter, r *http.Request) {
// login handler
if strings.HasSuffix(r.URL.Path, "/connect") {
fmt.Println("=====global middleware", r.URL.Path)
var req types.ConnectDBParams
err := httpx.Parse(r, &req)
rClone := utils.CopyHttpRequest(r)
err := httpx.Parse(rClone, &req)
if err != nil {
fmt.Println("=====req3333", req)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("=====err", err)
fmt.Println("=====req.Address", req.Address)
fmt.Println("=====req.Port", req.Port)
fmt.Println("=====req.Authorization", req.Authorization)
}
c1 := http.Cookie{
Name: "access_token",
Value: "12333",
Path: "/",
HttpOnly: true,
MaxAge: 3600,
}

var req1 types.ConnectDBParams
err1 := httpx.Parse(r, &req1)
fmt.Println("=====err1=====", err1)
tokenString, clientInfo, err := parseConnectDBParams(&req, config)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Set-Cookie", c1.String())
next(w, r)
}
}
}
token := http.Cookie{
Name: "token",
Value: tokenString,
Path: "/",
HttpOnly: true,
MaxAge: 1800,
}
nsid := http.Cookie{
Name: "nsid",
Value: clientInfo.ClientID,
Path: "/",
HttpOnly: true,
MaxAge: 1800,
}

func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// login handler
if strings.HasSuffix(r.URL.Path, "/connect") {
fmt.Println("=====global middleware", r.URL.Path)
}
c1 := http.Cookie{
Name: "access_token",
Value: "12333",
Path: "/",
HttpOnly: true,
MaxAge: 3600,
query := r.URL.Query()
query.Set("nebulaVersion", string(clientInfo.NebulaVersion))
r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode())

// w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Set-Cookie", token.String())
w.Header().Add("Set-Cookie", nsid.String())
} else if strings.HasSuffix(r.URL.Path, "/disconnect") {
nsidCookie, err := r.Cookie("nsid")

if err == nil {
query := r.URL.Query()
query.Set("nsid", nsidCookie.Value)
r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode())
}

w.Header().Set("Set-Cookie", utils.DisabledCookie("token").String())
w.Header().Add("Set-Cookie", utils.DisabledCookie("nsid").String())
} else {
}
next(w, r)
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Set-Cookie", c1.String())
next(w, r)
}
}

Expand Down
Loading

0 comments on commit f80ca90

Please sign in to comment.