app_access.go 1.4 KB
package middleware

import (
	"github.com/beego/beego/v2/server/web/context"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib"
	"net/http"
	"time"
)

func AppAccessFilter() func(ctx *context.Context) {
	authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST)
	authLib.BaseServiceGateway.ConnectTimeout = 200 * time.Millisecond
	authLib.BaseServiceGateway.ReadWriteTimeout = 200 * time.Millisecond
	return func(ctx *context.Context) {
		token := ctx.Request.Header.Get("x-mmm-accesstoken")
		appKey := ctx.Request.Header.Get("x-mmm-appkey")
		if len(appKey) == 0 || len(token) == 0 {
			ctx.Output.SetStatus(http.StatusOK)
			ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidApp), false, false)
			return
		}
		response, err := authLib.AppLogin(authlib.RequestAppLogin{
			AppKey: appKey,
			Token:  token,
		})
		if err != nil {
			ctx.Output.SetStatus(http.StatusOK)
			ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidApp), false, false)
			return
		}
		if !response.AppEnabled {
			ctx.Output.SetStatus(http.StatusOK)
			ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidApp), false, false)
			return
		}
		ctx.Input.SetData("AppKey", appKey)
	}
}