oauth2: handle token expiry for JWT

Change-Id: I84b4c282ef00e87d13a9b16fdcebd97ea5ed4f4e
Reviewed-on: https://go-review.googlesource.com/1650
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Burcu Dogan 2014-12-16 09:42:17 -08:00
parent 13407478f7
commit 289b5d10a0
2 changed files with 9 additions and 5 deletions

2
jwt.go
View File

@ -131,7 +131,7 @@ func (js jwtSource) Token() (*Token, error) {
token.AccessToken, _ = b["access_token"].(string) token.AccessToken, _ = b["access_token"].(string)
token.TokenType, _ = b["token_type"].(string) token.TokenType, _ = b["token_type"].(string)
token.raw = b token.raw = b
if e, ok := b["expires_in"].(int); ok { if e, ok := b["expires_in"].(float64); ok {
token.Expiry = time.Now().Add(time.Duration(e) * time.Second) token.Expiry = time.Now().Add(time.Duration(e) * time.Second)
} }
if idtoken, ok := b["id_token"].(string); ok { if idtoken, ok := b["id_token"].(string); ok {

View File

@ -44,7 +44,8 @@ func TestJWTFetch_JSONResponse(t *testing.T) {
w.Write([]byte(`{ w.Write([]byte(`{
"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "access_token": "90d64460d14870c08c81352a05dedd3465940a7c",
"scope": "user", "scope": "user",
"token_type": "bearer" "token_type": "bearer",
"expires_in": 3600
}`)) }`))
})) }))
defer ts.Close() defer ts.Close()
@ -59,13 +60,16 @@ func TestJWTFetch_JSONResponse(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if tok.Expired() { if tok.Expired() {
t.Errorf("Token shouldn't be expired.") t.Errorf("Token shouldn't be expired")
} }
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" { if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
t.Errorf("Unexpected access token, %#v.", tok.AccessToken) t.Errorf("Unexpected access token, %#v", tok.AccessToken)
} }
if tok.TokenType != "bearer" { if tok.TokenType != "bearer" {
t.Errorf("Unexpected token type, %#v.", tok.TokenType) t.Errorf("Unexpected token type, %#v", tok.TokenType)
}
if tok.Expiry.IsZero() {
t.Errorf("Unexpected token expiry, %#v", tok.Expiry)
} }
scope := tok.Extra("scope") scope := tok.Extra("scope")
if scope != "user" { if scope != "user" {