func (s Server) basicAuthMiddleware(h http.Handler) http.Handler {
	errUnauthorized := errors.New("unauthorized")
	checkAuth := func(r *http.Request) bool {
		username, password, ok := r.BasicAuth()
		if !ok {
			return false
		}
		hashedPassword, ok := s.config.Credentials[username]
		if !ok {
			return false
		}
		return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) == nil
	}
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		log := reqid.FromContextToLogger(ctx, s.log)
		log = log.WithFields(logger.Fields{"ip": r.RemoteAddr})
		if !checkAuth(r) {
			log.Warn("invalid auth request")
			w.Header().Set("WWW-Authenticate", `Basic realm="Budget Manager"`)
			utils.EncodeError(ctx, w, log, errUnauthorized, http.StatusUnauthorized)
			return
		}
		log.Debug("successful auth request")
		h.ServeHTTP(w, r)
	})
}
basicAuthMiddleware checks whether user is authorized
package web
import (
	"errors"
	"fmt"
	"net/http"
	"strings"
	"time"
	"golang.org/x/crypto/bcrypt"
	"github.com/ShoshinNikita/budget-manager/internal/logger"
	"github.com/ShoshinNikita/budget-manager/internal/pkg/reqid"
	"github.com/ShoshinNikita/budget-manager/internal/web/utils"
)