1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Rework project structure

This commit is contained in:
Lukas Schulte Pelkum
2021-04-15 20:15:42 +02:00
parent de2add44ec
commit 1792ef1c38
20 changed files with 238 additions and 328 deletions

View File

@@ -1,21 +0,0 @@
package pastes
import (
"strconv"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/utils"
)
// generateDeletionToken generates a new deletion token
func generateDeletionToken() (string, error) {
// Read the deletion token length
rawLength := env.Get("DELETION_TOKEN_LENGTH", "12")
length, err := strconv.Atoi(rawLength)
if err != nil {
return "", err
}
// Generate the deletion token
return utils.RandomString(length), nil
}

View File

@@ -1,56 +0,0 @@
package pastes
import (
"time"
"github.com/alexedwards/argon2id"
"github.com/lus/pasty/internal/env"
)
// Paste represents a saved paste
type Paste struct {
ID string `json:"id" bson:"_id"`
Content string `json:"content" bson:"content"`
SuggestedSyntaxType string `json:"suggestedSyntaxType" bson:"suggestedSyntaxType"`
DeletionToken string `json:"deletionToken" bson:"deletionToken,omitempty"`
Created int64 `json:"created" bson:"created"`
AutoDelete bool `json:"autoDelete" bson:"autoDelete"`
}
// Create creates a new paste object using the given content
func Create(id, content string) (*Paste, error) {
// TODO: Generate the suggested syntax type
suggestedSyntaxType := ""
// Generate the deletion token
deletionToken, err := generateDeletionToken()
if err != nil {
return nil, err
}
// Return the paste object
return &Paste{
ID: id,
Content: content,
SuggestedSyntaxType: suggestedSyntaxType,
DeletionToken: deletionToken,
Created: time.Now().Unix(),
AutoDelete: env.Bool("AUTODELETE", false),
}, nil
}
// HashDeletionToken hashes the current deletion token of a paste
func (paste *Paste) HashDeletionToken() error {
hash, err := argon2id.CreateHash(paste.DeletionToken, argon2id.DefaultParams)
if err != nil {
return err
}
paste.DeletionToken = hash
return nil
}
// CheckDeletionToken checks whether or not the given deletion token is correct
func (paste *Paste) CheckDeletionToken(deletionToken string) bool {
match, err := argon2id.ComparePasswordAndHash(deletionToken, paste.DeletionToken)
return err == nil && match
}