This repository was archived by the owner on Jul 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyStore.go
More file actions
183 lines (152 loc) · 4.13 KB
/
Copy pathkeyStore.go
File metadata and controls
183 lines (152 loc) · 4.13 KB
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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/json"
"errors"
"net/http"
"sync"
"github.com/lestrrat-go/jwx/v3/jwk"
"go.uber.org/fx"
"go.uber.org/zap"
)
var (
ErrNoSuchKey = errors.New("no key exists with that KID")
)
// KeyStore represents storage, possibly external, for keys.
type KeyStore interface {
// Store inserts the given key into this storage. Care must be taken
// not to store private keys in unsafe, external locations.
Store(Key) error
// Load retrieves the Key with the given kid. If no such key exists,
// this method returns ErrNoSuckKey.
Load(kid string) (Key, error)
// LoadAll loads all keys known to this storage. Note that this method
// may filter expired keys, depending on the implementation.
LoadAll() ([]Key, error)
// Delete removes a key from this storage. If no such key exists,
// this method returns ErrNoSuchKey.
Delete(kid string) error
}
// InMemoryKeyStore is a KeyStore that uses a simple map guarded
// by a read/write mutex. Instances must be created with NewInMemoryKeyStore.
type InMemoryKeyStore struct {
lock sync.RWMutex
keys map[string]Key
}
func NewInMemoryKeyStore() *InMemoryKeyStore {
return &InMemoryKeyStore{
keys: make(map[string]Key),
}
}
func (s *InMemoryKeyStore) Store(k Key) error {
s.lock.Lock()
s.keys[k.KID] = k
s.lock.Unlock()
return nil
}
func (s *InMemoryKeyStore) Load(kid string) (k Key, err error) {
var exists bool
s.lock.RLock()
k, exists = s.keys[kid]
s.lock.RUnlock()
if !exists {
err = ErrNoSuchKey
}
return
}
func (s *InMemoryKeyStore) LoadAll() (ks []Key, err error) {
s.lock.RLock()
ks = make([]Key, 0, len(s.keys))
for _, k := range s.keys {
ks = append(ks, k)
}
s.lock.RUnlock()
return
}
func (s *InMemoryKeyStore) Delete(kid string) (err error) {
s.lock.Lock()
if _, exists := s.keys[kid]; exists {
delete(s.keys, kid)
} else {
err = ErrNoSuchKey
}
s.lock.Unlock()
return
}
// KeyHandler renders PUBLIC keys over HTTP.
type KeyHandler struct {
logger *zap.Logger
keyAccessor *KeyAccessor
keyStore KeyStore
}
func NewKeyHandler(logger *zap.Logger, keyAccessor *KeyAccessor, keyStore KeyStore) *KeyHandler {
return &KeyHandler{
logger: logger,
keyAccessor: keyAccessor,
keyStore: keyStore,
}
}
func (kh *KeyHandler) writeKey(response http.ResponseWriter, key Key) {
response.Header().Set("Content-Type", "application/jwk+json")
key.WriteTo(response)
}
// ServeHTTP serves up the JWK format of generated keys. If this handler receives a path variable
// named "kid", that is used to lookup the key to render. Otherwise, this handler returns the current
// verification key.
func (kh *KeyHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if kid := request.PathValue("kid"); len(kid) > 0 {
if key, err := kh.keyStore.Load(kid); err == nil {
kh.writeKey(response, key)
} else {
response.WriteHeader(http.StatusNotFound)
}
} else if key, err := kh.keyAccessor.Load(); err == nil {
kh.writeKey(response, key)
} else {
response.WriteHeader(http.StatusServiceUnavailable)
}
}
// KeysHandler serves up the set of all keys in a Keys.
type KeysHandler struct {
logger *zap.Logger
keyStore KeyStore
}
func NewKeysHandler(l *zap.Logger, keyStore KeyStore) *KeysHandler {
return &KeysHandler{
logger: l,
keyStore: keyStore,
}
}
func (kh *KeysHandler) fetchKeySet() (set jwk.Set, err error) {
var keys []Key
keys, err = kh.keyStore.LoadAll()
if err == nil {
set, err = NewPublicSet(keys...)
}
return
}
// ServeHTTP serves up the JWK key set in jwk-set format.
func (kh *KeysHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
var data []byte
set, err := kh.fetchKeySet()
if err == nil {
data, err = json.Marshal(set)
}
if err == nil {
response.Header().Set("Content-Type", "application/jwk-set+json")
response.Write(data)
} else {
response.WriteHeader(http.StatusInternalServerError)
}
}
func ProvideKeyStore() fx.Option {
return fx.Provide(
fx.Annotate(
NewInMemoryKeyStore,
fx.As(new(KeyStore)),
),
NewKeyHandler,
NewKeysHandler,
)
}