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

reimplement v2 API controller

This commit is contained in:
Lukas Schulte Pelkum
2023-06-07 19:51:54 +02:00
parent a8077a54f9
commit 941da057ae
17 changed files with 433 additions and 14 deletions

View File

@@ -0,0 +1,19 @@
package pastes
import (
"context"
"github.com/lus/pasty/internal/randx"
)
func GenerateID(ctx context.Context, repo Repository, charset string, length int) (string, error) {
for {
id := randx.String(charset, length)
existing, err := repo.FindByID(ctx, id)
if err != nil {
return "", err
}
if existing == nil {
return id, nil
}
}
}

View File

@@ -1,9 +1,31 @@
package pastes
import "github.com/alexedwards/argon2id"
type Paste struct {
ID string `json:"id"`
Content string `json:"content"`
ModificationToken string `json:"modificationToken,omitempty"`
Created int64 `json:"created"`
Metadata map[string]interface{} `json:"metadata"`
ID string `json:"id"`
Content string `json:"content"`
ModificationToken string `json:"modificationToken,omitempty"`
Created int64 `json:"created"`
Metadata map[string]any `json:"metadata"`
}
func (paste *Paste) HashModificationToken() error {
if paste.ModificationToken == "" {
return nil
}
hash, err := argon2id.CreateHash(paste.ModificationToken, argon2id.DefaultParams)
if err != nil {
return err
}
paste.ModificationToken = hash
return nil
}
func (paste *Paste) CheckModificationToken(modificationToken string) bool {
if paste.ModificationToken == "" {
return false
}
match, err := argon2id.ComparePasswordAndHash(modificationToken, paste.ModificationToken)
return err == nil && match
}