-
Notifications
You must be signed in to change notification settings - Fork 64
/
key.go
62 lines (48 loc) · 1.23 KB
/
key.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
package manager
import (
"time"
"github.com/bricks-cloud/bricksllm/internal/key"
"github.com/bricks-cloud/bricksllm/internal/util"
)
type Storage interface {
GetKeysByTag(tag string) ([]*key.ResponseKey, error)
UpdateKey(id string, key *key.UpdateKey) (*key.ResponseKey, error)
CreateKey(key *key.RequestKey) (*key.ResponseKey, error)
DeleteKey(id string) error
}
type Encrypter interface {
Encrypt(secret string) string
}
type Manager struct {
s Storage
e Encrypter
}
func NewManager(s Storage, e Encrypter) *Manager {
return &Manager{
s: s,
e: e,
}
}
func (m *Manager) GetKeysByTag(tag string) ([]*key.ResponseKey, error) {
return m.s.GetKeysByTag(tag)
}
func (m *Manager) CreateKey(rk *key.RequestKey) (*key.ResponseKey, error) {
rk.CreatedAt = time.Now().Unix()
rk.UpdatedAt = time.Now().Unix()
rk.Key = m.e.Encrypt(rk.Key)
rk.KeyId = util.NewUuid()
if err := rk.Validate(); err != nil {
return nil, err
}
return m.s.CreateKey(rk)
}
func (m *Manager) UpdateKey(id string, uk *key.UpdateKey) (*key.ResponseKey, error) {
uk.UpdatedAt = time.Now().Unix()
if err := uk.Validate(); err != nil {
return nil, err
}
return m.s.UpdateKey(id, uk)
}
func (m *Manager) DeleteKey(id string) error {
return m.s.DeleteKey(id)
}