-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7194 Deferred session fetching for public endpoints
- Loading branch information
Showing
13 changed files
with
325 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...er/ktor-server-plugins/ktor-server-auth/jvm/test/io/ktor/tests/auth/SessionAuthJvmTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.auth | ||
|
||
import io.ktor.client.request.HttpRequestBuilder | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.header | ||
import io.ktor.client.request.post | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.install | ||
import io.ktor.server.auth.Authentication | ||
import io.ktor.server.auth.authenticate | ||
import io.ktor.server.auth.session | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.post | ||
import io.ktor.server.routing.routing | ||
import io.ktor.server.sessions.SessionStorage | ||
import io.ktor.server.sessions.Sessions | ||
import io.ktor.server.sessions.cookie | ||
import io.ktor.server.sessions.defaultSessionSerializer | ||
import io.ktor.server.sessions.serialization.KotlinxSessionSerializer | ||
import io.ktor.server.sessions.sessions | ||
import io.ktor.server.sessions.set | ||
import io.ktor.server.testing.testApplication | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class SessionAuthJvmTest { | ||
|
||
@Test | ||
fun sessionIgnoredForNonPublicEndpoints() = testApplication { | ||
val brokenStorage = object : SessionStorage { | ||
override suspend fun write(id: String, value: String) = Unit | ||
override suspend fun invalidate(id: String) = error("invalidate called") | ||
override suspend fun read(id: String): String = error("read called") | ||
} | ||
application { | ||
install(Sessions) { | ||
cookie<MySession>("S", storage = brokenStorage) { | ||
serializer = KotlinxSessionSerializer(Json.Default) | ||
} | ||
deferred = true | ||
} | ||
install(Authentication.Companion) { | ||
session<MySession> { | ||
validate { it } | ||
} | ||
} | ||
routing { | ||
authenticate { | ||
get("/authenticated") { | ||
call.respondText("Secret info") | ||
} | ||
} | ||
post("/session") { | ||
call.sessions.set(MySession(1)) | ||
call.respondText("OK") | ||
} | ||
get("/public") { | ||
call.respondText("Public info") | ||
} | ||
} | ||
} | ||
val withCookie: HttpRequestBuilder.() -> Unit = { | ||
header("Cookie", "S=${defaultSessionSerializer<MySession>().serialize(MySession(1))}") | ||
} | ||
|
||
assertEquals(HttpStatusCode.Companion.OK, client.post("/session").status) | ||
assertEquals(HttpStatusCode.Companion.OK, client.get("/public", withCookie).status) | ||
assertFailsWith<IllegalStateException> { | ||
client.get("/authenticated", withCookie).status | ||
} | ||
} | ||
|
||
@Serializable | ||
data class MySession(val id: Int) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...server-plugins/ktor-server-sessions/common/src/io/ktor/server/sessions/SessionDeferral.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.server.sessions | ||
|
||
import io.ktor.server.application.ApplicationCall | ||
|
||
/** | ||
* Creates a lazy loading session from the given providers. | ||
*/ | ||
internal expect fun createDeferredSession(call: ApplicationCall, providers: List<SessionProvider<*>>): StatefulSession |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...r-sessions/jsAndWasmShared/src/io/ktor/server/sessions/SessionDeferral.jsAndWasmShared.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.server.sessions | ||
|
||
import io.ktor.server.application.ApplicationCall | ||
|
||
internal actual fun createDeferredSession(call: ApplicationCall, providers: List<SessionProvider<*>>): StatefulSession = | ||
TODO("Deferred session retrieval is currently only available for JVM") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.