From 4ecde2e11749ac090d33fef8a4f9c600bab3d3de Mon Sep 17 00:00:00 2001 From: Srikanta <51379715+srnagar@users.noreply.github.com> Date: Tue, 28 Jan 2020 13:27:28 -0800 Subject: [PATCH] Java support for DefaultAzureCredential in Service Bus (#3) * Java support for DefaultAzureCredential in Service Bus * Moving to a different namespace --- .../DefaultAzureServiceBusCredential.java | 36 +++++++++++++++++++ .../DefaultAzureServiceBusCredentialTest.java | 19 ++++++++++ 2 files changed, 55 insertions(+) create mode 100644 java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredential.java create mode 100644 java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredentialTest.java diff --git a/java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredential.java b/java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredential.java new file mode 100644 index 0000000..0ce0a6d --- /dev/null +++ b/java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredential.java @@ -0,0 +1,36 @@ +package com.jongallant.azure.identity.extensions; + +import com.azure.core.credential.TokenRequestContext; +import com.azure.identity.DefaultAzureCredential; +import com.azure.identity.DefaultAzureCredentialBuilder; +import reactor.core.publisher.Mono; + +import java.time.Instant; +import java.util.concurrent.CompletableFuture; + +public class DefaultAzureServiceBusCredential extends TokenProvider { + + private final DefaultAzureCredential defaultAzureCredential; + + public DefaultAzureServiceBusCredential() { + this(new DefaultAzureCredentialBuilder().build()); + } + + public DefaultAzureServiceBusCredential(DefaultAzureCredential defaultAzureCredential) { + this.defaultAzureCredential = defaultAzureCredential; + } + + @Override + public CompletableFuture getSecurityTokenAsync(String audience) { + + String scope = "https://servicebus.azure.net/.default"; + TokenRequestContext tokenRequestContext = new TokenRequestContext() + .addScopes(scope); + + return defaultAzureCredential + .getToken(tokenRequestContext) + .flatMap(accessToken -> Mono.just(new SecurityToken(SecurityTokenType.JWT, audience, accessToken.getToken(), + Instant.now(), accessToken.getExpiresAt().toInstant()))).toFuture(); + } + +} diff --git a/java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredentialTest.java b/java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredentialTest.java new file mode 100644 index 0000000..ec26707 --- /dev/null +++ b/java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredentialTest.java @@ -0,0 +1,19 @@ +package com.jongallant.azure.identity.extensions; + +import com.azure.identity.DefaultAzureCredential; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.microsoft.azure.servicebus.primitives.ServiceBusException; +import com.microsoft.azure.servicebus.security.DefaultAzureServiceBusCredential; +import org.junit.Test; + +public class DefaultAzureServiceBusCredentialTest { + + @Test + public void testDefaultCredential() throws ServiceBusException, InterruptedException { + DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build(); + TopicClient topicClient = new TopicClient("jongsb2", "srnagartopic", + new ClientSettings(new DefaultAzureServiceBusCredential())); + + topicClient.send(new Message("hello")); + } +}