public TokenRefreshAuditEntry CreateAuditEntry(OAuthAccessToken token)
        {
            var result = new TokenRefreshAuditEntry
            {
                ExpirationTime = token.ExpiresAt
            };

            if (_maintainList)
            {
                _auditEntries.Add(result);
            }

            return(result);
        }
        private async Task <OAuthAccessToken> TryRefreshUntilCancelledOrSuccess(
            TokenRefreshAuditEntry auditItem,
            OAuthAccessToken token,
            CancellationToken cancellationToken,
            Func <OAuthAccessToken, Task <OAuthAccessToken> > refreshToken)
        {
            OAuthAccessToken newToken;

            do
            {
                auditItem.RefreshAttemps++;
                newToken = await TryRefresh(token, refreshToken);

                if (newToken == null)
                {
                    await Task.Delay(_parameters.RetryInterval, cancellationToken);
                }
            } while (newToken == null && !cancellationToken.IsCancellationRequested);

            return(newToken);
        }
        private Task WaitForRefreshTimeAsync(OAuthAccessToken token, CancellationToken cancellationToken, TokenRefreshAuditEntry auditItem)
        {
            var delay = DateTime.UtcNow.GetPercentageTowards(token.ExpiresAt, _parameters.TokenRefreshExpirationPercentage);

            auditItem.PlannedRefreshDelay = delay;

            if (delay == TimeSpan.Zero)
            {
                return(_completedTask);
            }

            return(Task.Delay(delay, cancellationToken));
        }
 public void RefreshEnded(TokenRefreshAuditEntry auditEntry)
 {
     auditEntry.ActualRefreshEnd = DateTime.UtcNow;
 }