Skip to content

Commit 3fbdcea

Browse files
committed
Implemented token fetch using CloudSDK
1 parent 9c24da7 commit 3fbdcea

File tree

6 files changed

+46
-280
lines changed

6 files changed

+46
-280
lines changed

cds-feature-advanced-event-mesh/src/main/java/com/sap/cds/feature/messaging/aem/client/binding/AemEndpointView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Optional<String> getUri() {
4141
* @return an {@link Optional} containing the AMQP URI if present, otherwise an
4242
* empty {@link Optional}.
4343
*/
44-
public Optional<String> getAmqpUriKey() {
44+
public Optional<String> getAmqpUri() {
4545
return Optional.ofNullable((String) getAemEndpoint().get(AMQP_URI_KEY));
4646
}
4747

@@ -75,4 +75,4 @@ private Map<String, Object> getAemEndpoint() {
7575
}
7676
}
7777

78-
}
78+
}

cds-feature-advanced-event-mesh/src/main/java/com/sap/cds/feature/messaging/aem/client/binding/AemTokenFetchClient.java

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package com.sap.cds.feature.messaging.aem.jms;
22

3-
import java.io.IOException;
43
import java.net.URI;
54
import java.util.Map;
5+
import java.util.Optional;
66
import java.util.function.BiFunction;
77

88
import org.apache.qpid.jms.JmsConnectionExtensions;
99
import org.apache.qpid.jms.JmsConnectionFactory;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
12-
13-
import com.sap.cds.feature.messaging.aem.client.binding.AemAuthorizationServiceView;
1412
import com.sap.cds.feature.messaging.aem.client.binding.AemEndpointView;
15-
import com.sap.cds.feature.messaging.aem.client.binding.AemTokenFetchClient;
13+
import com.sap.cds.feature.messaging.aem.client.binding.AemOauth2PropertySupplier;
1614
import com.sap.cds.services.ServiceException;
1715
import com.sap.cds.services.messaging.jms.BrokerConnection;
1816
import com.sap.cds.services.messaging.jms.BrokerConnectionProvider;
1917
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
18+
import com.sap.cloud.sdk.cloudplatform.connectivity.AuthenticationType;
19+
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
20+
import com.sap.cloud.sdk.cloudplatform.connectivity.OAuth2DestinationBuilder;
21+
import com.sap.cloud.sdk.cloudplatform.connectivity.OAuth2PropertySupplier;
22+
import com.sap.cloud.sdk.cloudplatform.connectivity.OnBehalfOf;
23+
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
24+
import com.sap.cloud.security.xsuaa.http.HttpHeaders;
2025

2126
import jakarta.jms.Connection;
2227

@@ -28,34 +33,36 @@ public class AemMessagingConnectionProvider extends BrokerConnectionProvider {
2833

2934
private final ServiceBinding binding;
3035
private final AemEndpointView endpointView;
31-
private final AemTokenFetchClient tokenFetchClient;
36+
private final Destination destination;
3237

3338
public AemMessagingConnectionProvider(ServiceBinding binding) {
3439
super(binding.getName().get());
3540
this.binding = binding;
3641
this.endpointView = new AemEndpointView(binding);
37-
this.tokenFetchClient = new AemTokenFetchClient(new AemAuthorizationServiceView(binding));
42+
OAuth2PropertySupplier supplier = new AemOauth2PropertySupplier(ServiceBindingDestinationOptions.forService(binding).build());
43+
this.destination = OAuth2DestinationBuilder
44+
.forTargetUrl(this.endpointView.getAmqpUri().get())
45+
.withTokenEndpoint(supplier.getTokenUri().toString())
46+
.withClient(supplier.getClientIdentity(), OnBehalfOf.TECHNICAL_USER_CURRENT_TENANT)
47+
.authenticationType(AuthenticationType.OAUTH2_CLIENT_CREDENTIALS)
48+
.build();
3849
}
3950

4051
@Override
4152
protected BrokerConnection createBrokerConnection(String name, Map<String, String> clientProperties) throws Exception {
4253
// see https://solace.community/discussion/1677/how-oauth-can-be-used-with-apache-qpid-jms-2-0-amqp
4354
logger.debug("Retrieving credentials for Basic Auth from service binding '{}'", binding.getName().get());
4455

45-
String amqpUri = this.endpointView.getAmqpUriKey().orElseThrow(() -> new ServiceException(
56+
String amqpUri = this.endpointView.getAmqpUri().orElseThrow(() -> new ServiceException(
4657
"AMQP URI key is missing in the service binding. Please check the service binding configuration."));
4758
amqpUri = amqpUri + SASL_MECHANISM_URI_PARAMETER;
4859

4960
final BiFunction<Connection, URI, Object> tokenExtension = new BiFunction<>() {
5061
@Override
5162
public Object apply(final Connection connection, final URI uri) {
52-
try {
53-
String token = tokenFetchClient.fetchToken().orElseThrow(() -> new ServiceException("Token is missing"));
63+
String token = fetchToken().orElseThrow(() -> new ServiceException("Token is missing"));
5464

55-
return token;
56-
} catch (IOException e) {
57-
throw new ServiceException(e);
58-
}
65+
return token;
5966
}
6067
};
6168

@@ -68,4 +75,24 @@ public Object apply(final Connection connection, final URI uri) {
6875
return new BrokerConnection(name, factory);
6976
}
7077

78+
private Optional<String> fetchToken() {
79+
Optional<String> token = this.destination.asHttp()
80+
.getHeaders()
81+
.stream()
82+
.filter(h -> h.getName().equals(HttpHeaders.AUTHORIZATION))
83+
.findFirst()
84+
.map(h -> getToken(h.getValue()));
85+
return token;
86+
}
87+
88+
private String getToken(String value) {
89+
String token = null;
90+
if (value != null) {
91+
String[] parts = value.split(" ");
92+
token = parts.length > 1 ? parts[1] : null;
93+
}
94+
95+
return token;
96+
}
97+
7198
}

cds-feature-advanced-event-mesh/src/test/java/com/sap/cds/feature/messaging/aem/client/binding/AemEndpointViewTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ void testGetUri_NotPresent() {
4949
}
5050

5151
@Test
52-
void testGetAmqpUriKey() {
52+
void testGetAmqpUri() {
5353
Map<String, Object> credentials = Map.of(
5454
"endpoints", Map.of(
5555
"advanced-event-mesh", Map.of("amqp_uri", "amqp://example.com")
5656
)
5757
);
5858
when(serviceBinding.getCredentials()).thenReturn(credentials);
5959

60-
Optional<String> amqpUri = endpointView.getAmqpUriKey();
60+
Optional<String> amqpUri = endpointView.getAmqpUri();
6161
assertTrue(amqpUri.isPresent());
6262
assertEquals("amqp://example.com", amqpUri.get());
6363
}
6464

6565
@Test
66-
void testGetAmqpUriKey_NotPresent() {
66+
void testGetAmqpUri_NotPresent() {
6767
when(serviceBinding.getCredentials()).thenReturn(Map.of());
6868

69-
Optional<String> amqpUri = endpointView.getAmqpUriKey();
69+
Optional<String> amqpUri = endpointView.getAmqpUri();
7070
assertFalse(amqpUri.isPresent());
7171
}
7272

cds-feature-advanced-event-mesh/src/test/java/com/sap/cds/feature/messaging/aem/client/binding/AemTokenFetchClientTest.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)