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:
19
internal/pastes/id_generation.go
Normal file
19
internal/pastes/id_generation.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user