Repository: ktorio/ktor
File: ktor-server/ktor-server-plugins/ktor-server-auth-jwt/jvm/test/io/ktor/server/auth/jwt/JWTAuthTest.kt (Kotlin)

Code before:

    @Test
    fun `KTOR-9352 makeAlgorithm with EC key type and null algorithm`() {
        val ecKeyPair = KeyPairGenerator.getInstance("EC").apply {
            initialize(256, SecureRandom())
        }.generateKeyPair()
        val ecAlgorithm = Algorithm.ECDSA256(ecKeyPair.public as ECPublicKey, ecKeyPair.private as ECPrivateKey)

        val token = JWT.create()
            .withAudience(audience)
            .withIssuer(issuer)
            .withKeyId(kid)
            .sign(ecAlgorithm)

        val jwk = mockk<Jwk> {
            every { algorithm } returns null
            every { type } returns "EC"
            every { publicKey } returns ecKeyPair.public
        }
        val provider = mockk<JwkProvider> {
            every { this@mockk.get(kid) } returns jwk
        }

        val decodedKid = JWT.decode(token).keyId
        val resolvedJwk = provider.get(decodedKid)
        val resolvedAlgorithm = resolvedJwk.makeAlgorithm()
        val verifier = JWT.require(resolvedAlgorithm).withIssuer(issuer).build()
        verifier.verify(token)
    }

    @Test
    fun testVerifyNotProvided() = testApplication {
        configureServer {
            jwt {}
        }
        assertFailsWith<IllegalArgumentException> {
            startApplication()
        }
    }

    @Test
    fun authHeaderFromCookie(): Unit = testApplication {
        configureServer {
            jwt {
                this@jwt.realm = this@JWTAuthTest.realm
                authHeader { call ->
                    call.request.cookies["JWT"]?.let { parseAuthorizationHeader(it) }
                }
                verifier(issuer, audience, algorithm)
                validate { jwt ->
                    JWTPrincipal(jwt.payload)

Review comment (refactor):
Lines 399-400 decode the token to get the kid and then resolve the JWK, but since the test already uses the predefined `kid` constant when creating the token (line 387), these lines are redundant. The mock provider is already configured to return the jwk for that specific kid.

You can simplify this to directly use:
```kotlin
val resolvedAlgorithm = jwk.makeAlgorithm()
```
This would make the test more straightforward and consistent with the existing `verifyWithMock` and `verifyNullAlgorithmWithMock` tests which also use mocks directly.
```suggestion
        val resolvedAlgorithm = jwk.makeAlgorithm()
```

Code after:

    @Test
    fun `KTOR-9352 makeAlgorithm with EC key type and null algorithm`() {
        val ecKeyPair = KeyPairGenerator.getInstance("EC").apply {
            initialize(256, SecureRandom())
        }.generateKeyPair()
        val ecAlgorithm = Algorithm.ECDSA256(ecKeyPair.public as ECPublicKey, ecKeyPair.private as ECPrivateKey)

        val token = JWT.create()
            .withAudience(audience)
            .withIssuer(issuer)
            .withKeyId(kid)
            .sign(ecAlgorithm)

        val jwk = mockk<Jwk> {
            every { algorithm } returns null
            every { type } returns "EC"
            every { publicKey } returns ecKeyPair.public
        }
        val provider = mockk<JwkProvider> {
            every { this@mockk.get(kid) } returns jwk
        }

        val resolvedAlgorithm = jwk.makeAlgorithm()
        val verifier = JWT.require(resolvedAlgorithm).withIssuer(issuer).build()
        verifier.verify(token)
    }

    @Test
    fun `KTOR-9352 makeAlgorithm with EC P-384 key and null algorithm`() {
        val ecKeyPair = KeyPairGenerator.getInstance("EC").apply {
            initialize(java.security.spec.ECGenParameterSpec("secp384r1"))
        }.generateKeyPair()
        val ecAlgorithm = Algorithm.ECDSA384(ecKeyPair.public as ECPublicKey, ecKeyPair.private as ECPrivateKey)

        val token = JWT.create()
            .withAudience(audience)
            .withIssuer(issuer)
            .withKeyId(kid)
            .sign(ecAlgorithm)

        val jwk = mockk<Jwk> {
            every { algorithm } returns null
            every { type } returns "EC"
            every { publicKey } returns ecKeyPair.public
        }

        val resolvedAlgorithm = jwk.makeAlgorithm()
        val verifier = JWT.require(resolvedAlgorithm).withIssuer(issuer).build()
        verifier.verify(token)
    }