Skip to content

Commit

Permalink
Merge pull request #336 from ulrikandersen/ktor-server-3.0.1
Browse files Browse the repository at this point in the history
Upgrade to Ktor Server 3.0.1
  • Loading branch information
ulrikandersen authored Nov 29, 2024
2 parents f4faf06 + b87c0bf commit b6d36d9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 73 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ allprojects {

val jacksonVersion by extra { "2.15.1" }
val junitVersion by extra { "5.9.2" }
val ktorVersion by extra { "2.3.9" }
val ktorVersion by extra { "3.0.1" }
val kotlinxSerializationVersion by extra { "1.7.3" }
val kotlinxDateTimeVersion by extra { "0.6.1" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ class KtorAuthenticationTest {
optionalRoutes(object : OptionalController {
override suspend fun testPath(
testString: String,
principal: Principal?,
call: ApplicationCall
) {
principalCaptureSlot.captured = principal as? UserIdPrincipal
principalCaptureSlot.captured = call.principal<UserIdPrincipal>()
call.respond(HttpStatusCode.OK)
}
})
Expand All @@ -70,10 +69,9 @@ class KtorAuthenticationTest {
optionalRoutes(object : OptionalController {
override suspend fun testPath(
testString: String,
principal: Principal?,
call: ApplicationCall
) {
principalCaptureSlot.captured = principal as? UserIdPrincipal
principalCaptureSlot.captured = call.principal<UserIdPrincipal>()
call.respond(HttpStatusCode.OK)
}
})
Expand All @@ -97,8 +95,8 @@ class KtorAuthenticationTest {

routing {
requiredRoutes(object : RequiredController {
override suspend fun testPath(testString: String, principal: Principal, call: ApplicationCall) {
principalCaptureSlot.captured = principal as UserIdPrincipal
override suspend fun testPath(testString: String, call: ApplicationCall) {
principalCaptureSlot.captured = call.principal<UserIdPrincipal>()
call.respond(HttpStatusCode.OK)
}
})
Expand All @@ -122,8 +120,8 @@ class KtorAuthenticationTest {

routing {
requiredRoutes(object : RequiredController {
override suspend fun testPath(testString: String, principal: Principal, call: ApplicationCall) {
principalCaptureSlot.captured = principal as UserIdPrincipal // should not get called
override suspend fun testPath(testString: String, call: ApplicationCall) {
principalCaptureSlot.captured = call.principal<UserIdPrincipal>() // should not get called
call.respond(HttpStatusCode.OK)
}
})
Expand Down Expand Up @@ -170,8 +168,8 @@ class KtorAuthenticationTest {

routing {
defaultRoutes(object : DefaultController {
override suspend fun testPath(testString: String, principal: Principal, call: ApplicationCall) {
principalCaptureSlot.captured = principal as UserIdPrincipal
override suspend fun testPath(testString: String, call: ApplicationCall) {
principalCaptureSlot.captured = call.principal<UserIdPrincipal>()
call.respond(HttpStatusCode.OK)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,6 @@ class KtorControllerInterfaceGenerator(
builder.addParameter(param.toParameterSpecBuilder().build())
}

val securityOption = operation.securitySupport(globalSecurity)
val addAuth = securityOption.allowsAuthenticated && options.contains(ControllerCodeGenOptionType.AUTHENTICATION)
if (addAuth) {
builder.addParameter(
"principal",
ClassName(
"io.ktor.server.auth",
"Principal"
).copy(nullable = securityOption == SecuritySupport.AUTHENTICATION_OPTIONAL)
).build()
}

builder.addKdoc(buildControllerFunKdoc(operation, params))

if (operation.happyPathResponse(packages.base).isUnit()) {
Expand Down Expand Up @@ -204,28 +192,6 @@ class KtorControllerInterfaceGenerator(
)
.indent()

if (addAuth) {
if (securityOption == SecuritySupport.AUTHENTICATION_OPTIONAL) {
builder
.addStatement(
"val principal = %M.%M<%T>()",
MemberName("io.ktor.server.application", "call"),
MemberName("io.ktor.server.auth", "principal", isExtension = true),
ClassName("io.ktor.server.auth", "Principal")
)
} else {
builder
.addStatement(
"val principal = %M.%M<%T>() ?: throw %M(%S)", // should not happen as authenticate { ... } ensures principal is present
MemberName("io.ktor.server.application", "call"),
MemberName("io.ktor.server.auth", "principal", isExtension = true),
ClassName("io.ktor.server.auth", "Principal"),
MemberName("kotlin", "IllegalStateException"),
"Principal not found"
)
}
}

pathParams.forEach { param ->
builder.addStatement(
"val ${param.name} = %M.parameters.%M<${param.type}>(\"${param.originalName}\")",
Expand Down Expand Up @@ -276,8 +242,6 @@ class KtorControllerInterfaceGenerator(

val methodParameters =
listOf(headerParams, pathParams, queryParams, bodyParams).asSequence().flatten().map { it.name }
.plus(if (addAuth) "principal" else null)
.filterNotNull()
.joinToString(", ")

if (operation.happyPathResponse(packages.base).isUnit()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.Parameters
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.auth.Principal
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.principal
import io.ktor.server.plugins.BadRequestException
import io.ktor.server.plugins.ParameterConversionException
import io.ktor.server.response.respond
Expand All @@ -17,7 +15,6 @@ import io.ktor.server.util.getOrFail
import io.ktor.util.converters.DefaultConversionService
import io.ktor.util.reflect.typeInfo
import kotlin.Any
import kotlin.IllegalStateException
import kotlin.String
import kotlin.Suppress

Expand All @@ -29,11 +26,7 @@ public interface RequiredController {
* @param testString
* @param call The Ktor application call
*/
public suspend fun testPath(
testString: String,
principal: Principal,
call: ApplicationCall,
)
public suspend fun testPath(testString: String, call: ApplicationCall)

public companion object {
/**
Expand All @@ -44,10 +37,8 @@ public interface RequiredController {
public fun Route.requiredRoutes(controller: RequiredController) {
authenticate("BasicAuth", "BearerAuth", optional = false) {
`get`("/required") {
val principal = call.principal<Principal>() ?: throw
IllegalStateException("Principal not found")
val testString = call.request.queryParameters.getOrFail<kotlin.String>("testString")
controller.testPath(testString, principal, call)
controller.testPath(testString, call)
}
}
}
Expand Down Expand Up @@ -151,11 +142,7 @@ public interface OptionalController {
* @param testString
* @param call The Ktor application call
*/
public suspend fun testPath(
testString: String,
principal: Principal?,
call: ApplicationCall,
)
public suspend fun testPath(testString: String, call: ApplicationCall)

public companion object {
/**
Expand All @@ -166,9 +153,8 @@ public interface OptionalController {
public fun Route.optionalRoutes(controller: OptionalController) {
authenticate("BasicAuth", optional = true) {
`get`("/optional") {
val principal = call.principal<Principal>()
val testString = call.request.queryParameters.getOrFail<kotlin.String>("testString")
controller.testPath(testString, principal, call)
controller.testPath(testString, call)
}
}
}
Expand Down Expand Up @@ -272,11 +258,7 @@ public interface DefaultController {
* @param testString
* @param call The Ktor application call
*/
public suspend fun testPath(
testString: String,
principal: Principal,
call: ApplicationCall,
)
public suspend fun testPath(testString: String, call: ApplicationCall)

public companion object {
/**
Expand All @@ -287,10 +269,8 @@ public interface DefaultController {
public fun Route.defaultRoutes(controller: DefaultController) {
authenticate("basicAuth", optional = false) {
`get`("/default") {
val principal = call.principal<Principal>() ?: throw
IllegalStateException("Principal not found")
val testString = call.request.queryParameters.getOrFail<kotlin.String>("testString")
controller.testPath(testString, principal, call)
controller.testPath(testString, call)
}
}
}
Expand Down

0 comments on commit b6d36d9

Please sign in to comment.