-
Notifications
You must be signed in to change notification settings - Fork 868
Description
return exp - now + preemptExpiryTime; |
C#
return exp - now + preemptExpiryTime;
is meant to calculate the remaining time-to-live (TTL) for credentials, but if the intention is to reduce the TTL by a "preempt" period (to refresh credentials before they actually expire), you should subtract preemptExpiryTime, not add it.
Why Adding Is Incorrect
Adding preemptExpiryTime actually increases the TTL, meaning your credentials might be used past their intended expiration window, risking authentication errors.
Example Scenario
Suppose:
exp = 100 (expiration time in seconds since epoch)
now = 90 (current time in seconds since epoch)
preemptExpiryTime = 5 (preemptive buffer in seconds)
Current Code:
C#
return exp - now + preemptExpiryTime; // 100 - 90 + 5 = 15
Interpretation:
The credentials will be considered valid for 15 more seconds.
Intended Behavior (Should Subtract):
C#
return exp - now - preemptExpiryTime; // 100 - 90 - 5 = 5
Interpretation:
The credentials will be considered valid for only 5 more seconds, ensuring they are refreshed 5 seconds before actual expiration.
Why This Matters
By adding preemptExpiryTime, you risk using credentials past their safe window, potentially causing failures in authentication. By subtracting, you ensure credentials are refreshed early, avoiding issues due to clock drift or network delays.
Corrected Code
C#
return exp - now - preemptExpiryTime;
Summary:
You should subtract preemptExpiryTime to get a reduced TTL for early refresh, not add it. Adding increases the TTL and risks late refreshes.