From d9fc566084ae7c315ff65b0b21cc083041287676 Mon Sep 17 00:00:00 2001 From: Logan Gore Date: Mon, 19 Oct 2026 10:24:17 -0700 Subject: [PATCH 1/2] Return Error from consumer authenticateJwt when the API rejects the JWT When local JWT validation failed and the remote /sessions/authenticate call returned an error, consumer authenticateJwt wrapped that error as StytchResult.Success(JWTErrorResponse) or StytchResult.Success(JWTNullResponse). Callers branching on Success vs Error would treat a rejected JWT as authenticated. Pass the StytchResult.Error through unchanged, matching the B2B implementation. JWTErrorResponse and JWTNullResponse are no longer returned and are deprecated. --- .../com/stytch/java/common/JWTResponse.kt | 2 ++ .../kotlin/com/stytch/java/common/Version.kt | 2 +- .../java/consumer/api/sessions/Sessions.kt | 23 +----------- .../consumer/api/sessions/SessionsTest.kt | 36 ++++++++++++++++++- version.gradle.kts | 2 +- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/stytch/src/main/kotlin/com/stytch/java/common/JWTResponse.kt b/stytch/src/main/kotlin/com/stytch/java/common/JWTResponse.kt index eacf060c..0b23084f 100644 --- a/stytch/src/main/kotlin/com/stytch/java/common/JWTResponse.kt +++ b/stytch/src/main/kotlin/com/stytch/java/common/JWTResponse.kt @@ -5,6 +5,7 @@ import com.stytch.java.consumer.models.sessions.Session public sealed interface JWTResponse +@Deprecated("No longer returned; authenticateJwt reports failures as StytchResult.Error.") public object JWTNullResponse : JWTResponse public data class JWTSessionResponse( @@ -15,6 +16,7 @@ public data class JWTAuthResponse( val response: AuthenticateResponse, ) : JWTResponse +@Deprecated("No longer returned; authenticateJwt reports failures as StytchResult.Error.") public data class JWTErrorResponse( val statusCode: Int, val requestId: String?, diff --git a/stytch/src/main/kotlin/com/stytch/java/common/Version.kt b/stytch/src/main/kotlin/com/stytch/java/common/Version.kt index 8976099f..843fe17d 100644 --- a/stytch/src/main/kotlin/com/stytch/java/common/Version.kt +++ b/stytch/src/main/kotlin/com/stytch/java/common/Version.kt @@ -1,3 +1,3 @@ package com.stytch.java.common -internal const val VERSION = "11.3.0" +internal const val VERSION = "11.3.1" diff --git a/stytch/src/main/kotlin/com/stytch/java/consumer/api/sessions/Sessions.kt b/stytch/src/main/kotlin/com/stytch/java/consumer/api/sessions/Sessions.kt index 32af698b..54922d55 100644 --- a/stytch/src/main/kotlin/com/stytch/java/consumer/api/sessions/Sessions.kt +++ b/stytch/src/main/kotlin/com/stytch/java/consumer/api/sessions/Sessions.kt @@ -12,9 +12,7 @@ import com.squareup.moshi.Types import com.stytch.java.common.ConsumerPolicyCache import com.stytch.java.common.InstantAdapter import com.stytch.java.common.JWTAuthResponse -import com.stytch.java.common.JWTErrorResponse import com.stytch.java.common.JWTException -import com.stytch.java.common.JWTNullResponse import com.stytch.java.common.JWTResponse import com.stytch.java.common.JWTSessionResponse import com.stytch.java.common.JwtOptions @@ -587,26 +585,7 @@ internal class SessionsImpl( StytchResult.Success(JWTAuthResponse(netResult.value)) } - is StytchResult.Error -> { - when (val exception = netResult.exception) { - is StytchException.Response -> { - val errorResponse = exception.reason - StytchResult.Success( - JWTErrorResponse( - statusCode = errorResponse.statusCode, - requestId = errorResponse.requestId, - errorType = errorResponse.errorType, - errorMessage = errorResponse.errorMessage, - errorUrl = errorResponse.errorUrl, - ), - ) - } - - else -> { - StytchResult.Success(JWTNullResponse) - } - } - } + is StytchResult.Error -> netResult } } } diff --git a/stytch/src/test/kotlin/com/stytch/java/consumer/api/sessions/SessionsTest.kt b/stytch/src/test/kotlin/com/stytch/java/consumer/api/sessions/SessionsTest.kt index c4f2b06c..25d8e4a2 100644 --- a/stytch/src/test/kotlin/com/stytch/java/consumer/api/sessions/SessionsTest.kt +++ b/stytch/src/test/kotlin/com/stytch/java/consumer/api/sessions/SessionsTest.kt @@ -2,7 +2,9 @@ package com.stytch.java.consumer.api.sessions import com.stytch.java.common.JWTException import com.stytch.java.common.JwtOptions +import com.stytch.java.common.StytchException import com.stytch.java.common.StytchResult +import com.stytch.java.http.HttpClient import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.CoroutineScope @@ -12,12 +14,15 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer import org.jose4j.jwk.HttpsJwks import org.jose4j.jwk.JsonWebKey import org.jose4j.jwk.RsaJwkGenerator import org.jose4j.jws.AlgorithmIdentifiers import org.jose4j.jws.JsonWebSignature import org.jose4j.jwt.JwtClaims +import org.junit.After import org.junit.Before import org.junit.Test import java.time.Instant @@ -30,10 +35,12 @@ internal class SessionsTest { private lateinit var jwtNoClaims: String private lateinit var jwtWithClaims: String private val projectId = "project-test-00000000-0000-0000-0000-000000000000" + private lateinit var server: MockWebServer private val mainThreadSurrogate = newSingleThreadContext("UI thread") @Before fun before() { + server = MockWebServer().apply { start() } Dispatchers.setMain(mainThreadSurrogate) val rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048) val jsonWebKey = JsonWebKey.Factory.newJwk(rsaJsonWebKey.publicKey) @@ -110,7 +117,7 @@ internal class SessionsTest { }.compactSerialization sessions = SessionsImpl( - httpClient = mockk(relaxed = true, relaxUnitFun = true), + httpClient = HttpClient(server.url("").toString().trimEnd('/'), projectId, "secret"), coroutineScope = CoroutineScope(mainThreadSurrogate), jwksClient = jwksClient, jwtOptions = @@ -137,4 +144,31 @@ internal class SessionsTest { val result = sessions.authenticateJwtLocal(jwtWithClaims, 50000) require(result is StytchResult.Success) } + + @After + fun after() { + server.shutdown() + } + + @Test + fun `authenticateJwt returns Error when the API rejects the JWT`() = + runTest { + server.enqueue( + MockResponse().setResponseCode(401).setBody( + """{"status_code":401,"request_id":"req","error_type":"session_not_found","error_message":"nope","error_url":"url"}""", + ), + ) + val result = sessions.authenticateJwt("aaa.bbb.ccc", null) + require(result is StytchResult.Error) + assert(result.exception is StytchException.Response) + } + + @Test + fun `authenticateJwt returns Error when the API error response cannot be parsed`() = + runTest { + server.enqueue(MockResponse().setResponseCode(500).setBody("upstream unavailable")) + val result = sessions.authenticateJwt("aaa.bbb.ccc", null) + require(result is StytchResult.Error) + assert(result.exception is StytchException.Critical) + } } diff --git a/version.gradle.kts b/version.gradle.kts index e238c941..c31bab2c 100644 --- a/version.gradle.kts +++ b/version.gradle.kts @@ -1 +1 @@ -version = "11.3.0" +version = "11.3.1" From 09914e8d9fbe72306eed768a17c1ea597a0e1d99 Mon Sep 17 00:00:00 2001 From: Logan Gore Date: Mon, 19 Oct 2026 10:53:09 -0700 Subject: [PATCH 2/2] Bump version to 11.4.0 --- stytch/src/main/kotlin/com/stytch/java/common/Version.kt | 2 +- version.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stytch/src/main/kotlin/com/stytch/java/common/Version.kt b/stytch/src/main/kotlin/com/stytch/java/common/Version.kt index 843fe17d..aea3d293 100644 --- a/stytch/src/main/kotlin/com/stytch/java/common/Version.kt +++ b/stytch/src/main/kotlin/com/stytch/java/common/Version.kt @@ -1,3 +1,3 @@ package com.stytch.java.common -internal const val VERSION = "11.3.1" +internal const val VERSION = "11.4.0" diff --git a/version.gradle.kts b/version.gradle.kts index c31bab2c..8d04e936 100644 --- a/version.gradle.kts +++ b/version.gradle.kts @@ -1 +1 @@ -version = "11.3.1" +version = "11.4.0"