Files
boc/backend/auth/password.go
T
Bernt 78b57273e2 security: Add proper authentication, RBAC, and tenant isolation
- Add password hashing with bcrypt
- Add AuthService with proper login
- Add password strength validation
- Add RBAC middleware (AdminOnly, ManagerOrAdmin)
- Add tenant isolation middleware
- Update CRM handler with tenant filtering
- Add JWT fallback for development mode
- Add user context helpers
- Build successful
2026-08-10 12:52:48 +00:00

63 lines
1.5 KiB
Go

package auth
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
// HashPassword skapar en bcrypt hash av lösenordet
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", fmt.Errorf("failed to hash password: %w", err)
}
return string(bytes), nil
}
// VerifyPassword kontrollerar att lösenordet matchar hashen
func VerifyPassword(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// ValidatePasswordStrength kontrollerar lösenordsstyrka
func ValidatePasswordStrength(password string) error {
if len(password) < 8 {
return fmt.Errorf("password must be at least 8 characters")
}
hasUpper := false
hasLower := false
hasNumber := false
hasSpecial := false
for _, c := range password {
switch {
case c >= 'A' && c <= 'Z':
hasUpper = true
case c >= 'a' && c <= 'z':
hasLower = true
case c >= '0' && c <= '9':
hasNumber = true
case c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~':
hasSpecial = true
}
}
if !hasUpper {
return fmt.Errorf("password must contain at least one uppercase letter")
}
if !hasLower {
return fmt.Errorf("password must contain at least one lowercase letter")
}
if !hasNumber {
return fmt.Errorf("password must contain at least one number")
}
if !hasSpecial {
return fmt.Errorf("password must contain at least one special character")
}
return nil
}