Skip to content

Commit 20124de

Browse files
committedOct 20, 2024
x/oauth2: apply the expires_in value returned by the server to Token.ExpiresIn
In typical usage, Token.Expiry alone is sufficient. However, the ExpiresIn field was introduced in golang/go#61417. Even when a server returns an expires_in value in methods like Config.Exchange, only the Expiry field is updated, leaving ExpiresIn unchanged, which can cause confusion. This change ensures that the ExpiresIn field is properly updated when the server provides an expires_in value.
1 parent 3e64809 commit 20124de

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed
 

‎internal/token.go

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ type Token struct {
5050
// mechanisms for that TokenSource will not be used.
5151
Expiry time.Time
5252

53+
// ExpiresIn is the OAuth2 wire format "expires_in" field,
54+
// which specifies how many seconds later the token expires,
55+
// relative to an unknown time base approximately around "now".
56+
ExpiresIn int64
57+
5358
// Raw optionally contains extra metadata from the server
5459
// when updating a token.
5560
Raw interface{}
@@ -295,6 +300,7 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
295300
expires, _ := strconv.Atoi(e)
296301
if expires != 0 {
297302
token.Expiry = time.Now().Add(time.Duration(expires) * time.Second)
303+
token.ExpiresIn = int64(expires)
298304
}
299305
default:
300306
var tj tokenJSON
@@ -312,6 +318,7 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
312318
TokenType: tj.TokenType,
313319
RefreshToken: tj.RefreshToken,
314320
Expiry: tj.expiry(),
321+
ExpiresIn: int64(tj.ExpiresIn),
315322
Raw: make(map[string]interface{}),
316323
}
317324
json.Unmarshal(body, &token.Raw) // no error checks for optional fields

‎token.go

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func tokenFromInternal(t *internal.Token) *Token {
163163
TokenType: t.TokenType,
164164
RefreshToken: t.RefreshToken,
165165
Expiry: t.Expiry,
166+
ExpiresIn: t.ExpiresIn,
166167
raw: t.Raw,
167168
}
168169
}

0 commit comments

Comments
 (0)
Please sign in to comment.