Skip to content

Commit 0b9fd14

Browse files
committed
refactor: bump ktor major version for examples 2.3.13 -> 3.3.0
1 parent 2abe29f commit 0b9fd14

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

build.gradle.kts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val kotestVersion = "6.0.3"
1515
val bouncyCastleVersion = "1.82"
1616
val springBootVersion = "3.5.6"
1717
val reactorTestVersion = "3.7.11"
18-
val ktorVersion = "2.3.13"
18+
val ktorVersion = "3.3.0"
1919
val jsonPathVersion = "2.9.0"
2020

2121
val mainClassKt = "no.nav.security.mock.oauth2.StandaloneMockOAuth2ServerKt"
@@ -29,6 +29,7 @@ plugins {
2929
id("com.google.cloud.tools.jib") version "3.4.5"
3030
id("com.vanniktech.maven.publish") version "0.34.0"
3131
id("org.jetbrains.dokka") version "2.0.0"
32+
kotlin("plugin.serialization") version "2.2.0"
3233
`java-library`
3334
signing
3435
}
@@ -127,15 +128,18 @@ dependencies {
127128
testImplementation("io.projectreactor:reactor-test:$reactorTestVersion")
128129
testImplementation("io.ktor:ktor-server-netty:$ktorVersion")
129130
testImplementation("io.ktor:ktor-server-sessions:$ktorVersion")
130-
testImplementation("io.ktor:ktor-server-locations:$ktorVersion")
131+
testImplementation("io.ktor:ktor-server-resources:$ktorVersion")
131132
testImplementation("io.ktor:ktor-server-auth:$ktorVersion")
132133
testImplementation("io.ktor:ktor-server-auth-jwt:$ktorVersion")
133134
testImplementation("io.ktor:ktor-server-content-negotiation:$ktorVersion")
134135
testImplementation("io.ktor:ktor-client-core:$ktorVersion")
135136
testImplementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
136137
testImplementation("io.ktor:ktor-serialization-jackson:$ktorVersion")
137138
testImplementation("io.ktor:ktor-client-cio:$ktorVersion")
138-
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion")
139+
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion"){
140+
//Provides transitive vulnerable dependency maven:commons-codec:commons-codec:1.11 WS-2019-0379 6.5 Input Validation Results powered by Mend.io
141+
exclude("commons-codec", "commons-codec")
142+
}
139143
}
140144

141145
configurations {

src/test/kotlin/examples/kotlin/ktor/login/OAuth2LoginApp.kt

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,31 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
99
import io.ktor.http.ContentType
1010
import io.ktor.http.HttpMethod
1111
import io.ktor.http.HttpStatusCode
12+
import io.ktor.http.encodedPath
13+
import io.ktor.resources.Resource
1214
import io.ktor.serialization.jackson.jackson
1315
import io.ktor.server.application.Application
1416
import io.ktor.server.application.ApplicationCall
15-
import io.ktor.server.application.call
1617
import io.ktor.server.application.install
18+
import io.ktor.server.application.log
1719
import io.ktor.server.auth.Authentication
1820
import io.ktor.server.auth.OAuthAccessTokenResponse
1921
import io.ktor.server.auth.OAuthServerSettings
2022
import io.ktor.server.auth.authenticate
2123
import io.ktor.server.auth.authentication
2224
import io.ktor.server.auth.oauth
2325
import io.ktor.server.engine.embeddedServer
24-
import io.ktor.server.locations.KtorExperimentalLocationsAPI
25-
import io.ktor.server.locations.Location
26-
import io.ktor.server.locations.Locations
27-
import io.ktor.server.locations.location
28-
import io.ktor.server.locations.locations
29-
import io.ktor.server.locations.url
3026
import io.ktor.server.netty.Netty
27+
import io.ktor.server.resources.Resources
28+
import io.ktor.server.resources.href
29+
import io.ktor.server.resources.resource
3130
import io.ktor.server.response.respondText
31+
import io.ktor.server.routing.application
3232
import io.ktor.server.routing.get
3333
import io.ktor.server.routing.param
3434
import io.ktor.server.routing.routing
35+
import io.ktor.server.util.url
36+
import kotlinx.serialization.Serializable
3537

3638
fun main() {
3739
embeddedServer(Netty, port = 8080) {
@@ -54,48 +56,57 @@ fun main() {
5456
}.start(true)
5557
}
5658

57-
@OptIn(KtorExperimentalLocationsAPI::class)
5859
fun Application.module(authConfig: AuthConfig) {
5960
val idProviders = authConfig.providers.map { it.settings }.associateBy { it.name }
6061

61-
install(Locations)
62+
install(Resources)
6263
install(Authentication) {
6364
oauth("oauth2") {
6465
client = httpClient
6566
providerLookup = {
66-
idProviders[application.locations.resolve<Login>(Login::class, this).type] ?: idProviders.values.first()
67+
val t = this.parameters["type"].orEmpty()
68+
idProviders[t] ?: idProviders.values.first()
6769
}
6870
urlProvider = {
69-
url(Login(it.name))
71+
url {
72+
encodedPath = application.href(Login(it.name))
73+
}
7074
}
7175
}
7276
}
7377

7478
routing {
79+
trace { application.log.info(it.buildText()) }
7580
authenticate("oauth2") {
7681
get {
7782
call.respondText("nothing to see here really")
7883
}
79-
location<Login> {
84+
resource<Login> {
85+
// /login/{type}?error=...
8086
param("error") {
81-
handle {
82-
call.respondText(ContentType.Text.Html, HttpStatusCode.BadRequest) {
87+
get {
88+
call.respondText(
89+
ContentType.Text.Html,
90+
HttpStatusCode.BadRequest
91+
) {
8392
"received error on login: ${call.parameters.getAll("error").orEmpty()}"
8493
}
8594
}
8695
}
87-
handle {
96+
// /login/{type}
97+
get {
8898
call.respondText("welcome ${call.subject()}")
8999
}
90100
}
101+
91102
}
92103
}
93104
}
94105

95-
@Location("/login/{type?}")
96-
@OptIn(KtorExperimentalLocationsAPI::class)
106+
@Serializable
107+
@Resource("/login/{type?}")
97108
class Login(
98-
val type: String = "",
109+
val type: String? = "",
99110
)
100111

101112
class AuthConfig(

src/test/kotlin/examples/kotlin/ktor/login/OAuth2LoginAppTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ internal class OAuth2LoginAppTest {
5353
port: Int,
5454
test: ApplicationEngine.() -> R,
5555
): R {
56-
val engine =
56+
val server =
5757
embeddedServer(Netty, port = port) {
5858
moduleFunction(this)
5959
}
60-
engine.start()
60+
server.start()
6161
try {
62-
return engine.test()
62+
return test(server.engine)
6363
} finally {
64-
engine.stop(0L, 0L)
64+
server.stop(0L, 0L)
6565
}
6666
}
6767

0 commit comments

Comments
 (0)