Repository: SAP/cloud-security-xsuaa-integration
Pull request #806 (2022-02-01 18:27:35)

[Spring] Support relaxed-binding rules
Supports also non relaxed-binding rules for non spring framework cases

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding


Patch:
diff --git a/spring-xsuaa/src/main/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefault.java b/spring-xsuaa/src/main/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefault.java
index 0354b44e..111eadfa 100644
--- a/spring-xsuaa/src/main/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefault.java
+++ b/spring-xsuaa/src/main/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefault.java
@@ -41,7 +41,7 @@ public class XsuaaServiceConfigurationDefault implements XsuaaServiceConfigurati
 	@Value("${xsuaa.verificationkey:}")
 	private String verificationKey;
 
-	@Value("${xsuaa.credentialtype:#{null}}")
+	@Value("${xsuaa.credential-type:#{null}}")
 	private String credentialType;
 
 	@Value("${xsuaa.certurl:#{null}}")
diff --git a/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpoints.java b/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpoints.java
index f01fed85..7a478b65 100644
--- a/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpoints.java
+++ b/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpoints.java
@@ -104,6 +104,7 @@ public class XsuaaDefaultEndpoints implements OAuth2ServiceEndpointsProvider {
 
 	@Override
 	public URI getJwksUri() {
+		assertNotNull(baseUri, "XsuaaDefaultEndpoints.getJwksUri() requires baseUri not to be null.");
 		return expandPath(baseUri, KEYSET_ENDPOINT);
 	}
 
diff --git a/token-client/src/main/java/com/sap/cloud/security/xsuaa/util/UriUtil.java b/token-client/src/main/java/com/sap/cloud/security/xsuaa/util/UriUtil.java
index f751872e..85943a2b 100644
--- a/token-client/src/main/java/com/sap/cloud/security/xsuaa/util/UriUtil.java
+++ b/token-client/src/main/java/com/sap/cloud/security/xsuaa/util/UriUtil.java
@@ -69,7 +69,8 @@ public class UriUtil {
 	 */
 	// TODO rename to getUriWithPathAppended
 	@Nonnull
-	public static URI expandPath(URI baseUri, String pathToAppend) {
+	public static URI expandPath(@Nonnull URI baseUri, String pathToAppend) {
+		Assertions.assertNotNull(baseUri, "the baseUri parameter must not be null");
 		try {
 			String newPath = baseUri.getPath() + pathToAppend;
 			return new URI(baseUri.getScheme(), baseUri.getUserInfo(), baseUri.getHost(), baseUri.getPort(),


Test patch:
diff --git a/env/src/test/java/com/sap/cloud/security/config/OAuth2ServiceConfigurationBuilderTest.java b/env/src/test/java/com/sap/cloud/security/config/OAuth2ServiceConfigurationBuilderTest.java
index ec5a4d50..881adb07 100644
--- a/env/src/test/java/com/sap/cloud/security/config/OAuth2ServiceConfigurationBuilderTest.java
+++ b/env/src/test/java/com/sap/cloud/security/config/OAuth2ServiceConfigurationBuilderTest.java
@@ -5,6 +5,7 @@
  */
 package com.sap.cloud.security.config;
 
+import com.sap.cloud.security.xsuaa.client.XsuaaDefaultEndpoints;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -197,7 +198,31 @@ public class OAuth2ServiceConfigurationBuilderTest {
 		assertThat(configuration.getClientId()).isEqualTo("base-client-id");
 		assertThat(configuration.getClientSecret()).isEqualTo("base-client-secret");
 		assertThat(configuration.getUrl()).isEqualTo(URI.create("http://url.base"));
+		assertThat(configuration.getCertUrl()).isNull();
 		assertThat(configuration.getProperty("testing-key")).isEqualTo("base-value");
 		assertThat(configuration.getDomains()).isEqualTo(Collections.EMPTY_LIST);
 	}
+
+	@Test
+	public void x509IntegrationTest() {
+		String url = "http://the.cert.Url.org";
+
+		OAuth2ServiceConfiguration configuration = OAuth2ServiceConfigurationBuilder.forService(Service.XSUAA)
+				.withClientId("client-id")
+				.withCertUrl(url)
+				.withCertificate("cert")
+				.withPrivateKey("key")
+				.withCredentialType(CredentialType.X509)
+				.build();
+
+		assertThat(configuration.getClientId()).isEqualTo("client-id");
+		assertThat(configuration.getClientIdentity().getCertificate()).isEqualTo("cert");
+		assertThat(configuration.getClientIdentity().getKey()).isEqualTo("key");
+		assertThat(configuration.getUrl()).isNull();
+		assertThat(configuration.getCertUrl()).isEqualTo(URI.create(url));
+		assertThat(configuration.getCredentialType()).isEqualTo(CredentialType.X509);
+
+		XsuaaDefaultEndpoints endpoints = new XsuaaDefaultEndpoints(configuration);
+		assertThat(endpoints.getAuthorizeEndpoint().toString()).isEqualTo("http://the.cert.Url.org/oauth/authorize");
+	}
 }
\ No newline at end of file
diff --git a/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefaultTest.java b/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefaultTest.java
index 21d194c0..164d4e74 100644
--- a/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefaultTest.java
+++ b/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServiceConfigurationDefaultTest.java
@@ -15,7 +15,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @TestPropertySource(properties = { "xsuaa.clientid=client", "xsuaa.certificate=cert", "xsuaa.key=key",
-		"xsuaa.certurl=https://my.cert.authentication.sap.com", "xsuaa.credentialtype=x509" })
+		"xsuaa.certurl=https://my.cert.authentication.sap.com", "xsuaa.credential-type=x509" })
 @ContextConfiguration(classes = { XsuaaServiceConfigurationDefault.class })
 public class XsuaaServiceConfigurationDefaultTest {
 
diff --git a/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServicePropertySourceFactoryTest.java b/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServicePropertySourceFactoryTest.java
index 9221c39a..69ff7aca 100644
--- a/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServicePropertySourceFactoryTest.java
+++ b/spring-xsuaa/src/test/java/com/sap/cloud/security/xsuaa/XsuaaServicePropertySourceFactoryTest.java
@@ -82,7 +82,7 @@ class TestConfiguration {
 	@Value("${xsuaa.key:}")
 	public String key;
 
-	@Value("${xsuaa.credentialtype:}")
+	@Value("${xsuaa.credential-type:}")
 	public String credentialType;
 
 	@Value("${xsuaa.certurl:}")
diff --git a/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpointsTest.java b/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpointsTest.java
index e14572f7..7eab99d3 100644
--- a/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpointsTest.java
+++ b/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/XsuaaDefaultEndpointsTest.java
@@ -65,6 +65,17 @@ public class XsuaaDefaultEndpointsTest {
 		new XsuaaDefaultEndpoints(null, CERT_URL);
 	}
 
+	@Test(expected = IllegalArgumentException.class)
+	public void getJwksUri_throwsException_whenBaseUriIsNull() {
+		Mockito.when(oAuth2ServiceConfiguration.getCertUrl()).thenReturn(URI.create(CERT_URL));
+		Mockito.when(oAuth2ServiceConfiguration.getCredentialType()).thenReturn(X509);
+
+		cut = new XsuaaDefaultEndpoints(oAuth2ServiceConfiguration);
+
+		assertThat(cut.getTokenEndpoint().toString(), is(CERT_URL + "/oauth/token")); // ok
+		cut.getJwksUri(); // raise exception
+	}
+
 	@Test
 	@Deprecated
 	public void getEndpoint_forBaseUrl() {