fix: cfg access

This commit is contained in:
2025-05-25 16:52:10 +02:00
parent 05ee30f6db
commit 8e22a3ac05
3 changed files with 39 additions and 28 deletions

View File

@ -30,6 +30,7 @@ func NewAPIServer(addr string, db *repository.Queries, minio *storage.FileStorag
addr: addr, addr: addr,
repo: db, repo: db,
storage: minio, storage: minio,
cfg: cfg,
} }
} }
@ -44,12 +45,13 @@ func (s *APIServer) Run() error {
oauthHandler := oauth.NewOAuthHandler(s.repo, s.cfg) oauthHandler := oauth.NewOAuthHandler(s.repo, s.cfg)
router.Route("/api/v1", func(r chi.Router) { router.Route("/api/v1", func(r chi.Router) {
r.Use(imiddleware.WithSkipper(imiddleware.AuthMiddleware(s.cfg), "/api/v1/login", "/api/v1/register", "/api/v1/oauth/token")) am := imiddleware.New(s.cfg)
r.Use(imiddleware.WithSkipper(am.Runner, "/api/v1/login", "/api/v1/register", "/api/v1/oauth/token"))
userHandler := user.NewUserHandler(s.repo, s.storage) userHandler := user.NewUserHandler(s.repo, s.storage)
userHandler.RegisterRoutes(r) userHandler.RegisterRoutes(r)
authHandler := auth.NewAuthHandler(s.repo) authHandler := auth.NewAuthHandler(s.repo, s.cfg)
authHandler.RegisterRoutes(r) authHandler.RegisterRoutes(r)
oauthHandler.RegisterRoutes(r) oauthHandler.RegisterRoutes(r)

View File

@ -21,9 +21,10 @@ type AuthHandler struct {
cfg *config.AppConfig cfg *config.AppConfig
} }
func NewAuthHandler(repo *repository.Queries) *AuthHandler { func NewAuthHandler(repo *repository.Queries, cfg *config.AppConfig) *AuthHandler {
return &AuthHandler{ return &AuthHandler{
repo: repo, repo,
cfg,
} }
} }

View File

@ -12,8 +12,17 @@ import (
"gitea.local/admin/hspguard/internal/web" "gitea.local/admin/hspguard/internal/web"
) )
func AuthMiddleware(cfg *config.AppConfig) func(http.Handler) http.Handler { type AuthMiddleware struct {
return func(next http.Handler) http.Handler { cfg *config.AppConfig
}
func New(cfg *config.AppConfig) *AuthMiddleware {
return &AuthMiddleware{
cfg,
}
}
func (m *AuthMiddleware) Runner(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization") authHeader := r.Header.Get("Authorization")
if authHeader == "" { if authHeader == "" {
@ -28,7 +37,7 @@ func AuthMiddleware(cfg *config.AppConfig) func(http.Handler) http.Handler {
} }
tokenStr := parts[1] tokenStr := parts[1]
token, userClaims, err := auth.VerifyToken(tokenStr, cfg.Jwt.PublicKey) token, userClaims, err := auth.VerifyToken(tokenStr, m.cfg.Jwt.PublicKey)
if err != nil || !token.Valid { if err != nil || !token.Valid {
http.Error(w, fmt.Sprintf("invalid token: %v", err), http.StatusUnauthorized) http.Error(w, fmt.Sprintf("invalid token: %v", err), http.StatusUnauthorized)
return return
@ -37,7 +46,6 @@ func AuthMiddleware(cfg *config.AppConfig) func(http.Handler) http.Handler {
ctx := context.WithValue(r.Context(), types.UserIdKey, userClaims.Subject) ctx := context.WithValue(r.Context(), types.UserIdKey, userClaims.Subject)
next.ServeHTTP(w, r.WithContext(ctx)) next.ServeHTTP(w, r.WithContext(ctx))
}) })
}
} }
func WithSkipper(mw func(http.Handler) http.Handler, excludedPaths ...string) func(http.Handler) http.Handler { func WithSkipper(mw func(http.Handler) http.Handler, excludedPaths ...string) func(http.Handler) http.Handler {