middleware

package
v1.20.76 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CorpTokenAuthFilter = func(ctx *context.Context) {
	token := ctx.Input.Query("token")
	if token == "" {
		return
	}

	corpId, err := decodeToken(token)

	if err != nil {
		response := vanilla.MakeErrorResponse(500, "corp_token:invalid_corp", fmt.Sprintf("无效的corp token 1 - [%s]", token))
		ctx.Output.JSON(response, true, false)
		return
	}

	if gBContextFactory != nil {
		jsonData := simplejson.New()
		jsonData.Set("corp_id", corpId)
		jsonData.Set("__source", "corp_token_auth")
		bCtx := gBContextFactory.NewContext(go_context.Background(), ctx.Request, 0, "", jsonData)

		spanCtx, _ := vanilla.Tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(ctx.Request.Header))
		uri := ctx.Request.URL.Path
		operationName := fmt.Sprintf("%s %s", ctx.Request.Method, uri)
		span := vanilla.Tracer.StartSpan(operationName, ext.RPCServerOption(spanCtx))
		bCtx = opentracing.ContextWithSpan(bCtx, span)

		o := orm.NewOrmWithSpan(span)
		bCtx = go_context.WithValue(bCtx, "orm", o)

		ctx.Input.SetData("bContext", bCtx)
		ctx.Input.SetData("span", opentracing.SpanFromContext(bCtx))
	}
}
View Source
var ExtractCorpTokenFilter = func(ctx *context.Context) {
	cookie, err := ctx.Request.Cookie("__cs")
	if err != nil {

		return
	}

	encodedToken := cookie.Value
	if encodedToken == "" {
		return
	}

	uuid, openid, err := encrypt.DecodeToken(encodedToken)
	if err != nil {
		beego.Error(err.Error())
		return
	}

	ctx.Input.SetData("__corp_token", fmt.Sprintf("%s____%s", uuid, openid))
}
View Source
var JWTAuthFilter = func(ctx *context.Context) {
	if _, ok := ctx.Input.Data()["bContext"]; ok {

		return
	}
	uri := ctx.Request.RequestURI

	if uri == "/" {
		if gBContextFactory != nil {
			bCtx := gBContextFactory.NewContext(go_context.Background(), ctx.Request, 0, "", nil)
			o := orm.NewOrm()
			bCtx = go_context.WithValue(bCtx, "orm", o)
			ctx.Input.SetData("bContext", bCtx)
		}
		return
	}

	for _, skipUrl := range SKIP_JWT_CHECK_URLS {
		if strings.Contains(uri, skipUrl) {
			if strings.Contains(uri, "/logined_microapp_corp_user") || strings.Contains(uri, "/logined_angler_user") || strings.Contains(uri, "/user_reflection") {

			} else {
				beego.Debug("[jwt_middleware] skip jwt check", "url", skipUrl)
				if gBContextFactory != nil {
					bCtx := gBContextFactory.NewContext(go_context.Background(), ctx.Request, 0, "", nil)
					o := orm.NewOrm()
					bCtx = go_context.WithValue(bCtx, "orm", o)

					ctx.Input.SetData("bContext", bCtx)
				}
				return
			}
		}
	}

	jwtToken := ctx.Input.Header("AUTHORIZATION")

	if jwtToken == "" {
		jwtToken = ctx.Input.Query("_jwt")
	}

	if jwtToken == "" {
		cookie, err := ctx.Request.Cookie("_jwt")
		if err != nil {
			jwtToken = ""
		} else {
			jwtToken = cookie.Value
			jwtToken, err = url.QueryUnescape(jwtToken)
			if err != nil {
				beego.Error(err)
			}
		}
	}

	if jwtToken != "" {
		js, err := vanilla.DecodeJWT(jwtToken)

		if err != nil {
			response := vanilla.MakeErrorResponse(500, "jwt:invalid_jwt_token", err.Error())
			ctx.Output.JSON(response, true, false)
			return
		}

		userId, authUserId, err := vanilla.ParseUserIdFromJwtData(js)
		if err != nil {
			response := vanilla.MakeErrorResponse(500, "jwt:invalid_jwt_token", err.Error())
			ctx.Output.JSON(response, true, false)
			return
		}

		bCtx := gBContextFactory.NewContext(go_context.Background(), ctx.Request, userId, jwtToken, js)
		bCtx = go_context.WithValue(bCtx, "user_id", userId)
		bCtx = go_context.WithValue(bCtx, "uid", authUserId)

		{

			spanCtx, _ := vanilla.Tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(ctx.Request.Header))
			uri := ctx.Request.URL.Path
			operationName := fmt.Sprintf("%s %s", ctx.Request.Method, uri)
			span := vanilla.Tracer.StartSpan(operationName, ext.RPCServerOption(spanCtx))
			bCtx = opentracing.ContextWithSpan(bCtx, span)

			bCtx = go_context.WithValue(bCtx, "jwt", jwtToken)
			o := orm.NewOrmWithSpan(span)
			bCtx = go_context.WithValue(bCtx, "orm", o)
		}

		location := ctx.Input.Header("X-VXIAOCHENG-Loc")
		bCtx = go_context.WithValue(bCtx, "user_loc", location)

		ctx.Input.SetData("bContext", bCtx)
		ctx.Input.SetData("span", opentracing.SpanFromContext(bCtx))
	} else {
		response := vanilla.MakeErrorResponse(500, "jwt:invalid_jwt_token", fmt.Sprintf("无效的jwt token 5 - [%s]", jwtToken))
		ctx.Output.JSON(response, true, false)
		return
	}

}
View Source
var ModifyRestMethodFilter = func(ctx *context.Context) {
	if ctx.Input.Query("_method") != "" && ctx.Input.IsPost() {
		ctx.Request.Method = strings.ToUpper(ctx.Input.Query("_method"))
	}
}
View Source
var RequestHeaderDetectFilter = func(ctx *context.Context) {
	reqMode := ctx.Input.Header(vanilla.REQUEST_HEADER_FORMAT)
	if reqMode != "" {
		v := ctx.Input.GetData("bContext")
		var bCtx go_context.Context
		if v == nil {
			bCtx = go_context.Background()
		} else {
			bCtx = v.(go_context.Context)
		}

		bCtx = go_context.WithValue(bCtx, "REQUEST_MODE", reqMode)
		ctx.Input.SetData("bContext", bCtx)
		beego.Info(fmt.Sprintf("set request mod: %s", reqMode))
	}
}
View Source
var SALT string = "030e2cf548cf9da683e340371d1a74ee"
View Source
var SKIP_JWT_CHECK_URLS []string = make([]string, 0)

Functions

func SetBusinessContextFactory

func SetBusinessContextFactory(factory vanilla.IBusinessContextFactory)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL