forked from keybase/client
/
checkers.go
126 lines (109 loc) · 2.68 KB
/
checkers.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"fmt"
"regexp"
"strings"
"unicode"
)
var CheckEmail = Checker{
F: func(s string) bool {
re := regexp.MustCompile(`^\S+@\S+\.\S+$`)
return len(s) > 3 && re.MatchString(s)
},
Hint: "must be a valid email address",
}
var CheckUsername = Checker{
F: func(s string) bool {
re := regexp.MustCompile(`^([a-zA-Z0-9][a-zA-Z0-9_]?)+$`)
return len(s) >= 2 && len(s) <= 16 && re.MatchString(s)
},
Hint: "between 2 and 16 characters long",
}
var CheckEmailOrUsername = Checker{
F: func(s string) bool {
return CheckEmail.F(s) || CheckUsername.F(s)
},
Hint: "valid usernames are 2-12 letters long",
}
var CheckPassphraseSimple = Checker{
F: func(s string) bool {
return len(s) > 0
},
Hint: "passphrase cannot be empty",
}
var CheckPassphraseNew = Checker{
F: func(s string) bool {
r := []rune(s)
if len(r) > 0 && unicode.IsSpace(r[0]) {
return false
}
return len(s) >= MinPassphraseLength
},
Hint: fmt.Sprintf("passphrase must be %d or more characters", MinPassphraseLength),
PreserveSpace: true,
}
var CheckInviteCode = Checker{
F: func(s string) bool {
return len(s) > 4
},
Hint: "Invite codes are 4 or more characters",
}
var CheckDeviceName = Checker{
F: func(s string) bool {
re := regexp.MustCompile(`^[a-zA-Z0-9][ _'a-zA-Z0-9+-]*$`)
bad := regexp.MustCompile(` |[ '+_-]$|['+_-][ ]?['+_-]`)
return len(s) >= 3 && len(s) <= 64 && re.MatchString(s) && !bad.MatchString(s)
},
Hint: "between 3 and 64 characters long; use a-Z, 0-9, space, plus, underscore, dash and apostrophe",
}
var CheckKex2SecretPhrase = Checker{
F: func(s string) bool {
if err := validPhrase(s, Kex2PhraseEntropy); err != nil {
G.Log.Debug("invalid kex2 phrase: %s", err)
return false
}
return true
},
Hint: "It looks like there was a typo in the secret phrase. Please try again.",
}
func IsYes(s string) bool {
s = strings.ToLower(strings.TrimSpace(s))
return s == "y" || s == "yes"
}
func IsEmpty(s string) bool {
return len(strings.TrimSpace(s)) == 0
}
func IsNo(s string) bool {
s = strings.ToLower(strings.TrimSpace(s))
return s == "n" || s == "no"
}
var CheckYesNo = Checker{
F: func(s string) bool {
return IsYes(s) || IsNo(s)
},
Hint: "'yes' or 'no'",
}
var CheckNotEmpty = Checker{
F: func(s string) bool {
return len(s) > 0
},
Hint: "cannot be empty",
}
type CheckMember struct {
Set []string
}
func (c CheckMember) Checker() Checker {
return Checker{
F: func(s string) bool {
for _, v := range c.Set {
if v == s {
return true
}
}
return false
},
Hint: strings.Join(c.Set, ", "),
}
}