fix: secure admin endpoints with authentication middleware (issue #4)

- Add config parameter to AuthService constructor
- Implement proper config-based auth checks in middleware
- Wrap all admin endpoints (users, groups, tokens) with authentication
- Apply granular scopes: admin:users:*, admin:groups:*, admin:tokens:*
- Maintain backward compatibility when config is nil

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-21 12:15:38 +03:00
parent 3aff0ab5ef
commit e6d87d025f
4 changed files with 45 additions and 18 deletions

View File

@@ -26,13 +26,15 @@ type AuthContext struct {
type AuthService struct {
db *badger.DB
logger *logrus.Logger
config *types.Config
}
// NewAuthService creates a new authentication service
func NewAuthService(db *badger.DB, logger *logrus.Logger) *AuthService {
func NewAuthService(db *badger.DB, logger *logrus.Logger, config *types.Config) *AuthService {
return &AuthService{
db: db,
logger: logger,
config: config,
}
}

View File

@@ -138,11 +138,12 @@ func (s *RateLimitService) RateLimitMiddleware(next http.HandlerFunc) http.Handl
}
}
// isAuthEnabled checks if authentication is enabled (would be passed from config)
// isAuthEnabled checks if authentication is enabled from config
func (s *AuthService) isAuthEnabled() bool {
// This would normally be injected from config, but for now we'll assume enabled
// TODO: Inject config dependency
return true
if s.config != nil {
return s.config.AuthEnabled
}
return true // Default to enabled if no config
}
// Helper method to check rate limits (simplified version)