How to Account for Device Clock Drift in okta-mobile-kotlin and okta-mobile-swift
Last Updated:
Overview
There are two time-related claims in the OpenID Connect id_token that the okta-mobile-* SDKs check when verifying tokens. The 'iat' claim is the Epoch time when the Okta Authorization Server minted the token. The 'exp' claim is the Epoch time when the id_token will no longer be valid. If either claim does not fall within an expected range, the SDKs will fail to verify the id_token.
When this happens, the below errors may be observed in the application logs,
- The current time MUST be before the time represented by the exp Claim.
- Issued at time is not within the allowed threshold of now.
- This token was issued at a time that exceeds the allowed grace jwt_interval.
- The token has expired.
Applies To
- okta-mobile-kotlin (okta-idx-android)
- okta-mobile-swift (okta-idx-swift)
- OAuth2/OpenID Connect Applications
- Token Validation
Cause
Mobile devices tend to have the system clock set incorrectly more often than other systems. There can be several reasons for this, such as the user purposely changing the time. This time, sync issues can cause token validation failures.
Solution
The recommended approach is to ensure the mobile device's time is set correctly by relying on a provider's time sync source.
For some applications where time drift is not consequential to the application or security, it may be desirable for these devices to log in successfully. For these scenarios, the okta-mobile-* SDKs provide options to adjust the grace period.
For okta-mobile-swift, the default grace period (5 minutes) can be modified with the issuedAtGraceInterval property.
For okta-mobile-kotlin the default grace period (5 minutes) can be modified by listening for the ValidateIdTokenEvent and modifying the setting. General information for setting EventHandlers can be found here.
Below is a sample of setting the grace period to 60 minutes:
AuthFoundationDefaults.eventCoordinator = EventCoordinator(
object : EventHandler {
override fun onEvent(event: Any) {
when (event) {
is ValidateIdTokenEvent -> {
event.issuedAtGracePeriodInSeconds = 3600 // 1 hour in seconds
}
}
}
}
)
