Skip to content

Commit

Permalink
Factor out JWT duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Aug 29, 2024
1 parent 2c3dab1 commit 7eefa9f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 53 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<kotlin.compiler.languageVersion>2.0</kotlin.compiler.languageVersion>
<kotlin.compiler.apiVersion>2.0</kotlin.compiler.apiVersion>
<kotlin.compiler.apiVersion>${kotlin.compiler.languageVersion}</kotlin.compiler.apiVersion>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<kotlin.lib.version>2.0.10</kotlin.lib.version>
<kotlin.lib.version>2.0.20</kotlin.lib.version>
<java.version>8</java.version>
</properties>

Expand Down Expand Up @@ -117,7 +117,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.4.0</version>
<version>3.5.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -153,7 +153,7 @@
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.8.0</version>
<version>3.10.0</version>
<executions>
<execution>
<id>dokka-jar</id>
Expand Down
91 changes: 42 additions & 49 deletions src/test/kotlin/com/vonage/client/kt/VideoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import java.net.URI
import java.time.Duration
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.time.temporal.Temporal
import java.time.temporal.TemporalUnit
import java.util.*
import kotlin.test.*

Expand Down Expand Up @@ -112,6 +110,48 @@ class VideoTest : AbstractTest() {
assertEquals(UUID.fromString(streamId), response.streamId)
}

private fun assertEqualsJwt(encoded: String, role: Role = Role.PUBLISHER,
ttl: Duration = Duration.ofHours(24), assertions: Map<String, String>? = null) {
val decoded = JWT.decode(encoded)
assertNotNull(decoded)
assertNotNull(decoded.id)
assertNotNull(decoded.signature)
assertNotNull(decoded.issuedAt)
val iat = decoded.issuedAtAsInstant
assertTrue(Instant.now().isBefore(iat.plusSeconds(15)))
val expectedExpires = iat.plus(ttl).truncatedTo(ChronoUnit.SECONDS)
assertEquals(expectedExpires, decoded.expiresAtAsInstant?.truncatedTo(ChronoUnit.SECONDS))
val claims = decoded.claims
assertNotNull(claims)
assertEquals(applicationId, claims["application_id"]?.asString())
assertEquals("session.connect", claims["scope"]?.asString())
assertEquals(sessionId, claims["session_id"]?.asString())
assertEquals(role.name.lowercase(), claims["role"]?.asString())
assertions?.forEach { (key, value) -> assertEquals(value, claims[key]?.asString()) }
}

@Test
fun `generate token default parameters`() {
assertEqualsJwt(existingSession.generateToken())
}

@Test
fun `generate token all parameters`() {
val data = userName
val role = Role.SUBSCRIBER
val ttl = Duration.ofHours(8)

val encoded = existingSession.generateToken {
data(data); role(role); expiryLength(ttl)
initialLayoutClassList(layoutClasses)
}

assertEqualsJwt(encoded, role, ttl, mapOf(
"connection_data" to data,
"initial_layout_class_list" to layoutClasses.joinToString(separator = " ")
))
}

@Test
fun `start audio connector all fields`() {
mockPost(expectedUrl = "$baseUrl/connect", expectedRequestParams = mapOf(
Expand Down Expand Up @@ -473,51 +513,4 @@ class VideoTest : AbstractTest() {
sessionId(sessionId); token(token); name(renderName)
})
}

@Test
fun `generate token all parameters`() {
val data = userName
val role = Role.SUBSCRIBER
val ttl = Duration.ofHours(8)
val encoded = existingSession.generateToken {
data(data); role(role); expiryLength(ttl)
initialLayoutClassList(layoutClasses)
}
val decoded = JWT.decode(encoded)
assertNotNull(decoded)
assertNotNull(decoded.id)
assertNotNull(decoded.signature)
val iat = decoded.issuedAtAsInstant
assertTrue(Instant.now().isBefore(iat.plusSeconds(15)))
assertEquals(
iat.plus(ttl).truncatedTo(ChronoUnit.SECONDS),
decoded.expiresAtAsInstant.truncatedTo(ChronoUnit.SECONDS)
)
val claims = decoded.claims
assertNotNull(claims)
assertEquals(applicationId, claims["application_id"]?.asString())
assertEquals("session.connect", claims["scope"]?.asString())
assertEquals(sessionId, claims["session_id"]?.asString())
assertEquals(data, claims["connection_data"]?.asString())
assertEquals(role.name.lowercase(), claims["role"]?.asString())
assertEquals(layoutClasses.joinToString(separator = " "), claims["initial_layout_class_list"]?.asString())
}

@Test
fun `generate token default parameters`() {
val encoded = existingSession.generateToken()
val decoded = JWT.decode(encoded)
assertNotNull(decoded)
assertNotNull(decoded.id)
assertNotNull(decoded.signature)
assertNotNull(decoded.issuedAt)
val expectedExpires = decoded.issuedAtAsInstant.plusSeconds(86400).truncatedTo(ChronoUnit.SECONDS)
assertEquals(expectedExpires, decoded.expiresAtAsInstant?.truncatedTo(ChronoUnit.SECONDS))
val claims = decoded.claims
assertNotNull(claims)
assertEquals(applicationId, claims["application_id"]?.asString())
assertEquals("session.connect", claims["scope"]?.asString())
assertEquals(sessionId, claims["session_id"]?.asString())
assertEquals(Role.PUBLISHER.toString(), claims["role"]?.asString())
}
}

0 comments on commit 7eefa9f

Please sign in to comment.