Skip to content

Commit 3e8b2be

Browse files
committed
internal: tolerate malformed expires_in values more
Fixes #239 Change-Id: Id3fdfbfb64bc1a12ab0e952e83ae444b50de1bb5 Reviewed-on: https://go-review.googlesource.com/c/161964 Reviewed-by: Ross Light <light@google.com> Run-TryBot: Ross Light <light@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 5f6b76b commit 3e8b2be

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

internal/token.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func (e *tokenJSON) expiry() (t time.Time) {
7878
type expirationTime int32
7979

8080
func (e *expirationTime) UnmarshalJSON(b []byte) error {
81+
if len(b) == 0 || string(b) == "null" {
82+
return nil
83+
}
8184
var n json.Number
8285
err := json.Unmarshal(b, &n)
8386
if err != nil {
@@ -257,7 +260,7 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
257260
Raw: vals,
258261
}
259262
e := vals.Get("expires_in")
260-
if e == "" {
263+
if e == "" || e == "null" {
261264
// TODO(jbd): Facebook's OAuth2 implementation is broken and
262265
// returns expires_in field in expires. Remove the fallback to expires,
263266
// when Facebook fixes their implementation.

oauth2_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,22 @@ const day = 24 * time.Hour
275275
func TestExchangeRequest_JSONResponse_Expiry(t *testing.T) {
276276
seconds := int32(day.Seconds())
277277
for _, c := range []struct {
278+
name string
278279
expires string
279280
want bool
280281
}{
281-
{fmt.Sprintf(`"expires_in": %d`, seconds), true},
282-
{fmt.Sprintf(`"expires_in": "%d"`, seconds), true}, // PayPal case
283-
{fmt.Sprintf(`"expires": %d`, seconds), true}, // Facebook case
284-
{`"expires": false`, false}, // wrong type
285-
{`"expires": {}`, false}, // wrong type
286-
{`"expires": "zzz"`, false}, // wrong value
282+
{"normal", fmt.Sprintf(`"expires_in": %d`, seconds), true},
283+
{"paypal", fmt.Sprintf(`"expires_in": "%d"`, seconds), true},
284+
{"facebook", fmt.Sprintf(`"expires": %d`, seconds), true},
285+
{"issue_239", fmt.Sprintf(`"expires_in": null, "expires": %d`, seconds), true},
286+
287+
{"wrong_type", `"expires": false`, false},
288+
{"wrong_type2", `"expires": {}`, false},
289+
{"wrong_value", `"expires": "zzz"`, false},
287290
} {
288-
testExchangeRequest_JSONResponse_expiry(t, c.expires, c.want)
291+
t.Run(c.name, func(t *testing.T) {
292+
testExchangeRequest_JSONResponse_expiry(t, c.expires, c.want)
293+
})
289294
}
290295
}
291296

0 commit comments

Comments
 (0)