diff --git a/sdk/communication/azure-communication-chat/src/main/java/com/azure/communication/chat/ChatClientBuilder.java b/sdk/communication/azure-communication-chat/src/main/java/com/azure/communication/chat/ChatClientBuilder.java index c69b774914642..3b061256d7bfa 100644 --- a/sdk/communication/azure-communication-chat/src/main/java/com/azure/communication/chat/ChatClientBuilder.java +++ b/sdk/communication/azure-communication-chat/src/main/java/com/azure/communication/chat/ChatClientBuilder.java @@ -4,6 +4,7 @@ package com.azure.communication.chat; import com.azure.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder; +import com.azure.communication.common.CommunicationTokenCredential; import com.azure.communication.common.CommunicationUserCredential; import java.util.ArrayList; @@ -14,6 +15,7 @@ import com.azure.core.http.HttpClient; import com.azure.core.http.HttpPipeline; import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; import com.azure.core.http.policy.CookiePolicy; import com.azure.core.http.policy.HttpLogOptions; import com.azure.core.http.policy.HttpLoggingPolicy; @@ -166,9 +168,10 @@ public ChatAsyncClient buildAsyncClient() { } else { Objects.requireNonNull(communicationUserCredential); Objects.requireNonNull(httpClient); + CommunicationTokenCredential tokenCredential = new CommunicationTokenCredential(communicationUserCredential); pipeline = createHttpPipeline(httpClient, - new ChatUserCredentialPolicy(communicationUserCredential), + new BearerTokenAuthenticationPolicy(tokenCredential, ""), customPolicies); } diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java new file mode 100644 index 0000000000000..1339fe95986e2 --- /dev/null +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/CommunicationTokenCredential.java @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.common; + +import java.util.concurrent.ExecutionException; + +import com.azure.core.credential.AccessToken; +import com.azure.core.credential.TokenCredential; +import com.azure.core.credential.TokenRequestContext; + +import reactor.core.publisher.Mono; + +public class CommunicationTokenCredential implements TokenCredential { + private CommunicationUserCredential credential; + + public CommunicationTokenCredential(CommunicationUserCredential communicationUserCrendential){ + credential = communicationUserCrendential; + } + + @Override + public Mono getToken(TokenRequestContext request){ + try{ + return Mono.just(credential.getToken().get()); + } + catch (InterruptedException ex) { + return Mono.error(ex); + } catch (ExecutionException ex) { + return Mono.error(ex); + } + } +}