forked from koding/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypair.go
217 lines (171 loc) · 4.68 KB
/
keypair.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package kontrol
import (
"errors"
"fmt"
"time"
"github.com/koding/cache"
)
// KeyPair defines a single key pair entity
type KeyPair struct {
// ID is the unique id defining the key pair
ID string
// Public key is used to validate tokens
Public string
// Private key is used to sign/generate tokens
Private string
}
func (k *KeyPair) Validate() error {
if k.ID == "" {
return errors.New("KeyPair ID field is empty")
}
if k.Public == "" {
return errors.New("KeyPair Public field is empty")
}
if k.Private == "" {
return errors.New("KeyPair Private field is empty")
}
return nil
}
// KeyPairStorage is responsible of managing key pairs
type KeyPairStorage interface {
// AddKey adds the given key pair to the storage
AddKey(*KeyPair) error
// DeleteKey deletes the given key pairs from the storage
DeleteKey(*KeyPair) error
// GetKeyFromID retrieves the KeyPair from the given ID
GetKeyFromID(id string) (*KeyPair, error)
// GetKeyFromPublic retrieves the KeyPairs from the given public key.
//
// If the key is no longer valid and the storage is able to deterime
// that it was deleted, the returned error is of *DeletedKeyPairError
// type.
GetKeyFromPublic(publicKey string) (*KeyPair, error)
// Is valid checks if the given publicKey is valid or not. It's up to the
// implementer how to implement it. A valid public key returns a nil error.
//
// If the key is no longer valid and the storage is able to deterime
// that it was deleted, the returned error is of *DeletedKeyPairError
// type.
IsValid(publicKey string) error
}
func NewMemKeyPairStorage() *MemKeyPairStorage {
return &MemKeyPairStorage{
id: cache.NewMemory(),
public: cache.NewMemory(),
}
}
func NewMemKeyPairStorageTTL(ttl time.Duration) *MemKeyPairStorage {
return &MemKeyPairStorage{
id: cache.NewMemoryWithTTL(ttl),
public: cache.NewMemoryWithTTL(ttl),
}
}
type MemKeyPairStorage struct {
id cache.Cache
public cache.Cache
}
func (m *MemKeyPairStorage) AddKey(keyPair *KeyPair) error {
if err := keyPair.Validate(); err != nil {
return err
}
m.id.Set(keyPair.ID, keyPair)
m.public.Set(keyPair.Public, keyPair)
return nil
}
func (m *MemKeyPairStorage) DeleteKey(keyPair *KeyPair) error {
if keyPair.Public == "" {
k, err := m.GetKeyFromID(keyPair.ID)
if err != nil {
return err
}
m.public.Delete(k.Public)
}
m.id.Delete(keyPair.ID)
return nil
}
func (m *MemKeyPairStorage) GetKeyFromID(id string) (*KeyPair, error) {
v, err := m.id.Get(id)
if err != nil {
return nil, err
}
keyPair, ok := v.(*KeyPair)
if !ok {
return nil, fmt.Errorf("MemKeyPairStorage: GetKeyFromID value is malformed %+v", v)
}
return keyPair, nil
}
func (m *MemKeyPairStorage) GetKeyFromPublic(public string) (*KeyPair, error) {
v, err := m.public.Get(public)
if err != nil {
return nil, err
}
keyPair, ok := v.(*KeyPair)
if !ok {
return nil, fmt.Errorf("MemKeyPairStorage: GetKeyFromPublic value is malformed %+v", v)
}
return keyPair, nil
}
func (m *MemKeyPairStorage) IsValid(public string) error {
_, err := m.GetKeyFromPublic(public)
return err
}
// CachedStorage caches the requests that are going to backend and tries to
// lower the load on the backend
type CachedStorage struct {
cache KeyPairStorage
backend KeyPairStorage
}
// NewCachedStorage creates a new CachedStorage
func NewCachedStorage(backend KeyPairStorage, cache KeyPairStorage) *CachedStorage {
return &CachedStorage{
cache: cache,
backend: backend,
}
}
var _ KeyPairStorage = (&CachedStorage{})
func (m *CachedStorage) AddKey(keyPair *KeyPair) error {
if err := m.backend.AddKey(keyPair); err != nil {
return err
}
return m.cache.AddKey(keyPair)
}
func (m *CachedStorage) DeleteKey(keyPair *KeyPair) error {
if err := m.backend.DeleteKey(keyPair); err != nil {
return err
}
return m.cache.DeleteKey(keyPair)
}
func (m *CachedStorage) GetKeyFromID(id string) (*KeyPair, error) {
if keyPair, err := m.cache.GetKeyFromID(id); err == nil {
return keyPair, nil
}
keyPair, err := m.backend.GetKeyFromID(id)
if err != nil {
return nil, err
}
// set key to the cache
if err := m.cache.AddKey(keyPair); err != nil {
return nil, err
}
return keyPair, nil
}
func (m *CachedStorage) GetKeyFromPublic(public string) (*KeyPair, error) {
if keyPair, err := m.cache.GetKeyFromPublic(public); err == nil {
return keyPair, nil
}
keyPair, err := m.backend.GetKeyFromPublic(public)
if err != nil {
return nil, err
}
// set key to the cache
if err := m.cache.AddKey(keyPair); err != nil {
return nil, err
}
return keyPair, nil
}
func (m *CachedStorage) IsValid(public string) error {
if err := m.cache.IsValid(public); err == nil {
return nil
}
return m.backend.IsValid(public)
}