Skip to content

Commit

Permalink
patch: disallow expired daemon tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenSrcerer committed May 3, 2024
1 parent 71bd6ab commit f2cee02
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ class MqttAuthorizationController(
return forbid("<missing/invalid jwt>", authDto.topic!!)

val sub = jwt.getJsonObject("payload").getString("sub")
val exp = jwt.getJsonObject("payload").getString("exp")
val exp = jwt.getJsonObject("payload").getLong("exp")

// Check expiration date
if (exp < Instant.now().epochSecond)
return forbid(authDto.jwt, sub)

// If JWT on the device is expiring in one week, rotate it
if (shouldRotateKey(sub, exp)) {
Expand Down Expand Up @@ -74,9 +78,9 @@ class MqttAuthorizationController(
allow(sub, authDto.topic) else forbid(sub, authDto.topic)
}

private fun shouldRotateKey(sub: String, exp: String): Boolean {
private fun shouldRotateKey(sub: String, exp: Long): Boolean {
return !rotationDeduplicationSet.contains(sub)
&& exp.toLong() <= Instant.now().epochSecond + SECONDS_WEEK
&& exp <= Instant.now().epochSecond + SECONDS_WEEK
}

/*
Expand Down

0 comments on commit f2cee02

Please sign in to comment.