Skip to content

Commit

Permalink
return an error when the key ID is not present in the cached key set
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Oct 29, 2021
1 parent 4451a7e commit a192c76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ type FileKeyCacher struct {
}

func (f *FileKeyCacher) Get(keyID string) (*jose.JSONWebKey, error) {
return f.keys[keyID], nil
v, ok := f.keys[keyID]
if !ok {
return nil, fmt.Errorf("key '%s' not found in the key set", keyID)
}
return v, nil
}

func (f *FileKeyCacher) Add(keyID string, _ []jose.JSONWebKey) (*jose.JSONWebKey, error) {
Expand Down
20 changes: 20 additions & 0 deletions jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ func TestNewFileKeyCacher(t *testing.T) {
}
}

func TestNewFileKeyCacher_unknownKey(t *testing.T) {
b, err := ioutil.ReadFile("./fixtures/symmetric.json")
if err != nil {
t.Error(err)
}
kc, err := NewFileKeyCacher(b, "")
if err != nil {
t.Error(err)
}
v, err := kc.Get("unknown")
if err == nil {
t.Error("error expected")
} else if e := err.Error(); e != "key 'unknown' not found in the key set" {
t.Error("unexpected error:", e)
}
if v != nil {
t.Error("nil value expected")
}
}

func jwkEndpoint(name string) http.HandlerFunc {
data, err := ioutil.ReadFile("./fixtures/" + name + ".json")
return func(rw http.ResponseWriter, _ *http.Request) {
Expand Down

0 comments on commit a192c76

Please sign in to comment.