Skip to content

Commit

Permalink
Log diff instead of non-existant Date header
Browse files Browse the repository at this point in the history
  • Loading branch information
schnapster committed Jan 15, 2025
1 parent a11a440 commit e13a15d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ internal class InteractionControllerTest : ApplicationTest() {

@Test
internal fun whenPing_thenPong() {
val timestamp = Instant.now().epochSecond.toString()
val headers = HttpHeaders().apply {
add(HEADER_SIGNATURE, "signature")
add(HEADER_TIMESTAMP, "timestamp")
add(HEADER_TIMESTAMP, timestamp)
}
val body = InteractionObject.Ping(42, metadata, 42, "foo", 1)

doReturn(true).`when`(interactionValidator).validateSignature(eq("signature"), eq("timestamp"), any())
doReturn(true).`when`(interactionValidator).validateSignature(eq("signature"), eq(timestamp), any())

val response = this.testRestTemplate.postForEntity("/api/interaction", HttpEntity(body, headers), InteractionResponse.Pong::class.java)

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.warning.mode=all
# Build broken due to warnings from KAPT: https://youtrack.jetbrains.com/issue/KT-68400/K2-w-Kapt-currently-doesnt-support-language-version-2.0.-Falling-back-to-1.9.
#kapt.use.k2=true

shipaVersion=0.7.16
shipaVersion=0.7.17

theJavaVersion=21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.launch
import org.slf4j.LoggerFactory
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
Expand Down Expand Up @@ -94,20 +93,13 @@ internal class InteractionController(
// discord does not state anything about having to check the timestamp recency.
}

val dateHeaderTimestamp: Instant? = try {
val dateMillis = req.getDateHeader(HttpHeaders.DATE)
if (dateMillis > 0) {
Instant.ofEpochMilli(dateMillis)
} else {
null
}
} catch (e: Exception) {
logger().info("Interaction {}: Request header Date is not a valid date: {}", untypedInteraction.id, req.getHeader(HttpHeaders.DATE), e)
null
}
val diff = discordTimestamp?.let { Duration.between(discordTimestamp, requestReceived) }
log.trace(
"Interaction {}: Discord timestamp header: {}, date header timestamp: {}, received time: {}",
untypedInteraction.id, discordTimestamp, dateHeaderTimestamp, requestReceived,
"Interaction {}: Discord timestamp header: {}, received time: {}, diff {}ms",
untypedInteraction.id,
discordTimestamp,
requestReceived,
diff?.toMillis(),
)

val metadata = ShipaMetadata(requestReceived)
Expand Down

0 comments on commit e13a15d

Please sign in to comment.