This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
credentials.go
63 lines (55 loc) · 1.43 KB
/
credentials.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
package parser
import (
"errors"
"strings"
"github.com/aquilax/tripcode"
"github.com/bakape/meguca/config"
"github.com/bakape/meguca/common"
)
var (
errNoPostPassword = errors.New("no post password")
errNoSubject = errors.New("no subject")
)
// ParseName parses the name field into a name and tripcode, if any
func ParseName(name string) (string, string, error) {
if name == "" {
return name, name, nil
}
if len(name) > common.MaxLenName {
return "", "", common.ErrNameTooLong
}
name = strings.TrimSpace(name)
// #password for tripcodes and ##password for secure tripcodes
firstHash := strings.IndexByte(name, '#')
if firstHash > -1 {
password := name[firstHash+1:]
name = name[:firstHash]
if password[0] == '#' {
trip := tripcode.SecureTripcode(password[1:], config.Get().Salt)
return name, trip, nil
}
return name, tripcode.Tripcode(password), nil
}
return name, "", nil
}
// ParseSubject verifies and trims a thread subject string
func ParseSubject(s string) (string, error) {
if s == "" {
return s, errNoSubject
}
if len(s) > common.MaxLenSubject {
return s, common.ErrSubjectTooLong
}
return strings.TrimSpace(s), nil
}
// VerifyPostPassword verifies a post password exists does not surpass the
// maximum allowed length
func VerifyPostPassword(s string) error {
if s == "" {
return errNoPostPassword
}
if len(s) > common.MaxLenPostPassword {
return common.ErrPostPasswordTooLong
}
return nil
}