2020-08-22 23:27:37 +03:00
|
|
|
package pastes
|
|
|
|
|
2020-08-23 00:24:38 +03:00
|
|
|
import (
|
|
|
|
"github.com/alexedwards/argon2id"
|
|
|
|
)
|
|
|
|
|
2020-08-22 23:27:37 +03:00
|
|
|
// Paste represents a saved paste
|
|
|
|
type Paste struct {
|
2020-08-24 21:22:53 +03:00
|
|
|
ID string `json:"id" bson:"_id"`
|
|
|
|
Content string `json:"content" bson:"content"`
|
|
|
|
SuggestedSyntaxType string `json:"suggestedSyntaxType" bson:"suggestedSyntaxType"`
|
|
|
|
DeletionToken string `json:"deletionToken" bson:"deletionToken"`
|
2020-08-22 23:27:37 +03:00
|
|
|
}
|
2020-08-23 00:24:38 +03:00
|
|
|
|
|
|
|
// Create creates a new paste object using the given content
|
2020-08-24 21:22:53 +03:00
|
|
|
func Create(id, content string) (*Paste, error) {
|
2020-08-23 00:24:38 +03:00
|
|
|
// 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{
|
2020-08-24 21:22:53 +03:00
|
|
|
ID: id,
|
2020-08-23 00:24:38 +03:00
|
|
|
Content: content,
|
|
|
|
SuggestedSyntaxType: suggestedSyntaxType,
|
|
|
|
DeletionToken: deletionToken,
|
|
|
|
}, 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)
|
2020-08-23 21:02:51 +03:00
|
|
|
return err == nil && match
|
2020-08-23 00:24:38 +03:00
|
|
|
}
|