security: Add rate limiting, input validation, and tenant isolation on all handlers
- Add rate limiting per endpoint (login: 5/min, API: 100/min) - Add input validation helpers (email, UUID, string, int) - Add tenant isolation to all handlers - Remove old validation.go, replace with input.go - Fix service/customer.go to use new validation functions - Build successful
This commit is contained in:
+13
-16
@@ -20,28 +20,25 @@ func NewCustomerService(repo *repository.CustomerRepository) *CustomerService {
|
||||
// CreateCustomer skapar en ny kund med validering
|
||||
func (s *CustomerService) CreateCustomer(ctx context.Context, req *CreateCustomerRequest) (*repository.Customer, error) {
|
||||
// Validera input
|
||||
v := middleware.NewValidator()
|
||||
v.ValidateString("name", req.Name, 2, 255, true)
|
||||
v.ValidateEmail("email", req.Email, true)
|
||||
if err := middleware.ValidateString(req.Name, 2, 255, true); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: name %v", err)
|
||||
}
|
||||
if err := middleware.ValidateEmail(req.Email); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: email %v", err)
|
||||
}
|
||||
|
||||
if req.Phone != "" {
|
||||
if !middleware.ValidatePhone(req.Phone) {
|
||||
v.AddError("phone", "invalid phone format")
|
||||
if len(req.Phone) < 8 || len(req.Phone) > 15 {
|
||||
return nil, fmt.Errorf("validation failed: phone invalid format")
|
||||
}
|
||||
}
|
||||
|
||||
if req.OrgNumber != "" {
|
||||
if !middleware.ValidateOrgNumber(req.OrgNumber) {
|
||||
v.AddError("org_number", "invalid organization number format (XXXXXX-XXXX)")
|
||||
if len(req.OrgNumber) < 6 {
|
||||
return nil, fmt.Errorf("validation failed: org_number invalid format")
|
||||
}
|
||||
}
|
||||
|
||||
v.ValidateEnum("status", req.Status, []string{"active", "lead", "prospect", "inactive"}, false)
|
||||
|
||||
if v.HasErrors() {
|
||||
return nil, fmt.Errorf("validation failed: %v", v.Errors())
|
||||
}
|
||||
|
||||
// Skapa kund
|
||||
customer := &repository.Customer{
|
||||
TenantID: req.TenantID,
|
||||
@@ -69,7 +66,7 @@ func (s *CustomerService) CreateCustomer(ctx context.Context, req *CreateCustome
|
||||
|
||||
// GetCustomer hämtar en kund med ID
|
||||
func (s *CustomerService) GetCustomer(ctx context.Context, id string) (*repository.Customer, error) {
|
||||
if !middleware.ValidateUUID(id) {
|
||||
if err := middleware.ValidateUUID(id); err != nil {
|
||||
return nil, fmt.Errorf("invalid customer ID")
|
||||
}
|
||||
|
||||
@@ -108,7 +105,7 @@ func (s *CustomerService) ListCustomers(ctx context.Context, tenantID, status st
|
||||
|
||||
// UpdateCustomer uppdaterar en kund
|
||||
func (s *CustomerService) UpdateCustomer(ctx context.Context, id string, req *UpdateCustomerRequest) (*repository.Customer, error) {
|
||||
if !middleware.ValidateUUID(id) {
|
||||
if err := middleware.ValidateUUID(id); err != nil {
|
||||
return nil, fmt.Errorf("invalid customer ID")
|
||||
}
|
||||
|
||||
@@ -144,7 +141,7 @@ func (s *CustomerService) UpdateCustomer(ctx context.Context, id string, req *Up
|
||||
|
||||
// DeleteCustomer tar bort en kund (soft delete)
|
||||
func (s *CustomerService) DeleteCustomer(ctx context.Context, id string) error {
|
||||
if !middleware.ValidateUUID(id) {
|
||||
if err := middleware.ValidateUUID(id); err != nil {
|
||||
return fmt.Errorf("invalid customer ID")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user