Checklist
Description
Due to the way the isExpired was implemented, the iat is invalid for the first 0.5 seconds of JWT existence, given this code here:
|
public boolean isExpired(long leeway) { |
|
if (leeway < 0) { |
|
throw new IllegalArgumentException("The leeway must be a positive value."); |
|
} |
|
long todayTime = (long) (Math.floor(new Date().getTime() / 1000) * 1000); //truncate millis |
|
Date futureToday = new Date(todayTime + leeway * 1000); |
|
Date pastToday = new Date(todayTime - leeway * 1000); |
|
boolean expValid = payload.exp == null || !pastToday.after(payload.exp); |
|
boolean iatValid = payload.iat == null || !futureToday.before(payload.iat); |
|
return !expValid || !iatValid; |
|
} |
The line 165 is rounding the current time. If your server is very fast and generate, transmit and reaches your application in less than 0.5s, the futureToday.before(payload.iat) on the line 169 returns false. But the JWT is totally valid.
Solutions:
- Also compare
futureToday == payload.iat.
- Stop truncating the
todayTime.
I reimplemented the validation in Kotlin and added some logs:
fun isExpired(jwt: JWT, leeway: Int): Boolean {
val todayTime = (floor((Date().time / 1000).toDouble()) * 1000).toLong() //truncate millis
val futureToday = Date(todayTime + leeway * 1000)
val pastToday = Date(todayTime - leeway * 1000)
val expValid = jwt.expiresAt == null || !pastToday.after(jwt.expiresAt)
val iatValid = jwt.issuedAt == null || !futureToday.before(jwt.issuedAt)
return !expValid || !iatValid
}
This prints:
todayTime 1740576730000
futureToday 1740576730000
pastToday 1740576730000
expValid true
iatValid false
Reproduction
Probably create the JWT and use it straightway, it will fail on the iat.
Additional context
No response
JWTDecode.Android version
2.0.2
Android version(s)
API 32
Checklist
Description
Due to the way the
isExpiredwas implemented, theiatis invalid for the first 0.5 seconds of JWT existence, given this code here:JWTDecode.Android/lib/src/main/java/com/auth0/android/jwt/JWT.java
Lines 161 to 171 in df3eb30
The line 165 is rounding the current time. If your server is very fast and generate, transmit and reaches your application in less than 0.5s, the
futureToday.before(payload.iat)on the line 169 returns false. But the JWT is totally valid.Solutions:
futureToday == payload.iat.todayTime.I reimplemented the validation in Kotlin and added some logs:
This prints:
Reproduction
Probably create the JWT and use it straightway, it will fail on the
iat.Additional context
No response
JWTDecode.Android version
2.0.2
Android version(s)
API 32