github.com/grafviktor/keep-my-secret@v0.9.10-0.20230908165355-19f35cce90e5/internal/api/web/middleware/middleware.go (about)

     1  package middleware
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/grafviktor/keep-my-secret/internal/api/auth"
     7  	"github.com/grafviktor/keep-my-secret/internal/config"
     8  )
     9  
    10  // TokenVerifier is the interface to describe common logic. Used for dependency injection when testing the application
    11  type TokenVerifier interface {
    12  	VerifyAuthHeader(config config.AppConfig, w http.ResponseWriter, r *http.Request) (string, *auth.Claims, error)
    13  }
    14  
    15  type middleware struct {
    16  	config       config.AppConfig
    17  	authVerifier TokenVerifier
    18  }
    19  
    20  // New creates a new middleware instance
    21  // appConfig is the application configuration
    22  // authVerifier is the auth verifier instance which can be substituted with a mock object
    23  // Returns new middleware instance
    24  func New(appConfig config.AppConfig) middleware {
    25  	return middleware{
    26  		config:       appConfig,
    27  		authVerifier: auth.JWTVerifier{},
    28  	}
    29  }