2017-08-03 12:13:53 +00:00
|
|
|
package main
|
|
|
|
|
2020-01-24 15:33:14 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-06-14 13:20:14 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2020-01-24 15:33:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type memStorageSecret struct {
|
|
|
|
Expiry time.Time
|
|
|
|
Secret string
|
|
|
|
}
|
2017-08-03 12:13:53 +00:00
|
|
|
|
|
|
|
type storageMem struct {
|
2020-01-24 15:33:14 +00:00
|
|
|
store map[string]memStorageSecret
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStorageMem() storage {
|
|
|
|
return &storageMem{
|
2020-01-24 15:33:14 +00:00
|
|
|
store: make(map[string]memStorageSecret),
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
func (s storageMem) Create(secret string, expireIn time.Duration) (string, error) {
|
2023-06-10 18:36:11 +00:00
|
|
|
var (
|
|
|
|
expire time.Time
|
|
|
|
id = uuid.Must(uuid.NewV4()).String()
|
|
|
|
)
|
|
|
|
|
|
|
|
if expireIn > 0 {
|
|
|
|
expire = time.Now().Add(expireIn)
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:33:14 +00:00
|
|
|
s.store[id] = memStorageSecret{
|
2023-06-10 18:36:11 +00:00
|
|
|
Expiry: expire,
|
2020-01-24 15:33:14 +00:00
|
|
|
Secret: secret,
|
|
|
|
}
|
2017-08-03 12:13:53 +00:00
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storageMem) ReadAndDestroy(id string) (string, error) {
|
|
|
|
secret, ok := s.store[id]
|
|
|
|
if !ok {
|
|
|
|
return "", errSecretNotFound
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:33:14 +00:00
|
|
|
defer delete(s.store, id)
|
|
|
|
|
2020-01-26 15:53:34 +00:00
|
|
|
if !secret.Expiry.IsZero() && secret.Expiry.Before(time.Now()) {
|
2020-01-24 15:33:14 +00:00
|
|
|
return "", errSecretNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return secret.Secret, nil
|
|
|
|
}
|