💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 2cbb09bf14783d24e372e9b075… captured on 2023-12-28 at 15:32:53. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Go Back

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 name == "anon" || name == "root" {

59 return errors.New("blacklisted username")

60 }

61 if err := isNameValid(name); err != nil {

62 return err

63 }

64 for _, c := range name {

65 if c > unicode.MaxASCII ||

66 (!unicode.IsLetter(c) && !unicode.IsNumber(c)) {

67 return errors.New("your name contains " +

68 "invalid characters")

69 }

70 }

71 return nil

72 }

73

74 func isGroupNameValid(name string) (error) {

75 if err := isNameValid(name); err != nil {

76 return err

77 }

78 for _, c := range name {

79 if c > unicode.MaxASCII ||

80 (!unicode.IsLetter(c) && !unicode.IsNumber(c) &&

81 c != '-' && c != '_') {

82 return errors.New("the group name " +

83 "contains invalid characters")

84 }

85 }

86 return nil

87 }

88

89 func isRepoNameValid(name string) (error) {

90 if err := isNameValid(name); err != nil {

91 return err

92 }

93 for _, c := range name {

94 if c > unicode.MaxASCII ||

95 (!unicode.IsLetter(c) && !unicode.IsNumber(c) &&

96 c != '-' && c != '_') {

97 return errors.New("the repository name " +

98 "contains invalid characters")

99 }

100 }

101 return nil

102 }

103