Skip to content

Commit

Permalink
Java support for DefaultAzureCredential in Service Bus (#3)
Browse files Browse the repository at this point in the history
* Java support for DefaultAzureCredential in Service Bus

* Moving to a different namespace
  • Loading branch information
srnagar authored and jongio committed Jan 28, 2020
1 parent e4bde29 commit 4ecde2e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<SecurityToken> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 4ecde2e

Please sign in to comment.