package auth

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/tidbcloud/dataflow-service-ng/internal/cmd/bootstrap/server/wrapper"
	"github.com/tidbcloud/dataflow-service-ng/pkg/log"
)

var consoleAPIAuthSingleton = &consoleAPIAuth{}

func Auth(ctx *gin.Context) {
	authType := ctx.GetHeader(APIAuthTypeKey)
	if authType == string(APIAuthTypeOpenAPI) {
		logger := log.GetGinLogger(ctx)
		logger.Info("`Auth` middleware is only for console API")
		wrapper.NewAPIError(ctx, http.StatusUnauthorized, UnauthenticatedError)
		return
	}
	consoleAPIAuthSingleton.Auth(ctx)
}
