💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 561f607cda54f2b71aa0719698… captured on 2023-09-08 at 16:27:21. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-04-19)
-=-=-=-=-=-=-
0 package db
1
2 import (
3 "errors"
4 "strconv"
5 "unicode"
6
7 "golang.org/x/crypto/bcrypt"
8 )
9
10 func hashPassword(password string) (string, error) {
11 bytes, err := bcrypt.GenerateFromPassword([]byte(password),
12 bcrypt.DefaultCost)
13 return string(bytes), err
14 }
15
16 func checkPassword(password, hash string) bool {
17 err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
18 return err == nil
19 }
20
21 const (
22 passwordMinLen = 6
23 passwordMaxLen = 32
24 maxNameLen = 24
25 )
26
27 func isPasswordValid(password string) (error) {
28 if len(password) == 0 {
29 return errors.New("empty password")
30 }
31 if len(password) < passwordMinLen {
32 return errors.New("password too short(minimum " +
33 strconv.Itoa(passwordMinLen) +
34 " characters)")
35 }
36 if len(password) > passwordMaxLen {
37 return errors.New("password too long(maximum " +
38 strconv.Itoa(passwordMaxLen) +
39 " characters)")
40 }
41 return nil
42 }
43
44 func isNameValid(name string) error {
45 if len(name) == 0 {
46 return errors.New("empty name")
47 }
48 if len(name) > maxNameLen {
49 return errors.New("name too long")
50 }
51 if !unicode.IsLetter([]rune(name)[0]) {
52 return errors.New("your name must start with a letter")
53 }
54 return nil
55 }
56
57 func isUsernameValid(name string) error {
58 if err := isNameValid(name); err != nil {
59 return err
60 }
61 for _, c := range name {
62 if c > unicode.MaxASCII ||
63 (!unicode.IsLetter(c) && !unicode.IsNumber(c)) {
64 return errors.New("your name contains " +
65 "invalid characters")
66 }
67 }
68 return nil
69 }
70
71 func isGroupNameValid(name string) (error) {
72 if err := isNameValid(name); err != nil {
73 return err
74 }
75 for _, c := range name {
76 if c > unicode.MaxASCII ||
77 (!unicode.IsLetter(c) && !unicode.IsNumber(c) &&
78 c != '-' && c != '_') {
79 return errors.New("the group name " +
80 "contains invalid characters")
81 }
82 }
83 return nil
84 }
85
86 func isRepoNameValid(name string) (error) {
87 if err := isNameValid(name); err != nil {
88 return err
89 }
90 for _, c := range name {
91 if c > unicode.MaxASCII ||
92 (!unicode.IsLetter(c) && !unicode.IsNumber(c) &&
93 c != '-' && c != '_') {
94 return errors.New("the repository name " +
95 "contains invalid characters")
96 }
97 }
98 return nil
99 }
100