2021-04-15 21:15:42 +03:00
package shared
import (
2021-07-22 23:26:21 +03:00
"log"
2021-04-15 21:15:42 +03:00
"github.com/alexedwards/argon2id"
)
// Paste represents a saved paste
type Paste struct {
2021-07-22 23:26:21 +03:00
ID string ` json:"id" bson:"_id" `
Content string ` json:"content" bson:"content" `
DeletionToken string ` json:"deletionToken,omitempty" bson:"deletionToken" ` // Required for legacy paste storage support
ModificationToken string ` json:"modificationToken,omitempty" bson:"modificationToken" `
Created int64 ` json:"created" bson:"created" `
Metadata map [ string ] interface { } ` json:"metadata" bson:"metadata" `
2021-04-15 21:15:42 +03:00
}
2021-07-22 23:26:21 +03:00
// HashModificationToken hashes the current modification token of a paste
func ( paste * Paste ) HashModificationToken ( ) error {
hash , err := argon2id . CreateHash ( paste . ModificationToken , argon2id . DefaultParams )
2021-04-15 21:15:42 +03:00
if err != nil {
return err
}
2021-07-22 23:26:21 +03:00
paste . ModificationToken = hash
2021-04-15 21:15:42 +03:00
return nil
}
2021-07-22 23:26:21 +03:00
// CheckModificationToken checks whether or not the given modification token is correct
func ( paste * Paste ) CheckModificationToken ( modificationToken string ) bool {
// The modification token may be stored in the deletion token field in old pastes
usedToken := paste . ModificationToken
if usedToken == "" {
usedToken = paste . DeletionToken
if usedToken != "" {
log . Println ( "WARNING: You seem to have pastes with the old 'deletionToken' field stored in your storage driver. Though this does not cause any issues right now, it may in the future. Consider some kind of migration." )
}
}
match , err := argon2id . ComparePasswordAndHash ( modificationToken , usedToken )
2021-04-15 21:15:42 +03:00
return err == nil && match
}