package auth

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"github.com/gin-gonic/gin"
	"strings"
)

func AUTH() gin.HandlerFunc {
	return func(ctx *gin.Context) {

		token := ctx.Request.Header.Get("Token")
		user, _ := util.ParseToken(token)
		if !strings.Contains(strings.ToLower(user.UserInfo.Username), "kefu") {
			return
		}

		handlerName := strings.Split(ctx.HandlerName(), ".")[1]
		handleAlowed := []string{"GetCoinChains", "GetUserChains", "AddOperationLog"}
		_, handle := util.Contains(handleAlowed, handlerName)

		if !handle {
			handler.SendResponse(ctx, errno.PermissionDenied, nil)
			ctx.Abort()
			return
		}

		ctx.Next()
	}
}
