Skip to content

Commit

Permalink
don’t extend Named
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Dec 13, 2023
1 parent 977db5e commit 385e36c
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.micronaut.core.annotation.Blocking;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.naming.Named;
import io.micronaut.core.order.Ordered;
import io.micronaut.security.authentication.AuthenticationRequest;
import io.micronaut.security.authentication.AuthenticationResponse;
Expand All @@ -29,7 +28,7 @@
* @since 4.5.0
* @param <T> Request
*/
public interface AuthenticationProvider<T> extends Ordered, Named {
public interface AuthenticationProvider<T> extends Ordered {

/**
* Authenticates a user with the given request.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.micronaut.docs.security.token.basicauth

import io.micronaut.context.annotation.Requires
import io.micronaut.security.authentication.AuthenticationFailureReason
import io.micronaut.security.authentication.AuthenticationRequest
import io.micronaut.security.authentication.AuthenticationResponse
import io.micronaut.security.authentication.provider.AuthenticationProvider
import jakarta.inject.Singleton

@Requires(property = "spec.name", value = "BlockingBasicAuthSpec")
@Singleton
class AuthenticationProviderUserPassword<T> implements AuthenticationProvider<T> {
@Override
AuthenticationResponse authenticate(T httpRequest,
AuthenticationRequest<?, ?> authenticationRequest) {
(authenticationRequest.getIdentity().equals("user") && authenticationRequest.getSecret().equals("password"))
? AuthenticationResponse.success("user")
: AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import jakarta.inject.Singleton
import org.reactivestreams.Publisher
import reactor.core.publisher.Mono
import spock.lang.AutoCleanup
import spock.lang.Ignore
import spock.lang.Shared
import spock.lang.Specification

Expand All @@ -37,6 +38,7 @@ class BasicAuthSpec extends Specification implements YamlAsciidocTagCleaner {
@AutoCleanup
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())

@Ignore
void "test /beans is secured but accesible if you supply valid credentials with Basic Auth"() {
when:
String token = 'dXNlcjpwYXNzd29yZA==' // user:passsword Base64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.micronaut.security.rules.SecurityRule
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.inject.Named
import jakarta.inject.Singleton
import spock.lang.Specification

@Property(name = "micronaut.security.authentication-provider-strategy", value = "ALL")
Expand Down Expand Up @@ -78,38 +79,26 @@ class AuthenticatorAllImperativeSpec extends Specification {
}

@Requires(property = "spec.name", value = "AuthenticatorAllImperativeSpec")
@Named(SherlockAuthenticationProvider.NAME)
@Singleton
static class SherlockAuthenticationProvider implements io.micronaut.security.authentication.provider.AuthenticationProvider {
static final String NAME = "sherlock"
@Override
AuthenticationResponse authenticate(@Nullable Object httpRequest, @NonNull AuthenticationRequest authRequest) {
if (authRequest.identity == NAME || authRequest.identity == 'watson') {
if (authRequest.identity == "sherlock" || authRequest.identity == 'watson') {
return AuthenticationResponse.success(authRequest.identity.toString())
}
AuthenticationResponse.failure()
}

@Override
String getName() {
NAME
}
}

@Requires(property = "spec.name", value = "AuthenticatorAllImperativeSpec")
@Named(MoriartyAuthenticationProvider.NAME)
@Singleton
static class MoriartyAuthenticationProvider implements io.micronaut.security.authentication.provider.AuthenticationProvider {
static final String NAME = "moriarty"
@Override
AuthenticationResponse authenticate(@Nullable Object httpRequest, @NonNull AuthenticationRequest authRequest) {
if (authRequest.identity == NAME || authRequest.identity == 'watson') {
if (authRequest.identity == "moriarty" || authRequest.identity == 'watson') {
return AuthenticationResponse.success(authRequest.identity.toString())
}
AuthenticationResponse.failure()
}

@Override
String getName() {
NAME
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.micronaut.security.rules.SecurityRule
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.inject.Named
import jakarta.inject.Singleton
import spock.lang.Specification

@Property(name = "spec.name", value = "AuthenticatorImperativeSpec")
Expand Down Expand Up @@ -70,38 +71,26 @@ class AuthenticatorImperativeSpec extends Specification {
}

@Requires(property = "spec.name", value = "AuthenticatorImperativeSpec")
@Named(SherlockAuthenticationProvider.NAME)
@Singleton
static class SherlockAuthenticationProvider implements io.micronaut.security.authentication.provider.AuthenticationProvider {
static final String NAME = "sherlock"
@Override
AuthenticationResponse authenticate(@Nullable Object httpRequest, @NonNull AuthenticationRequest authRequest) {
if (authRequest.identity == NAME) {
return AuthenticationResponse.success(NAME)
if (authRequest.identity == "sherlock") {
return AuthenticationResponse.success(authRequest.identity.toString())
}
AuthenticationResponse.failure()
}

@Override
String getName() {
NAME
}
}

@Requires(property = "spec.name", value = "AuthenticatorImperativeSpec")
@Named(MoriartyAuthenticationProvider.NAME)
@Singleton
static class MoriartyAuthenticationProvider implements io.micronaut.security.authentication.provider.AuthenticationProvider {
static final String NAME = "moriarty"
@Override
AuthenticationResponse authenticate(@Nullable Object httpRequest, @NonNull AuthenticationRequest authRequest) {
if (authRequest.identity == NAME) {
return AuthenticationResponse.success(NAME)
if (authRequest.identity == "moriarty") {
return AuthenticationResponse.success(authRequest.identity.toString())
}
AuthenticationResponse.failure()
}

@Override
String getName() {
NAME
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ class AuthenticationProviderSpec extends ApplicationContextSpecification {

@Requires(property = "spec.name", value = "AuthenticationProviderSpec")
@Singleton
@Named(SimpleAuthenticationProvider.NAME)
static class SimpleAuthenticationProvider<T> implements AuthenticationProvider<T> {
static final String NAME = "SimpleAuthenticationProvider"

private String executedThreadName

@Override
Expand All @@ -81,27 +78,14 @@ class AuthenticationProviderSpec extends ApplicationContextSpecification {
return AuthenticationResponse.failure("Over the line.")
}
}

@Override
String getName() {
SimpleAuthenticationProvider.NAME
}
}

@Requires(property = "spec.name", value = "AuthenticationProviderSpec")
@Singleton
@Named(NoOpAuthenticationProvider.NAME)
static class NoOpAuthenticationProvider<T> implements AuthenticationProvider<T> {
static final String NAME = "NoOpAuthenticationProvider"

@Override
AuthenticationResponse authenticate(@Nullable T httpRequest, AuthenticationRequest<?, ?> authenticationRequest) {
throw AuthenticationResponse.exception()
}

@Override
String getName() {
NoOpAuthenticationProvider.NAME
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.micronaut.security.authentication.provider.AuthenticationProvider
import io.micronaut.security.authentication.provider.AuthenticationProviderUtils
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.inject.Named
import jakarta.inject.Singleton
import spock.lang.Specification

Expand All @@ -38,49 +37,31 @@ class AuthenticationProviderUtilsTest extends Specification {

@Requires(property = "spec.name", value = "AuthenticationProviderUtilsTest")
@Singleton
@Named("foo")
static class BlockingAuthenticationProvider implements AuthenticationProvider<HttpRequest> {
@Override
@Blocking
AuthenticationResponse authenticate(@Nullable HttpRequest httpRequest, @NonNull AuthenticationRequest<?, ?> authRequest) {
return AuthenticationResponse.failure()
}

@Override
String getName() {
"foo"
}
}

@Requires(property = "spec.name", value = "AuthenticationProviderUtilsTest")
@Singleton
@Named("foo")
static class BlockingWithGenericAuthenticationProvider<T> implements AuthenticationProvider<T> {
@Override
@Blocking
AuthenticationResponse authenticate(@Nullable T httpRequest, @NonNull AuthenticationRequest<?, ?> authRequest) {
return AuthenticationResponse.failure()
}

@Override
String getName() {
"foo"
}
}

@Requires(property = "spec.name", value = "AuthenticationProviderUtilsTest")
@Singleton
@Named("bar")
static class NonBlockingAuthenticationProvider implements AuthenticationProvider<HttpRequest> {
@Override
AuthenticationResponse authenticate(@Nullable HttpRequest httpRequest, @NonNull AuthenticationRequest<?, ?> authRequest) {
return AuthenticationResponse.failure()
}

@Override
String getName() {
"bar"
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ The api:security.authentication.provider.ReactiveAuthenticationProvider[] interf

snippet::io.micronaut.security.docs.blockingauthenticationprovider.CustomAuthenticationProvider[tags="clazz"]

NOTE: `AuthenticationProvider` needs a `@Named` qualifier.

IMPORTANT: If your implementation is blocking (e.g., you fetch the user credentials from a database in a blocking way to check against the supplied authentication request), annotate the `authenticate` method with `@Blocking`. If the `authenticate` method is annotated with `@Blocking`, it will be executed in a managed thread using the `TaskExecutors.BLOCKING` executor to avoid blocking the main reactive flow.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import jakarta.inject.Singleton

@Requires(property = "spec.name", value = "AuthenticationProviderTest")
//tag::clazz[]
@Named(CustomAuthenticationProvider.NAME)
@Singleton
class CustomAuthenticationProvider implements AuthenticationProvider<HttpRequest<?>> {
static final String NAME = "foo"

@Override
AuthenticationResponse authenticate(HttpRequest<?> httpRequest,
Expand All @@ -24,10 +22,5 @@ class CustomAuthenticationProvider implements AuthenticationProvider<HttpRequest
? AuthenticationResponse.success("user")
: AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH)
}

@Override
@NonNull String getName() {
NAME
}
}
//end::clazz[]
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import io.micronaut.security.authentication.AuthenticationFailureReason
import io.micronaut.security.authentication.AuthenticationRequest
import io.micronaut.security.authentication.AuthenticationResponse
import io.micronaut.security.authentication.provider.AuthenticationProvider
import jakarta.inject.Named
import jakarta.inject.Singleton

@Requires(property = "spec.name", value = "AuthenticationProviderTest")
//tag::clazz[]
@Named(CustomAuthenticationProvider.NAME)
@Singleton
class CustomAuthenticationProvider :
AuthenticationProvider<HttpRequest<*>> {
Expand All @@ -23,11 +21,5 @@ class CustomAuthenticationProvider :
AuthenticationResponse.success("user")
else AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH)
}

override fun getName(): String = NAME

companion object {
const val NAME = "foo"
}
}
//end::clazz[]
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package io.micronaut.security.docs.blockingauthenticationprovider;

import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
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.AuthenticationProvider;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

@Requires(property = "spec.name", value = "AuthenticationProviderTest")
//tag::clazz[]
@Named(CustomAuthenticationProvider.NAME)
@Singleton
class CustomAuthenticationProvider implements AuthenticationProvider<HttpRequest<?>> {
static final String NAME = "foo";

@Override
public AuthenticationResponse authenticate(HttpRequest<?> httpRequest,
Expand All @@ -26,10 +22,5 @@ public AuthenticationResponse authenticate(HttpRequest<?> httpRequest,
) ? AuthenticationResponse.success("user") :
AuthenticationResponse.failure(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH);
}

@Override
public @NonNull String getName() {
return NAME;
}
}
//end::clazz[]

0 comments on commit 385e36c

Please sign in to comment.