Skip to content

Commit d68c0e2

Browse files
authored
Merge pull request coreos#148 from chancez/improve_json_unmarshal_err
Check response content-type to improve message if cannot decode as JSON
2 parents c797a55 + 748ba91 commit d68c0e2

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

jwks.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package oidc
22

33
import (
44
"context"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"io/ioutil"
@@ -182,14 +181,16 @@ func (r *remoteKeySet) updateKeys() ([]jose.JSONWebKey, time.Time, error) {
182181

183182
body, err := ioutil.ReadAll(resp.Body)
184183
if err != nil {
185-
return nil, time.Time{}, fmt.Errorf("oidc: read response body: %v", err)
184+
return nil, time.Time{}, fmt.Errorf("unable to read response body: %v", err)
186185
}
186+
187187
if resp.StatusCode != http.StatusOK {
188188
return nil, time.Time{}, fmt.Errorf("oidc: get keys failed: %s %s", resp.Status, body)
189189
}
190190

191191
var keySet jose.JSONWebKeySet
192-
if err := json.Unmarshal(body, &keySet); err != nil {
192+
err = unmarshalResp(resp, body, &keySet)
193+
if err != nil {
193194
return nil, time.Time{}, fmt.Errorf("oidc: failed to decode keys: %v %s", err, body)
194195
}
195196

oidc.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io/ioutil"
10+
"mime"
1011
"net/http"
1112
"strings"
1213
"time"
@@ -93,18 +94,23 @@ func NewProvider(ctx context.Context, issuer string) (*Provider, error) {
9394
if err != nil {
9495
return nil, err
9596
}
97+
defer resp.Body.Close()
98+
9699
body, err := ioutil.ReadAll(resp.Body)
97100
if err != nil {
98-
return nil, err
101+
return nil, fmt.Errorf("unable to read response body: %v", err)
99102
}
103+
100104
if resp.StatusCode != http.StatusOK {
101105
return nil, fmt.Errorf("%s: %s", resp.Status, body)
102106
}
103-
defer resp.Body.Close()
107+
104108
var p providerJSON
105-
if err := json.Unmarshal(body, &p); err != nil {
109+
err = unmarshalResp(resp, body, &p)
110+
if err != nil {
106111
return nil, fmt.Errorf("oidc: failed to decode provider discovery object: %v", err)
107112
}
113+
108114
if p.Issuer != issuer {
109115
return nil, fmt.Errorf("oidc: issuer did not match the issuer returned by provider, expected %q got %q", issuer, p.Issuer)
110116
}
@@ -307,3 +313,16 @@ func (j *jsonTime) UnmarshalJSON(b []byte) error {
307313
*j = jsonTime(time.Unix(unix, 0))
308314
return nil
309315
}
316+
317+
func unmarshalResp(r *http.Response, body []byte, v interface{}) error {
318+
err := json.Unmarshal(body, &v)
319+
if err == nil {
320+
return nil
321+
}
322+
ct := r.Header.Get("Content-Type")
323+
mediaType, _, parseErr := mime.ParseMediaType(ct)
324+
if parseErr == nil && mediaType == "application/json" {
325+
return fmt.Errorf("got Content-Type = application/json, but could not unmarshal as JSON: %v", err)
326+
}
327+
return fmt.Errorf("expected Content-Type = application/json, got %q: %v", ct, err)
328+
}

0 commit comments

Comments
 (0)