-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java support for DefaultAzureCredential in Service Bus (#3)
* Java support for DefaultAzureCredential in Service Bus * Moving to a different namespace
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
java/com/jongallant/azure/identity/extensions/DefaultAzureServiceBusCredentialTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |