From 778494f9ec3c734f3f2cd15b34cce71aa1d0eeeb Mon Sep 17 00:00:00 2001 From: Mal Curtis Date: Tue, 11 Nov 2014 08:59:36 +1300 Subject: [PATCH] Handle expiry correctly in json response Go treats json numbers as float64 not int. Previously json response expiry information was ignored since it was expected to be an int. --- oauth2.go | 6 +++--- oauth2_test.go | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/oauth2.go b/oauth2.go index 24ddc3a..80e53bd 100644 --- a/oauth2.go +++ b/oauth2.go @@ -323,14 +323,14 @@ func retrieveToken(o *Options, v url.Values) (*Token, error) { token.TokenType, _ = b["token_type"].(string) token.RefreshToken, _ = b["refresh_token"].(string) token.raw = b - e, ok := b["expires_in"].(int) + e, ok := b["expires_in"].(float64) if !ok { // TODO(jbd): Facebook's OAuth2 implementation is broken and // returns expires_in field in expires. Remove the fallback to expires, // when Facebook fixes their implementation. - e, _ = b["expires"].(int) + e, _ = b["expires"].(float64) } - expires = e + expires = int(e) } // Don't overwrite `RefreshToken` with an empty value // if this was a token refreshing request. diff --git a/oauth2_test.go b/oauth2_test.go index 92419c6..9813330 100644 --- a/oauth2_test.go +++ b/oauth2_test.go @@ -115,7 +115,7 @@ func TestExchangeRequest_JSONResponse(t *testing.T) { t.Errorf("Unexpected exchange payload, %v is found.", string(body)) } w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer"}`)) + w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`)) })) defer ts.Close() f := newTestFlow(ts.URL) @@ -124,6 +124,9 @@ func TestExchangeRequest_JSONResponse(t *testing.T) { t.Error(err) } tok := tr.Token() + if tok.Expiry.IsZero() { + t.Errorf("Token expiry should not be zero.") + } if tok.Expired() { t.Errorf("Token shouldn't be expired.") }