From 464212c757e00ae1d21d85a6847e541841a94d0d Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 20 Mar 2022 14:32:11 +0100 Subject: [PATCH] [core] Re-check token validity more often than on expiry Signed-off-by: Knut Ahlers --- twitch/twitch.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/twitch/twitch.go b/twitch/twitch.go index 21bc704..2c21fcd 100644 --- a/twitch/twitch.go +++ b/twitch/twitch.go @@ -23,6 +23,8 @@ import ( const ( timeDay = 24 * time.Hour + tokenValidityRecheckInterval = time.Hour + twitchMinCacheTime = time.Second * 30 twitchRequestRetries = 5 @@ -46,10 +48,11 @@ type ( clientID string clientSecret string - accessToken string - refreshToken string - tokenValidity time.Time - tokenUpdateHook func(string, string) error + accessToken string + refreshToken string + tokenValidity time.Time + tokenValidityChecked time.Time + tokenUpdateHook func(string, string) error appAccessToken string @@ -577,12 +580,14 @@ func (c *Client) UpdateToken(accessToken, refreshToken string) { } func (c *Client) ValidateToken(ctx context.Context, force bool) error { - if c.tokenValidity.After(time.Now()) && !force { + if c.tokenValidity.After(time.Now()) && time.Since(c.tokenValidityChecked) < tokenValidityRecheckInterval && !force { // We do have an expiration time and it's not expired // so we can assume we've checked the token and it should // still be valid. - // NOTE(kahlers): In case of a token revokation this - // assumption is invalid and will lead to failing requests + // To detect a token revokation early-ish we re-check the + // token in defined interval. This is not the optimal + // solution as we will get failing requests between revokation + // and recheck but it's better than nothing. return nil } @@ -611,6 +616,7 @@ func (c *Client) ValidateToken(ctx context.Context, force bool) error { } c.tokenValidity = time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second) + c.tokenValidityChecked = time.Now() log.WithField("expiry", c.tokenValidity).Trace("Access token validated") return nil