Skip to content
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

Kotlin Extension Functions for ReactiveUserDetailsService #8510

Open
juergenzimmermann opened this issue May 10, 2020 · 5 comments
Open

Kotlin Extension Functions for ReactiveUserDetailsService #8510

juergenzimmermann opened this issue May 10, 2020 · 5 comments
Labels
in: core An issue in spring-security-core type: enhancement A general enhancement

Comments

@juergenzimmermann
Copy link

Spring (WebFlux) has e.g. this extension function:

suspend fun ServerResponse.BodyBuilder.bodyValueAndAwait(body: Any): ServerResponse =
        bodyValue(body).awaitSingle()

See https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt#L62

According to the ...AndAwait naming convention I propose these Kotlin extension functions for ReactiveUserDetailsService:

suspend fun ReactiveUserDetailsService.findByUsernameAndAwait(username: String): UserDetails? =
    findByUsername(username).awaitFirstOrNull()

suspend fun ReactiveUserDetailsService.getAuthoritiesAndAwait(username: String): Collection<GrantedAuthority> {
    val userDetails = findByUsernameAndAwait(username) ?: return emptyList()
    return userDetails.authorities
}

suspend fun ReactiveUserDetailsService.getRolesAndAwait(username: String): Collection<String?> =
    getAuthoritiesAndAwait(username).map { grantedAuthority -> grantedAuthority.authority }
@juergenzimmermann juergenzimmermann added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels May 10, 2020
@eleftherias eleftherias self-assigned this May 11, 2020
@rwinch
Copy link
Member

rwinch commented May 12, 2020

@juergenzimmermann I think the difference is Spring's WebClient is intended to be used in blocking applications to allow parallel execution. If you need blocking operations, you should use the UserDetailsService instead of ReactiveUserDetailsService. Can you clarify why you feel this is appropriate for ReactiveUserDetailsService?

@rwinch rwinch added the status: waiting-for-feedback We need additional information before we can continue label May 12, 2020
@juergenzimmermann
Copy link
Author

@rwinch I'm having a reactive microservice, and therefore I'm using ReactiveUserDetailsService. Inside the business service I've to find out the roles of the user who was sending an HTTP request to the REST API.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 12, 2020
@rwinch
Copy link
Member

rwinch commented May 12, 2020

Sorry I just reread what was happening and realized it is using coroutines rather than blocking. Please ignore me :)

@eleftherias eleftherias added in: core An issue in spring-security-core and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels May 15, 2020
@eleftherias
Copy link
Contributor

@juergenzimmermann
I don't think getRolesAndAwait and getAuthoritiesAndAwait belong as extensions of ReactiveUserDetailsService, for the same reason that getRoles and getAuthorities are not methods in ReactiveUserDetailsService.
Roles and authorities are part of UserDetails which is why the getters are in that class.

We can modify this issue to be specific to findByUsernameAndAwait.

In the meantime, would this be equivalent to the following?

findByUsername(username).awaitFirstOrNull()

@juergenzimmermann
Copy link
Author

Yes, it will be equivalent to findByUsername(username).awaitFirstOrNull().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core An issue in spring-security-core type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants