dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

tableGists.go (1141B)


      1 package database
      2 
      3 import (
      4 	"dkforest/pkg/config"
      5 	"dkforest/pkg/utils"
      6 	"time"
      7 
      8 	hutils "dkforest/pkg/web/handlers/utils"
      9 	"github.com/labstack/echo"
     10 	"github.com/sirupsen/logrus"
     11 )
     12 
     13 type Gist struct {
     14 	ID        int64
     15 	UUID      string
     16 	UserID    UserID
     17 	User      User
     18 	Name      string
     19 	Content   string
     20 	Password  string
     21 	CreatedAt time.Time
     22 }
     23 
     24 func (d *DkfDB) GetGistByUUID(uuid string) (out Gist, err error) {
     25 	err = d.db.First(&out, "uuid = ?", uuid).Error
     26 	return
     27 }
     28 
     29 func GetGistPasswordHash(password string) string {
     30 	return utils.Sha512(getGistSaltedPasswordBytes(password))
     31 }
     32 
     33 func getGistSaltedPasswordBytes(password string) []byte {
     34 	return getSaltedPasswordBytes(config.GistPasswordSalt, password)
     35 }
     36 
     37 func (g *Gist) HasAccess(c echo.Context) bool {
     38 	if g.Password == "" {
     39 		return true
     40 	}
     41 	cookie, err := hutils.GetGistCookie(c, g.UUID)
     42 	if err != nil {
     43 		return false
     44 	}
     45 	if cookie.Value != g.Password {
     46 		hutils.DeleteGistCookie(c, g.UUID)
     47 		return false
     48 	}
     49 	return true
     50 }
     51 
     52 // DoSave user in the database, ignore error
     53 func (g *Gist) DoSave(db *DkfDB) {
     54 	if err := db.db.Save(g).Error; err != nil {
     55 		logrus.Error(err)
     56 	}
     57 }