-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathauth.go
146 lines (126 loc) · 4 KB
/
auth.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
141
142
143
144
145
146
package gofakeit
import "math/rand"
// Username will generate a random username based upon picking a random lastname and random numbers at the end
func Username() string {
return username(globalFaker.Rand)
}
// Username will generate a random username based upon picking a random lastname and random numbers at the end
func (f *Faker) Username() string {
return username(f.Rand)
}
func username(r *rand.Rand) string {
return getRandValue(r, []string{"person", "last"}) + replaceWithNumbers(r, "####")
}
// Password will generate a random password.
// Minimum number length of 5 if less than.
func Password(lower bool, upper bool, numeric bool, special bool, space bool, num int) string {
return password(globalFaker.Rand, lower, upper, numeric, special, space, num)
}
// Password will generate a random password.
// Minimum number length of 5 if less than.
func (f *Faker) Password(lower bool, upper bool, numeric bool, special bool, space bool, num int) string {
return password(f.Rand, lower, upper, numeric, special, space, num)
}
func password(r *rand.Rand, lower bool, upper bool, numeric bool, special bool, space bool, num int) string {
// Make sure the num minimum is at least 5
if num < 5 {
num = 5
}
i := 0
b := make([]byte, num)
var passString string
if lower {
passString += lowerStr
b[i] = lowerStr[r.Int63()%int64(len(lowerStr))]
i++
}
if upper {
passString += upperStr
b[i] = upperStr[r.Int63()%int64(len(upperStr))]
i++
}
if numeric {
passString += numericStr
b[i] = numericStr[r.Int63()%int64(len(numericStr))]
i++
}
if special {
passString += specialStr
b[i] = specialStr[r.Int63()%int64(len(specialStr))]
i++
}
if space {
passString += spaceStr
b[i] = spaceStr[r.Int63()%int64(len(spaceStr))]
i++
}
// Set default if empty
if passString == "" {
passString = lowerStr + numericStr
}
// Loop through and add it up
for i <= num-1 {
b[i] = passString[r.Int63()%int64(len(passString))]
i++
}
// Shuffle bytes
for i := range b {
j := r.Intn(i + 1)
b[i], b[j] = b[j], b[i]
}
return string(b)
}
func addAuthLookup() {
AddFuncLookup("username", Info{
Display: "Username",
Category: "auth",
Description: "Generates a random username",
Example: "Daniel1364",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return username(r), nil
},
})
AddFuncLookup("password", Info{
Display: "Password",
Category: "auth",
Description: "Generates a random password",
Example: "EEP+wwpk 4lU-eHNXlJZ4n K9%v&TZ9e",
Output: "string",
Params: []Param{
{Field: "lower", Display: "Lower", Type: "bool", Default: "true", Description: "Whether or not to add lower case characters"},
{Field: "upper", Display: "Upper", Type: "bool", Default: "true", Description: "Whether or not to add upper case characters"},
{Field: "numeric", Display: "Numeric", Type: "bool", Default: "true", Description: "Whether or not to add numeric characters"},
{Field: "special", Display: "Special", Type: "bool", Default: "true", Description: "Whether or not to add special characters"},
{Field: "space", Display: "Space", Type: "bool", Default: "false", Description: "Whether or not to add spaces"},
{Field: "length", Display: "Length", Type: "int", Default: "12", Description: "Number of characters in password"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
lower, err := info.GetBool(m, "lower")
if err != nil {
return nil, err
}
upper, err := info.GetBool(m, "upper")
if err != nil {
return nil, err
}
numeric, err := info.GetBool(m, "numeric")
if err != nil {
return nil, err
}
special, err := info.GetBool(m, "special")
if err != nil {
return nil, err
}
space, err := info.GetBool(m, "space")
if err != nil {
return nil, err
}
length, err := info.GetInt(m, "length")
if err != nil {
return nil, err
}
return password(r, lower, upper, numeric, special, space, length), nil
},
})
}