forked from Luzifer/ots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_mem.go
63 lines (49 loc) · 1.04 KB
/
storage_mem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"os"
"strconv"
"time"
"github.com/gofrs/uuid/v3"
)
type memStorageSecret struct {
Expiry time.Time
Secret string
}
type storageMem struct {
store map[string]memStorageSecret
}
func newStorageMem() storage {
return &storageMem{
store: make(map[string]memStorageSecret),
}
}
func (s storageMem) Create(secret string) (string, error) {
id := uuid.Must(uuid.NewV4()).String()
s.store[id] = memStorageSecret{
Expiry: s.expiry(),
Secret: secret,
}
return id, nil
}
func (s storageMem) ReadAndDestroy(id string) (string, error) {
secret, ok := s.store[id]
if !ok {
return "", errSecretNotFound
}
defer delete(s.store, id)
if !secret.Expiry.IsZero() && secret.Expiry.Before(time.Now()) {
return "", errSecretNotFound
}
return secret.Secret, nil
}
func (s storageMem) expiry() time.Time {
exp := os.Getenv("SECRET_EXPIRY")
if exp == "" {
return time.Time{}
}
e, err := strconv.ParseInt(exp, 10, 64)
if err != nil {
return time.Time{}
}
return time.Now().Add(time.Duration(e) * time.Second)
}