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

DATAGO-81464: custom condition for OAuth2 auto configuration #138

Merged
merged 8 commits into from
Jul 26, 2024
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.solace.spring.boot.autoconfigure;

import com.solace.spring.boot.autoconfigure.SolaceOAuthClientConfiguration.OAuth2ClientRegistrationIdCondition;
import com.solace.spring.boot.autoconfigure.SolaceOAuthClientConfiguration.SolaceOAuth2SchemeCondition;
import com.solacesystems.jcsmp.DefaultSolaceSessionOAuth2TokenProvider;
import com.solacesystems.jcsmp.JCSMPProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import java.util.Objects;
import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
Expand All @@ -19,7 +26,7 @@
* 'AUTHENTICATION_SCHEME_OAUTH2'.
*/
@Configuration
@ConditionalOnExpression("'${solace.java.api-properties.AUTHENTICATION_SCHEME}' == 'AUTHENTICATION_SCHEME_OAUTH2' OR '${solace.java.apiProperties.AUTHENTICATION_SCHEME}' == 'AUTHENTICATION_SCHEME_OAUTH2'")
@Conditional({SolaceOAuth2SchemeCondition.class, OAuth2ClientRegistrationIdCondition.class})
@Import(OAuth2ClientAutoConfiguration.class)
public class SolaceOAuthClientConfiguration {

Expand Down Expand Up @@ -66,4 +73,36 @@ public DefaultSolaceSessionOAuth2TokenProvider solaceSessionOAuth2TokenProvider(
return new DefaultSolaceSessionOAuth2TokenProvider(jcsmpProperties,
solaceOAuthAuthorizedClientServiceAndManager);
}


/**
* Condition class to check if the 'solace.java.apiProperties.AUTHENTICATION_SCHEME' property is
* set to 'AUTHENTICATION_SCHEME_OAUTH2'.
*/
static class SolaceOAuth2SchemeCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return Binder.get(context.getEnvironment())
.bind("solace.java", SolaceJavaProperties.class)
.map(SolaceJavaProperties::getApiProperties)
.map(p -> p.get(JCSMPProperties.AUTHENTICATION_SCHEME))
.map(v -> v.equals(JCSMPProperties.AUTHENTICATION_SCHEME_OAUTH2))
.orElse(false);
}
}

/**
* Condition class to check if the 'solace.java.oauth2ClientRegistrationId' property is set.
*/
static class OAuth2ClientRegistrationIdCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return Binder.get(context.getEnvironment())
.bind("solace.java", SolaceJavaProperties.class)
.map(SolaceJavaProperties::getOauth2ClientRegistrationId)
.isBound();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public static class TestApp {
void verifyApplicationContextContainsRequiredBeans() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
.profiles("oauthConfigIT").sources(TestApp.class)
.properties(String.format("%s=%s", "solace.java.api-properties.AUTHENTICATION_SCHEME",
"AUTHENTICATION_SCHEME_OAUTH2"))
.properties(
String.format("%s=%s", "solace.java.api-properties.AUTHENTICATION_SCHEME", "AUTHENTICATION_SCHEME_OAUTH2"),
String.format("%s=%s", "solace.java.oauth2ClientRegistrationId", "my-oauth2-client"))
Nephery marked this conversation as resolved.
Show resolved Hide resolved
.run()) {
assertThat(context.isRunning()).isTrue();
assertThat(context.getBean(SolaceSessionOAuth2TokenProvider.class)).isNotNull();
Expand All @@ -35,8 +36,9 @@ void verifyApplicationContextContainsRequiredBeans() {
void verifyApplicationContextContainsRequiredBeans2() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
.profiles("oauthConfigIT").sources(TestApp.class)
.properties(String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME",
"AUTHENTICATION_SCHEME_OAUTH2"))
.properties(
String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME", "AUTHENTICATION_SCHEME_OAUTH2"),
String.format("%s=%s", "solace.java.oauth2-client-registration-id", "my-oauth2-client"))
.run()) {
assertThat(context.isRunning()).isTrue();
assertThat(context.getBean(SolaceSessionOAuth2TokenProvider.class)).isNotNull();
Expand All @@ -49,14 +51,48 @@ void verifyApplicationContextContainsRequiredBeans2() {
void verifyApplicationContextDoesNotContainOAuth2BeansWhenAuthSchemeIsNotOAuth2() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
.profiles("oauthConfigIT").sources(TestApp.class)
.properties(String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME",
"AUTHENTICATION_SCHEME_BASIC"))
.properties(
String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME", "AUTHENTICATION_SCHEME_BASIC"),
String.format("%s=%s", "solace.java.oauth2ClientRegistrationId", "my-oauth2-client"))
.run()) {
assertThat(context.isRunning()).isTrue();

assertThatThrownBy(() -> context.getBean(SolaceSessionOAuth2TokenProvider.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
assertThatThrownBy(() -> context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class))
assertThatThrownBy(
() -> context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}
}

@Test
void verifyApplicationContextDoesNotContainOAuth2BeansWhenAuthSchemePropertyNotDefined() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
.profiles("oauthConfigIT").sources(TestApp.class)
.properties(String.format("%s=%s", "solace.java.oauth2ClientRegistrationId", "my-oauth2-client"))
.run()) {
assertThat(context.isRunning()).isTrue();

assertThatThrownBy(() -> context.getBean(SolaceSessionOAuth2TokenProvider.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
assertThatThrownBy(
() -> context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}
}

@Test
void verifyApplicationContextDoesNotContainOAuth2BeansWhenClientRegistrationIdNotDefined() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
.profiles("oauthConfigIT").sources(TestApp.class)
.properties(String.format("%s=%s", "solace.java.apiProperties.AUTHENTICATION_SCHEME", "AUTHENTICATION_SCHEME_OAUTH2"))
.run()) {
assertThat(context.isRunning()).isTrue();

assertThatThrownBy(() -> context.getBean(SolaceSessionOAuth2TokenProvider.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
assertThatThrownBy(
() -> context.getBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ solace:
reconnectRetries: 3
connectRetriesPerHost: 1
reconnectRetryWaitInMillis: 2000
oauth2ClientRegistrationId: my-oauth2-client