-
Notifications
You must be signed in to change notification settings - Fork 42
/
user_validator.go
140 lines (110 loc) · 4.29 KB
/
user_validator.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright © 2020 Intel Corporation
//
// SPDX-License-Identifier: GPL-3.0-only
package user
import (
"fmt"
"regexp"
"github.com/clearlinux/clr-installer/cmd"
"github.com/clearlinux/clr-installer/utils"
)
// Validator abstracts user validation efforts
type Validator struct {
Login string
UserName string
Password string
}
const (
// MaxUsernameLength is the longest possible username
MaxUsernameLength = 64
// MaxLoginLength is the longest possible login
MaxLoginLength = 31
// MinPasswordLength is the shortest possible password
MinPasswordLength = 8
// MaxPasswordLength is the shortest possible password
MaxPasswordLength = 255
// UsernameCharRequirementMessage is basic username requirements
UsernameCharRequirementMessage = "Username must contain only numbers, letters, commas, - or _"
// UsernameMaxRequirementMessage is the basic username requirements
UsernameMaxRequirementMessage = "UserName maximum length is %d"
// LoginNonEmptyRequirementMessage is basic login requirements
LoginNonEmptyRequirementMessage = "Login is required"
// LoginMaxRequirementMessage is the basic login requirements
LoginMaxRequirementMessage = "Login maximum length is %d"
// LoginRegexRequirementMessage is the basic password requirements
LoginRegexRequirementMessage = "Login must contain only numbers, letters, -, . or _"
// PasswordMinRequirementMessage is the basic password requirements
PasswordMinRequirementMessage = "Password must be at least %d characters long"
// PasswordMaxRequirementMessage is the basic password requirements
PasswordMaxRequirementMessage = "Password may be at most %d characters long"
)
var (
usernameExp = regexp.MustCompile("^([a-zA-Z]+[0-9a-zA-Z-_ ,'.]*|)$")
loginExp = regexp.MustCompile("^[a-zA-Z]+[0-9a-zA-Z-_.]*$")
)
// NewValidator creates/allocates a new user validation
func NewValidator(login string,
username string, password string) *Validator {
return &Validator{
Login: login,
UserName: username,
Password: password,
}
}
// loginEmptyCheck checks if to see if login is non-empty
func (uservalidator *Validator) loginEmptyCheck() error {
if uservalidator.Login == "" {
return fmt.Errorf(utils.Locale.Get(LoginNonEmptyRequirementMessage))
}
return nil
}
// loginMaxLengthCheck checks if login is less than or equal to MaxLoginLength
func (uservalidator *Validator) loginMaxLengthCheck() error {
if len(uservalidator.Login) > MaxLoginLength {
return fmt.Errorf(utils.Locale.Get(LoginMaxRequirementMessage, MaxLoginLength))
}
return nil
}
// loginRegexCheck checks if login meets regular expression loginExp
func (uservalidator *Validator) loginRegexCheck() error {
if !loginExp.MatchString(uservalidator.Login) {
return fmt.Errorf(utils.Locale.Get(LoginRegexRequirementMessage))
}
return nil
}
// usernameRegexCheck checks if username meets regular expression usernameExp
func (uservalidator *Validator) usernameRegexCheck() error {
if !usernameExp.MatchString(uservalidator.UserName) {
return fmt.Errorf(utils.Locale.Get(UsernameCharRequirementMessage))
}
return nil
}
// usernameMaxLengthCheck checks if the username is less than or equal to MaxUsernameLength
func (uservalidator *Validator) usernameMaxLengthCheck() error {
if len(uservalidator.UserName) > MaxUsernameLength {
return fmt.Errorf(utils.Locale.Get(UsernameMaxRequirementMessage, MaxUsernameLength))
}
return nil
}
// passwordMaximumLengthCheck checks if password has a maximum length of MaxPasswordLength
func (uservalidator *Validator) passwordMaximumLengthCheck() error {
if len(uservalidator.Password) > MaxPasswordLength {
return fmt.Errorf(utils.Locale.Get(PasswordMaxRequirementMessage, MaxPasswordLength))
}
return nil
}
// passwordMinimumLengthCheck checks if password has a minimum length of MinPasswordLength
func (uservalidator *Validator) passwordMinimumLengthCheck() error {
if len(uservalidator.Password) < MinPasswordLength {
return fmt.Errorf(utils.Locale.Get(PasswordMinRequirementMessage, MinPasswordLength))
}
return nil
}
// passwordCracklibCheck runs the cracklib-check executable piping password to stdin of cmd
// and writing stdoutput to byte buffer
func (uservalidator *Validator) passwordCracklibCheck() error {
if status, errstring := cmd.CracklibCheck(uservalidator.Password, "Password"); !status {
return fmt.Errorf(errstring)
}
return nil
}