|
|
@ -4,7 +4,6 @@ package store |
|
|
import ( |
|
|
import ( |
|
|
"crypto/hmac" |
|
|
"crypto/hmac" |
|
|
"crypto/sha512" |
|
|
"crypto/sha512" |
|
|
"encoding/base64" |
|
|
|
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"path/filepath" |
|
|
"path/filepath" |
|
|
"time" |
|
|
"time" |
|
|
@ -135,34 +134,30 @@ func (s *Store) GetEntryByIDRaw(id string) ([]byte, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// CreateEntry creates a new record and returns his short id
|
|
|
// CreateEntry creates a new record and returns his short id
|
|
|
func (s *Store) CreateEntry(entry Entry, givenID string) (string, string, error) { |
|
|
func (s *Store) CreateEntry(entry Entry, givenID string) (string, []byte, error) { |
|
|
if !govalidator.IsURL(entry.Public.URL) { |
|
|
if !govalidator.IsURL(entry.Public.URL) { |
|
|
return "", "", ErrNoValidURL |
|
|
return "", nil, ErrNoValidURL |
|
|
} |
|
|
} |
|
|
// try it 10 times to make a short URL
|
|
|
// try it 10 times to make a short URL
|
|
|
for i := 1; i <= 10; i++ { |
|
|
for i := 1; i <= 10; i++ { |
|
|
id, delID, err := s.createEntry(entry, givenID) |
|
|
id, delID, err := s.createEntry(entry, givenID) |
|
|
if err != nil && givenID != "" { |
|
|
if err != nil && givenID != "" { |
|
|
return "", "", err |
|
|
return "", nil, err |
|
|
} else if err != nil { |
|
|
} else if err != nil { |
|
|
logrus.Debugf("Could not create entry: %v", err) |
|
|
logrus.Debugf("Could not create entry: %v", err) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
return id, delID, nil |
|
|
return id, delID, nil |
|
|
} |
|
|
} |
|
|
return "", "", ErrGeneratingIDFailed |
|
|
return "", nil, ErrGeneratingIDFailed |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// DeleteEntry deletes an Entry fully from the DB
|
|
|
// DeleteEntry deletes an Entry fully from the DB
|
|
|
func (s *Store) DeleteEntry(id, hash string) error { |
|
|
func (s *Store) DeleteEntry(id string, givenHmac []byte) error { |
|
|
mac := hmac.New(sha512.New, util.GetPrivateKey()) |
|
|
mac := hmac.New(sha512.New, util.GetPrivateKey()) |
|
|
if _, err := mac.Write([]byte(id)); err != nil { |
|
|
if _, err := mac.Write([]byte(id)); err != nil { |
|
|
return errors.Wrap(err, "could not write hmac") |
|
|
return errors.Wrap(err, "could not write hmac") |
|
|
} |
|
|
} |
|
|
givenHmac, err := base64.RawURLEncoding.DecodeString(hash) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return errors.Wrap(err, "could not decode base64") |
|
|
|
|
|
} |
|
|
|
|
|
if !hmac.Equal(mac.Sum(nil), givenHmac) { |
|
|
if !hmac.Equal(mac.Sum(nil), givenHmac) { |
|
|
return errors.New("hmac verification failed") |
|
|
return errors.New("hmac verification failed") |
|
|
} |
|
|
} |
|
|
|