💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › ab256229e54e83ed83c230bc50… captured on 2023-09-08 at 16:28:14. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-05-24)

🚧 View Differences

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

Go Back

0 package db

1

2 import (

3 "crypto/rand"

4 "crypto/sha256"

5 "encoding/base64"

6 "time"

7 "log"

8 "errors"

9 )

10

11 func CanUsePassword(repo string, owner string, username string) (bool, error) {

12 row, err := db.Query(`SELECT securegit FROM user WHERE

13 UPPER(name) LIKE UPPER(?)`, username)

14 if err != nil {

15 log.Println(err)

16 return false, err

17 }

18 defer row.Close()

19 var secure int

20 if (!row.Next()) {

21 return false, errors.New("not found")

22 }

23 row.Scan(&secure)

24 if secure != 0 {

25 return false, nil

26 }

27 row.Close()

28

29 row, err = db.Query(`SELECT b.securegit FROM user a

30 INNER JOIN repo b ON a.userID = b.UserID WHERE

31 UPPER(a.name) LIKE UPPER(?) AND

32 UPPER(b.name) LIKE UPPER(?)`,

33 owner, repo)

34 if err != nil {

35 log.Println(err)

36 return false, err

37 }

38 if (!row.Next()) {

39 return false, errors.New("not found")

40 }

41 defer row.Close()

42 row.Scan(&secure)

43 return secure == 0, nil

44 }

45

46 func (user User) CreateToken() (string, error) {

47 data := make([]byte, 32)

48 if _, err := rand.Read(data); err != nil {

49 return "", err

50 }

51 token := base64.RawStdEncoding.EncodeToString(data)

52 sum := sha256.Sum224(data)

53 hash := base64.RawStdEncoding.EncodeToString(sum[:])

54 _, err := db.Exec(`INSERT INTO token(userID, token, hint, expiration)

55 VALUES(?, ?, ?, ?);`,

56 user.ID, hash, token[0:4],

57 time.Now().Unix() + 3600 * 24 * 30)

58 if err != nil {

59 return "", err

60 }

61

62 return token, nil

63 }

64

65 func (user User) RenewToken(tokenID int) (error) {

66 res, err := db.Exec(`UPDATE token SET expiration = ?

67 WHERE tokenID = ? AND userID = ?`,

68 time.Now().Unix() + 3600 * 24 * 30 + 1,

69 tokenID, user.ID)

70 if err != nil {

71 return err

72 }

73 rows, err := res.RowsAffected()

74 if err != nil {

75 return err

76 }

77 if rows < 1 {

78 return errors.New("invalid token id")

79 }

80 return nil

81 }

82

83 func (user User) DeleteToken(tokenID int) (error) {

84 row, err := db.Exec(`DELETE FROM token WHERE tokenID = ? AND userID = ?`,

85 tokenID, user.ID)

86 if err != nil {

87 return err

88 }

89 count, err := row.RowsAffected()

90 if err != nil {

91 return err

92 }

93 if count < 1 {

94 return errors.New("invalid token")

95 }

96 return nil

97 }

98

99 func (user User) GetTokens() ([]Token, error) {

100 rows, err := db.Query(`SELECT tokenID, expiration, hint FROM token

101 WHERE userID = ?`, user.ID)

102 if err != nil {

103 log.Println(err)

104 return nil, errors.New("unexpected error")

105 }

106 defer rows.Close()

107

108 tokens := []Token{}

109 for rows.Next() {

110 var r Token

111 err = rows.Scan(&r.ID, &r.Expiration, &r.Hint)

112 if err != nil {

113 return nil, err

114 }

115 r.ExpirationFormat = time.Unix(r.Expiration, 0).UTC().

116 Format(time.RFC1123)

117 tokens = append(tokens, r)

118 }

119 return tokens, nil

120 }

121

122 func (user *User) ToggleSecure() error {

123 user.SecureGit = !user.SecureGit

124

125 _, err := db.Exec("UPDATE user SET securegit = ? " +

126 "WHERE userID = ?", user.SecureGit, user.ID)

127 if err != nil {

128 return err

129 }

130

131 users[user.Signature] = *user

132 return nil

133 }

134

135 func TokenAuth(username string, token string) error {

136 decoded, err := base64.RawStdEncoding.DecodeString(token)

137 if err != nil {

138 log.Println(err)

139 return errors.New("unexpected error")

140 }

141 sum := sha256.Sum224(decoded)

142 hash := base64.RawStdEncoding.EncodeToString(sum[:])

143 row, err := db.Query(`SELECT b.expiration FROM user a

144 INNER JOIN token b ON a.userID = b.UserID WHERE

145 UPPER(a.name) LIKE UPPER(?) AND

146 UPPER(b.token) LIKE UPPER(?)`,

147 username, hash)

148 if err != nil {

149 log.Println(err)

150 return errors.New("unexpected error")

151 }

152 defer row.Close()

153 if !row.Next() {

154 return errors.New("invalid token")

155 }

156 var expiration int64

157 row.Scan(&expiration)

158 if expiration <= time.Now().Unix() {

159 return errors.New("token expired")

160 }

161 return nil

162 }

163