-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44668 from michalvavrik/feature/kotlin-authorizat…
…ion-policy-fix Support @AuthorizationPolicy on suspended Kotlin endpoint methods
- Loading branch information
Showing
6 changed files
with
130 additions
and
1 deletion.
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
17 changes: 17 additions & 0 deletions
17
...n/standard/src/main/kotlin/io/quarkus/it/resteasy/reactive/kotlin/SecuredClassResource.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,17 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import io.quarkus.vertx.http.security.AuthorizationPolicy | ||
import jakarta.annotation.security.PermitAll | ||
import jakarta.ws.rs.GET | ||
import jakarta.ws.rs.Path | ||
|
||
@AuthorizationPolicy(name = "suspended") | ||
@Path("/secured-class") | ||
class SecuredClassResource { | ||
|
||
@Path("/authorization-policy-suspend") | ||
@GET | ||
suspend fun authorizationPolicySuspend() = "Hello from Quarkus REST" | ||
|
||
@PermitAll @Path("/public") @GET suspend fun publicEndpoint() = "Hello to everyone!" | ||
} |
14 changes: 14 additions & 0 deletions
14
.../standard/src/main/kotlin/io/quarkus/it/resteasy/reactive/kotlin/SecuredMethodResource.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,14 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import io.quarkus.vertx.http.security.AuthorizationPolicy | ||
import jakarta.ws.rs.GET | ||
import jakarta.ws.rs.Path | ||
|
||
@Path("/secured-method") | ||
class SecuredMethodResource { | ||
|
||
@Path("/authorization-policy-suspend") | ||
@GET | ||
@AuthorizationPolicy(name = "suspended") | ||
suspend fun authorizationPolicySuspend() = "Hello from Quarkus REST" | ||
} |
26 changes: 26 additions & 0 deletions
26
...dard/src/main/kotlin/io/quarkus/it/resteasy/reactive/kotlin/SuspendAuthorizationPolicy.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,26 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import io.quarkus.security.identity.SecurityIdentity | ||
import io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy | ||
import io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy.CheckResult | ||
import io.smallrye.mutiny.Uni | ||
import io.vertx.core.http.HttpHeaders | ||
import io.vertx.ext.web.RoutingContext | ||
import jakarta.enterprise.context.ApplicationScoped | ||
|
||
@ApplicationScoped | ||
class SuspendAuthorizationPolicy : HttpSecurityPolicy { | ||
override fun checkPermission( | ||
request: RoutingContext?, | ||
identity: Uni<SecurityIdentity>?, | ||
requestContext: HttpSecurityPolicy.AuthorizationRequestContext? | ||
): Uni<CheckResult> { | ||
val authZHeader = request?.request()?.getHeader(HttpHeaders.AUTHORIZATION) | ||
if (authZHeader == "you-can-trust-me") { | ||
return CheckResult.permit() | ||
} | ||
return CheckResult.deny() | ||
} | ||
|
||
override fun name() = "suspended" | ||
} |
47 changes: 47 additions & 0 deletions
47
...ve-kotlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/SecurityTest.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,47 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import io.quarkus.test.junit.QuarkusTest | ||
import io.restassured.module.kotlin.extensions.Given | ||
import io.restassured.module.kotlin.extensions.Then | ||
import io.restassured.module.kotlin.extensions.When | ||
import io.vertx.core.http.HttpHeaders | ||
import org.hamcrest.CoreMatchers | ||
import org.junit.jupiter.api.Test | ||
|
||
@QuarkusTest | ||
class SecurityTest { | ||
|
||
@Test | ||
fun testAuthorizationPolicyOnSuspendedMethod_MethodLevel() { | ||
When { get("/secured-method/authorization-policy-suspend") } Then { statusCode(403) } | ||
Given { header(HttpHeaders.AUTHORIZATION.toString(), "you-can-trust-me") } When | ||
{ | ||
get("/secured-method/authorization-policy-suspend") | ||
} Then | ||
{ | ||
statusCode(200) | ||
body(CoreMatchers.`is`("Hello from Quarkus REST")) | ||
} | ||
} | ||
|
||
@Test | ||
fun testAuthorizationPolicyOnSuspendedMethod_ClassLevel() { | ||
// test class-level annotation is applied on a secured method | ||
When { get("/secured-class/authorization-policy-suspend") } Then { statusCode(403) } | ||
Given { header(HttpHeaders.AUTHORIZATION.toString(), "you-can-trust-me") } When | ||
{ | ||
get("/secured-class/authorization-policy-suspend") | ||
} Then | ||
{ | ||
statusCode(200) | ||
body(CoreMatchers.`is`("Hello from Quarkus REST")) | ||
} | ||
|
||
// test method-level @PermitAll has priority over @AuthorizationPolicy on the class | ||
When { get("/secured-class/public") } Then | ||
{ | ||
statusCode(200) | ||
body(CoreMatchers.`is`("Hello to everyone!")) | ||
} | ||
} | ||
} |