summaryrefslogtreecommitdiff
path: root/utils/validation.go
blob: e4ef884d9732d14761771c5fe796399ffa723f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package utils

import (
	"fmt"
	"os"
	"unicode"
)

// ValidatePassword checks a password against the policy defined in the PASSWORD_POLICY environment variable.
func ValidatePassword(password string) (bool, string) {
	policy := os.Getenv("PASSWORD_POLICY")
	if policy == "" {
		policy = "none" // Default to no policy if not set
	}

	switch policy {
	case "none":
		return true, ""
	case "standard":
		return validateStandard(password)
	case "enhanced":
		return validateEnhanced(password)
	case "strict":
		return validateStrict(password)
	default:
		// If an unknown policy is set, default to a safe option (standard).
		fmt.Printf("Warning: Unknown PASSWORD_POLICY '%s', falling back to 'standard'\n", policy)
		return validateStandard(password)
	}
}

// validateStandard checks for a minimum length of 6 characters.
func validateStandard(password string) (bool, string) {
	if len(password) < 6 {
		return false, "Password must be at least 6 characters long."
	}
	return true, ""
}

// validateEnhanced checks for a minimum length of 8 characters and complexity.
func validateEnhanced(password string) (bool, string) {
	if len(password) < 8 {
		return false, "Password must be at least 8 characters long."
	}

	var (
		hasUpper  bool
		hasLower  bool
		hasNumber bool
		hasSymbol bool
	)

	for _, char := range password {
		switch {
		case unicode.IsUpper(char):
			hasUpper = true
		case unicode.IsLower(char):
			hasLower = true
		case unicode.IsNumber(char):
			hasNumber = true
		case unicode.IsPunct(char) || unicode.IsSymbol(char):
			hasSymbol = true
		}
	}

	if !hasUpper || !hasLower || !hasNumber || !hasSymbol {
		return false, "Password must contain at least one uppercase letter, one lowercase letter, one number, and one symbol."
	}
	return true, ""
}

// validateStrict checks for a minimum length of 12 characters and complexity.
func validateStrict(password string) (bool, string) {
	if len(password) < 12 {
		return false, "Password must be at least 12 characters long."
	}

	// Using the same complexity requirements as 'enhanced' but with a longer length.
	var (
		hasUpper  bool
		hasLower  bool
		hasNumber bool
		hasSymbol bool
	)

	for _, char := range password {
		switch {
		case unicode.IsUpper(char):
			hasUpper = true
		case unicode.IsLower(char):
			hasLower = true
		case unicode.IsNumber(char):
			hasNumber = true
		case unicode.IsPunct(char) || unicode.IsSymbol(char):
			hasSymbol = true
		}
	}

	if !hasUpper || !hasLower || !hasNumber || !hasSymbol {
		return false, "Password must be at least one uppercase letter, one lowercase letter, one number, and one symbol."
	}
	return true, ""
}