From e946e1da145e3d60c76512cd9982f49c8882744d Mon Sep 17 00:00:00 2001 From: Bochun Zhang Date: Tue, 30 Aug 2016 22:46:22 -0700 Subject: [PATCH] Add expiry to token in preparation for cache. --- go/src/oauth2client/oauth2client.go | 14 ++++++++++++-- go/src/oauth2client/token.go | 14 +++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/go/src/oauth2client/oauth2client.go b/go/src/oauth2client/oauth2client.go index 3f6a9e66..5132f4c4 100644 --- a/go/src/oauth2client/oauth2client.go +++ b/go/src/oauth2client/oauth2client.go @@ -75,9 +75,19 @@ func retrieveAccessToken(url string, params url.Values) (*Token, error) { if err != nil { return nil, err } + var token *Token - err := json.Unmarshal(body, &token) - return token, err + + if err := json.Unmarshal(body, &token); err != nil { + return nil, err + } + + if token.ExpiresIn != nil { + expiry := int64(time.Now().Unix()) + int64(*token.ExpiresIn) + token.Expiry = time.Unix(expiry, 0) + token.ExpiresIn = nil + } + return token, nil } // Run 3LO verification. Sends a request to auth_uri with a verification code. diff --git a/go/src/oauth2client/token.go b/go/src/oauth2client/token.go index fb03ba4d..17ef287c 100644 --- a/go/src/oauth2client/token.go +++ b/go/src/oauth2client/token.go @@ -1,5 +1,9 @@ package oauth2client +import ( + "time" +) + // Definition for OAuth2 token type. // Referenced from https://godoc.org/golang.org/x/oauth2#Token type Token struct { @@ -16,6 +20,14 @@ type Token struct { // if it expires. RefreshToken string `json:"refresh_token,omitempty"` + // Expiry is the optional expiration time of the access token. + // + // If zero, TokenSource implementations will reuse the same + // token forever and RefreshToken or equivalent + // mechanisms for that TokenSource will not be used. + Expiry time.Time `json:"expiry,omitempty"` + // contains filtered or unexported fields + // ExpiresIn is the optional expiration time in seconds. - ExpiresIn int `json:"expires_in,omitempty"` + ExpiresIn *int `json:"expires_in,omitempty"` }