Skip to content

Commit

Permalink
Remove Reactor from the micronaut-security-jwt-cookie guide (#1435)
Browse files Browse the repository at this point in the history
* use HttpRequestAuthenticationProvider API

This AIP was introduced in 4.3.0

* micronaut-token-propagation guide

* micronaut-basic-auth guide

* micronaut-turbo-native

* micronaut-k8s

* use HttpRequestReactiveAuthenticationProvider

* micronaut-database-authentication-provider

* add back reactor

* Add Reactor back to turbo-native

* Fix micronaut-security-jwt-cookie

* Fix micronaut-database-authentication-provider

* Use B for consistency and fix micronaut-security-session-database-authentication

* Undeprecate micronaut-security-session

* Fix docs and some formatting

* Remove Reactor from the micronaut-security-jwt-cookie guide

* Apply a common layout to the code

* Revert "Apply a common layout to the code"
Z
This reverts commit 6eb21a3.

* Micronaut 4.3.4

* Remove debug

---------

Co-authored-by: Sergio del Amo <[email protected]>
  • Loading branch information
timyates and sdelamo authored Feb 27, 2024
1 parent 84dcc9b commit 5beb3c2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package example.micronaut.auth


import io.micronaut.core.annotation.NonNull
import io.micronaut.core.annotation.Nullable
import io.micronaut.http.HttpRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package example.micronaut.auth


import io.micronaut.core.annotation.NonNull
import io.micronaut.core.annotation.Nullable
import io.micronaut.http.HttpRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,22 @@ package example.micronaut
import io.micronaut.core.annotation.NonNull
import io.micronaut.core.annotation.Nullable
import io.micronaut.http.HttpRequest
import io.micronaut.security.authentication.AuthenticationFailureReason
import io.micronaut.security.authentication.AuthenticationRequest
import io.micronaut.security.authentication.AuthenticationResponse
import io.micronaut.security.authentication.provider.HttpRequestReactiveAuthenticationProvider
import io.micronaut.security.authentication.provider.HttpRequestAuthenticationProvider
import jakarta.inject.Singleton
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.FluxSink

@Singleton // <1>
class AuthenticationProviderUserPassword<B> implements HttpRequestReactiveAuthenticationProvider<B> { // <2>
class AuthenticationProviderUserPassword<B> implements HttpRequestAuthenticationProvider<B> { // <2>

@Override
Publisher<AuthenticationResponse> authenticate(
AuthenticationResponse authenticate(
@Nullable HttpRequest<B> httpRequest,
@NonNull AuthenticationRequest<String, String> authenticationRequest
) {
Flux.create(emitter -> {
if (authenticationRequest.identity == "sherlock" && authenticationRequest.secret == "password") {
emitter.next(AuthenticationResponse.success((String) authenticationRequest.identity))
emitter.complete()
} else {
emitter.error(AuthenticationResponse.exception())
}
}, FluxSink.OverflowStrategy.ERROR)
return authenticationRequest.identity == "sherlock" && authenticationRequest.secret == "password"
? AuthenticationResponse.success(authenticationRequest.getIdentity())
: AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,22 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpRequest;
import io.micronaut.security.authentication.AuthenticationFailureReason;
import io.micronaut.security.authentication.AuthenticationRequest;
import io.micronaut.security.authentication.AuthenticationResponse;
import io.micronaut.security.authentication.provider.HttpRequestReactiveAuthenticationProvider;
import io.micronaut.security.authentication.provider.HttpRequestAuthenticationProvider;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

@Singleton // <1>
public class AuthenticationProviderUserPassword<B> implements HttpRequestReactiveAuthenticationProvider<B> { // <2>
public class AuthenticationProviderUserPassword<B> implements HttpRequestAuthenticationProvider<B> { // <2>

@Override
public @NonNull Publisher<AuthenticationResponse> authenticate(
public @NonNull AuthenticationResponse authenticate(
@Nullable HttpRequest<B> requestContext,
@NonNull AuthenticationRequest<String, String> authenticationRequest
) {
return Flux.create(emitter -> {
if (authenticationRequest.getIdentity().equals("sherlock") &&
authenticationRequest.getSecret().equals("password")) {
emitter.next(AuthenticationResponse.success((String) authenticationRequest.getIdentity()));
emitter.complete();
} else {
emitter.error(AuthenticationResponse.exception());
}
}, FluxSink.OverflowStrategy.ERROR);
return authenticationRequest.getIdentity().equals("sherlock") && authenticationRequest.getSecret().equals("password")
? AuthenticationResponse.success(authenticationRequest.getIdentity())
: AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,19 @@
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.security.authentication.AuthenticationFailureReason
import io.micronaut.security.authentication.AuthenticationRequest
import io.micronaut.security.authentication.AuthenticationResponse
import io.micronaut.security.authentication.provider.HttpRequestReactiveAuthenticationProvider
import io.micronaut.security.authentication.provider.HttpRequestAuthenticationProvider
import jakarta.inject.Singleton
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.FluxSink

@Singleton // <1>
class AuthenticationProviderUserPassword<B> : HttpRequestReactiveAuthenticationProvider<B> { // <2>
class AuthenticationProviderUserPassword<B> : HttpRequestAuthenticationProvider<B> { // <2>

override fun authenticate(
httpRequest: HttpRequest<B>?,
authenticationRequest: AuthenticationRequest<String, String>
) : Publisher<AuthenticationResponse> = Flux.create({ emitter: FluxSink<AuthenticationResponse> ->
if (authenticationRequest.identity == "sherlock" && authenticationRequest.secret == "password") {
emitter.next(AuthenticationResponse.success(authenticationRequest.identity as String))
emitter.complete()
} else {
emitter.error(AuthenticationResponse.exception())
}
}, FluxSink.OverflowStrategy.ERROR)
) : AuthenticationResponse = if (authenticationRequest.identity == "sherlock" && authenticationRequest.secret == "password")
AuthenticationResponse.success(authenticationRequest.identity) else
AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH)
}
2 changes: 1 addition & 1 deletion guides/micronaut-security-jwt-cookie/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"apps": [
{
"name": "default",
"features": ["yaml", "security-jwt", "graalvm", "views-velocity", "geb", "reactor"]
"features": ["yaml", "security-jwt", "graalvm", "views-velocity", "geb"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ resource:application.yml[tag=security]

=== Authentication Provider

To keep this guide simple, create a naive reactive `AuthenticationProvider` to simulate user's authentication.
To keep this guide simple, create a naive `AuthenticationProvider` to simulate user's authentication.

source:AuthenticationProviderUserPassword[]

callout:singleton[1]
callout:http-request-reactive-authentication-provider[2]
callout:http-request-authentication-provider[2]

=== Apache Velocity

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.3
4.3.4

0 comments on commit 5beb3c2

Please sign in to comment.