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), 14)
12 return string(bytes), err
13 }
14
15 func checkPassword(password, hash string) bool {
16 err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
17 return err == nil
18 }
19
20 const (
21 passwordMinLen = 6
22 passwordMaxLen = 32
23 )
24
25 func isPasswordValid(password string) (error) {
26 if len(password) == 0 {
27 return errors.New("empty password")
28 }
29 if len(password) < passwordMinLen {
30 return errors.New("password too short(minimum " +
31 strconv.Itoa(passwordMinLen) +
32 " characters)")
33 }
34 if len(password) > passwordMaxLen {
35 return errors.New("password too long(maximum " +
36 strconv.Itoa(passwordMaxLen) +
37 " characters)")
38 }
39 return nil
40 }
41
42 func isNameValid(name string) (error) {
43 if len(name) == 0 {
44 return errors.New("empty name")
45 }
46 if !unicode.IsLetter([]rune(name)[0]) {
47 return errors.New("your name must start with a letter")
48 }
49 for _, c := range name {
50 if c > unicode.MaxASCII ||
51 (!unicode.IsLetter(c) && !unicode.IsNumber(c)) {
52 return errors.New("your name contains " +
53 "invalid characters")
54 }
55 }
56 return nil
57 }
58
59 func isGroupNameValid(name string) (error) {
60 if len(name) == 0 {
61 return errors.New("empty name")
62 }
63 if !unicode.IsLetter([]rune(name)[0]) {
64 return errors.New("the group name must start with a letter")
65 }
66 for _, c := range name {
67 if c > unicode.MaxASCII ||
68 (!unicode.IsLetter(c) && !unicode.IsNumber(c) &&
69 c != '-' && c != '_') {
70 return errors.New("the group name " +
71 "contains invalid characters")
72 }
73 }
74 return nil
75 }
76
77 func isRepoNameValid(name string) (error) {
78 if len(name) == 0 {
79 return errors.New("empty name")
80 }
81 if !unicode.IsLetter([]rune(name)[0]) {
82 return errors.New("the repository name " +
83 "must start with a letter")
84 }
85 for _, c := range name {
86 if c > unicode.MaxASCII ||
87 (!unicode.IsLetter(c) && !unicode.IsNumber(c) &&
88 c != '-' && c != '_') {
89 return errors.New("the repository name " +
90 "contains invalid characters")
91 }
92 }
93 return nil
94 }
95