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