-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JpaIdentityProvider not working with Reactive Panache entities #19302
Labels
Comments
/cc @FroMage, @loicmathieu |
Same issue with 2.4.1. |
I created a workaround package custom.handler
import custom.model.UserAccount
import io.quarkus.security.AuthenticationFailedException
import io.quarkus.security.ForbiddenException
import io.quarkus.security.identity.request.UsernamePasswordAuthenticationRequest
import io.quarkus.security.jpa.runtime.JpaIdentityProvider
import io.vertx.mutiny.pgclient.PgPool
import io.vertx.mutiny.sqlclient.Tuple.of
import java.time.Duration.ofSeconds
import java.util.*
import javax.annotation.Priority
import javax.enterprise.context.ApplicationScoped
import javax.enterprise.inject.Alternative
import javax.inject.Inject
import javax.persistence.EntityManager
// TODO WORKAROUND FOR https://github.com/quarkusio/quarkus/issues/19302
@Suppress("unused")
@Alternative
@Priority(1)
@ApplicationScoped
class CustomUserEntityIdentityProvider : JpaIdentityProvider() {
@Inject
lateinit var client: PgPool
override fun authenticate(em: EntityManager, request: UsernamePasswordAuthenticationRequest) =
client.preparedQuery("SELECT id, password, username FROM useraccount WHERE username = $1")
.execute(of(request.username))
.onFailure().transform(::AuthenticationFailedException)
.onItem().ifNotNull().transform { rs ->
if (rs.count() != 1) throw AuthenticationFailedException() else
rs.map {
UserAccount(
password = it.getString("password"),
username = it.getString("username")
).apply { id = it.getUUID("id") }
}
}.await().asOptional().atMost(ofSeconds(10)).orElse(null)?.first()?.run {
checkPassword(getMcfPassword(password), request).also {
addRoles(it, getUserRoles(id))
}.run { build() }
}
private fun getUserRoles(useraccount_id: UUID) =
client.preparedQuery("SELECT ur.name FROM userrole ur INNER JOIN useraccount_userrole ua_ur ON ua_ur.roles_id = ur.id WHERE ua_ur.users_id = $1")
.execute(of(useraccount_id))
.onFailure().recoverWithNull()
.onItem().ifNull().failWith(::ForbiddenException)
.onItem().ifNotNull().transform { rs -> rs.map { it.getString("name") } }
.await().asOptional().atMost(ofSeconds(10)).orElse(emptyList()).joinToString(separator = ",")
} |
Let me close it as not planned for the (non-reactive) security JPA extension, please see an enhancement request at #23553 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Describe the bug
When using
jpa-security
as described in guides, but replacing normal imperative hibernate entities with reactive Panache entities, I got an exception after calling this code:The exception:
I think this can be solved by modifying this snippet:
quarkus/extensions/security-jpa/runtime/src/main/java/io/quarkus/security/jpa/runtime/AbstractJpaIdentityProvider.java
Lines 51 to 59 in b8eff91
and adding something like:
I believe
getSingleUser
method is being invoked by the generatedJpaIdentityProvider
:quarkus/extensions/security-jpa/deployment/src/main/java/io/quarkus/security/jpa/deployment/QuarkusSecurityJpaProcessor.java
Lines 402 to 405 in 56d4ae5
Expected behavior
No response
Actual behavior
No response
How to Reproduce?
No response
Output of
uname -a
orver
Windows 10
Output of
java -version
16
GraalVM version (if different from Java)
No response
Quarkus version or git rev
2.1.1.Final
Build tool (ie. output of
mvnw --version
orgradlew --version
)Gradle 7.1.1
Additional information
No response
The text was updated successfully, but these errors were encountered: