This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.go
62 lines (57 loc) · 1.65 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
package faker
import "strings"
// Username returns random username from first names and two number
func (f *Faker) Username() string {
return Username(
WithRand(f.cfg.rand),
WithFirstNames(f.cfg.firstNames...),
)
}
// Username returns random username from first names and two number
//
// faker.Username(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))),
// faker.WithFirstNames("Tom")
// )
//
func Username(opts ...Option) string {
return FirstName(opts...) + Numerify("##", opts...)
}
// Password returns random password
func (f *Faker) Password() string {
return Password(
WithRand(f.cfg.rand),
WithPasswordMin(f.cfg.passwordMin),
WithPasswordMax(f.cfg.passwordMax),
WithPasswordChars(f.cfg.passwordChars),
)
}
// Password returns random password
//
// faker.Password(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))), // Rand instance
// faker.WithPasswordMin(8),
// faker.WithPasswordMax(16),
// faker.WithPasswordChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"),
// )
//
func Password(opts ...Option) string {
cfg := newConfig(opts...)
if cfg.passwordMin == 0 {
WithPasswordMin(8)(cfg)
}
if cfg.passwordMax == 0 {
WithPasswordMax(16)(cfg)
}
if len(cfg.passwordChars) == 0 {
WithPasswordChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")(cfg)
}
length := Integer(cfg.passwordMin, cfg.passwordMax, opts...)
var password strings.Builder
password.Grow(length)
for i := 0; i < length; i++ {
char := Integer(0, len(cfg.passwordChars)-1, opts...)
password.WriteByte(cfg.passwordChars[char])
}
return password.String()
}