Skip to content

Commit

Permalink
Merge pull request #45442 from geoand/http-mech-bean
Browse files Browse the repository at this point in the history
Move BasicAuthenticationMechanism to synthetic bean
  • Loading branch information
geoand authored Jan 9, 2025
2 parents 985ecf5 + ba997d7 commit 75a1743
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTransformation;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand All @@ -37,9 +36,7 @@
import org.jboss.jandex.TypeVariable;
import org.objectweb.asm.Opcodes;

import io.quarkus.arc.DefaultBean;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanBuildItem;
Expand Down Expand Up @@ -187,23 +184,28 @@ void detectBasicAuthImplicitlyRequired(HttpBuildTimeConfig buildTimeConfig,
}

@BuildStep(onlyIf = IsApplicationBasicAuthRequired.class)
AdditionalBeanBuildItem initBasicAuth(HttpBuildTimeConfig buildTimeConfig,
BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformerProducer,
@Record(ExecutionTime.RUNTIME_INIT)
SyntheticBeanBuildItem initBasicAuth(HttpSecurityRecorder recorder,
HttpConfiguration runtimeConfig,
HttpBuildTimeConfig buildTimeConfig,
BuildProducer<SecurityInformationBuildItem> securityInformationProducer) {

if (makeBasicAuthMechDefaultBean(buildTimeConfig)) {
//if not explicitly enabled we make this a default bean, so it is the fallback if nothing else is defined
annotationsTransformerProducer.produce(new AnnotationsTransformerBuildItem(AnnotationTransformation
.forClasses()
.whenClass(BASIC_AUTH_MECH_NAME)
.transform(ctx -> ctx.add(DefaultBean.class))));
}

if (buildTimeConfig.auth.basic.isPresent() && buildTimeConfig.auth.basic.get()) {
securityInformationProducer.produce(SecurityInformationBuildItem.BASIC());
}

return AdditionalBeanBuildItem.builder().setUnremovable().addBeanClass(BasicAuthenticationMechanism.class).build();
SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem
.configure(BasicAuthenticationMechanism.class)
.types(io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism.class)
.scope(Singleton.class)
.supplier(recorder.basicAuthenticationMechanismBean(runtimeConfig, buildTimeConfig.auth.form.enabled))
.setRuntimeInit()
.unremovable();
if (makeBasicAuthMechDefaultBean(buildTimeConfig)) {
configurator.defaultBean();
}

return configurator.done();
}

private static boolean makeBasicAuthMechDefaultBean(HttpBuildTimeConfig buildTimeConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
import java.util.Set;
import java.util.regex.Pattern;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.jboss.logging.Logger;

import io.netty.handler.codec.http.HttpHeaderNames;
Expand All @@ -51,7 +48,6 @@
* The authentication handler responsible for BASIC authentication as described by RFC2617
*
*/
@Singleton
public class BasicAuthenticationMechanism implements HttpAuthenticationMechanism {

private static final Logger log = Logger.getLogger(BasicAuthenticationMechanism.class);
Expand All @@ -75,7 +71,6 @@ public class BasicAuthenticationMechanism implements HttpAuthenticationMechanism
private final Charset charset;
private final Map<Pattern, Charset> userAgentCharsets;

@Inject
BasicAuthenticationMechanism(HttpConfiguration runtimeConfig, HttpBuildTimeConfig buildTimeConfig) {
this(runtimeConfig.auth.realm.orElse(null), buildTimeConfig.auth.form.enabled);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,14 @@ private static Set<String> parseRoles(String value) {
return Set.copyOf(roles);
}

public Supplier<BasicAuthenticationMechanism> basicAuthenticationMechanismBean(HttpConfiguration runtimeConfig,
boolean formAuthEnabled) {
return new Supplier<>() {
@Override
public BasicAuthenticationMechanism get() {
return new BasicAuthenticationMechanism(runtimeConfig.auth.realm.orElse(null), formAuthEnabled);
}
};
}

}

0 comments on commit 75a1743

Please sign in to comment.