You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: middlewares.go
    path of file: ./repos/meshery/server/handlers
    
    the code of the file until where you have to start completion: // Package handlers :  collection of handlers (aka "HTTP middleware")
package handlers

import (
	"context"
	"fmt"
	"net/http"
	"net/url"

	"github.com/gofrs/uuid"
	"github.com/layer5io/meshery/server/machines"
	mhelpers "github.com/layer5io/meshery/server/machines/helpers"
	"github.com/layer5io/meshery/server/machines/kubernetes"
	"github.com/layer5io/meshery/server/models"
	"github.com/layer5io/meshkit/utils"
	"github.com/layer5io/meshsync/pkg/model"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

const providerQParamName = "provider"

// ProviderMiddleware is a middleware to validate if a provider is set
func (h *Handler) ProviderMiddleware(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, req *http.Request) {
		var providerName string
		var provider models.Provider
		ck, err := req.Cookie(h.config.ProviderCookieName)
		if err == nil {
			providerName = ck.Value
		} else {
			providerName = req.Header.Get(h.config.ProviderCookieName)
	
    
func (h *Handler) AuthMiddleware(next http.Handler, auth models.AuthenticationMechanism) http.Handler {
	fn := func(w http.ResponseWriter, req *http.Request) {
		providerH := h.Provider
		if auth == models.NoAuth && providerH != "" {
			auth = models.ProviderAuth //If a provider is enforced then use provider authentication even in case of NoAuth
		}
		switch auth {
		// case models.NoAuth:
		// 	if providerH != "" {
		// 		w.WriteHeader(http.StatusUnauthorized)
		// 		return
		// 	}
		case models.ProviderAuth:
			providerI := req.Context().Value(models.ProviderCtxKey)
			// logrus.Debugf("models.ProviderCtxKey %s", models.ProviderCtxKey)
			provider, ok := providerI.(models.Provider)
			if !ok {
				http.Redirect(w, req, "/provider", http.StatusFound)
				return
			}
			if providerH != "" && providerH != provider.Name() {
				w.WriteHeader(http.StatusUnauthorized)
				return
			}
			// logrus.Debugf("provider %s", provider)
			isValid := h.validateAuth(provider, req)
			// logrus.Debugf("validate auth: %t", isValid)
			if !isValid {
				// if h.GetProviderType() == models.RemoteProviderType {
				// 	http.Redirect(w, req, "/user/login", http.StatusFound)
				// } else { // Local Provider
				// 	h.LoginHandler(w, req)
				// }
				// return
				if provider.GetProviderType() == models.RemoteProviderType {
					provider.HandleUnAuthenticated(w, req)
					return
				}
				// Local Provider
				h.LoginHandler(w, req, provider, true)
			}
		}
		next.ServeHTTP(w, req)
	}
	return http.HandlerFunc(fn)
}