Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 06c3fdc

Browse files
Added sendEmulateOAuthCards (#1053)
* Added sendEmulateOAuthCards * Removed unused imports Co-authored-by: Lee Parrish <[email protected]>
1 parent 2574aa6 commit 06c3fdc

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.microsoft.bot.schema.TokenStatus;
5050
import com.microsoft.bot.restclient.retry.RetryStrategy;
5151
import java.util.Collections;
52+
import java.util.concurrent.atomic.AtomicBoolean;
5253
import org.apache.commons.lang3.StringUtils;
5354

5455
import java.net.HttpURLConnection;
@@ -1069,41 +1070,47 @@ public CompletableFuture<Void> createConversation(String channelId, String servi
10691070
* If null, the default credentials will be used.
10701071
* @return An OAuth client for the bot.
10711072
*/
1072-
protected CompletableFuture<OAuthClient> createOAuthAPIClient(TurnContext turnContext,
1073-
AppCredentials oAuthAppCredentials) {
1073+
protected CompletableFuture<OAuthClient> createOAuthAPIClient(
1074+
TurnContext turnContext,
1075+
AppCredentials oAuthAppCredentials
1076+
) {
10741077
if (!OAuthClientConfig.emulateOAuthCards
10751078
&& StringUtils.equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR)
1076-
&& credentialProvider.isAuthenticationDisabled().join()) {
1079+
&& credentialProvider.isAuthenticationDisabled().join()
1080+
) {
10771081
OAuthClientConfig.emulateOAuthCards = true;
10781082
}
1083+
AtomicBoolean sendEmulateOAuthCards = new AtomicBoolean(false);
10791084

10801085
String appId = getBotAppId(turnContext);
10811086
String cacheKey = appId + (oAuthAppCredentials != null ? oAuthAppCredentials.getAppId() : "");
1082-
String oAuthScope = getBotFrameworkOAuthScope();
1083-
AppCredentials credentials = oAuthAppCredentials != null ? oAuthAppCredentials
1084-
: getAppCredentials(appId, oAuthScope).join();
10851087

10861088
OAuthClient client = oAuthClients.computeIfAbsent(cacheKey, key -> {
1087-
OAuthClient oAuthClient = new RestOAuthClient(
1088-
OAuthClientConfig.emulateOAuthCards ? turnContext.getActivity().getServiceUrl()
1089-
: OAuthClientConfig.OAUTHENDPOINT,
1090-
credentials);
1091-
1092-
if (OAuthClientConfig.emulateOAuthCards) {
1093-
// do not join task - we want this to run in the background.
1094-
OAuthClientConfig.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards);
1095-
}
1089+
sendEmulateOAuthCards.set(OAuthClientConfig.emulateOAuthCards);
1090+
1091+
String oAuthScope = getBotFrameworkOAuthScope();
1092+
AppCredentials credentials = oAuthAppCredentials != null
1093+
? oAuthAppCredentials
1094+
: getAppCredentials(appId, oAuthScope).join();
10961095

1097-
return oAuthClient;
1096+
return new RestOAuthClient(
1097+
OAuthClientConfig.emulateOAuthCards
1098+
? turnContext.getActivity().getServiceUrl()
1099+
: OAuthClientConfig.OAUTHENDPOINT,
1100+
credentials
1101+
);
10981102
});
10991103

11001104
// adding the oAuthClient into the TurnState
1101-
// TokenResolver.cs will use it get the correct credentials to poll for
1102-
// token for streaming scenario
11031105
if (turnContext.getTurnState().get(BotAdapter.OAUTH_CLIENT_KEY) == null) {
11041106
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, client);
11051107
}
11061108

1109+
if (sendEmulateOAuthCards.get()) {
1110+
return client.getUserToken().sendEmulateOAuthCards(true)
1111+
.thenApply(voidresult -> client);
1112+
}
1113+
11071114
return CompletableFuture.completedFuture(client);
11081115
}
11091116

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/OAuthClientConfig.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
package com.microsoft.bot.connector;
55

66
import com.microsoft.bot.connector.authentication.AuthenticationConstants;
7-
import org.apache.commons.lang3.NotImplementedException;
8-
9-
import java.util.concurrent.CompletableFuture;
107

118
/**
129
* OAuthClient config.
@@ -27,19 +24,4 @@ private OAuthClientConfig() {
2724
*/
2825
@SuppressWarnings("checkstyle:VisibilityModifier")
2926
public static boolean emulateOAuthCards = false;
30-
31-
/**
32-
* Send a dummy OAuth card when the bot is being used on the Emulator for
33-
* testing without fetching a real token.
34-
*
35-
* @param client The OAuth client.
36-
* @param emulate Indicates whether the Emulator should emulate the OAuth card.
37-
* @return A task that represents the work queued to execute.
38-
*/
39-
public static CompletableFuture<Void> sendEmulateOAuthCards(
40-
OAuthClient client,
41-
boolean emulate
42-
) {
43-
throw new NotImplementedException("sendEmulateOAuthCards");
44-
}
4527
}

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/UserToken.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,12 @@ CompletableFuture<List<TokenStatus>> getTokenStatus(
129129
String channelId,
130130
String include
131131
);
132+
133+
/**
134+
* Send a dummy OAuth card when the bot is being used on the Emulator for testing without fetching a real token.
135+
*
136+
* @param emulateOAuthCards Indicates whether the Emulator should emulate the OAuth card.
137+
* @return A task that represents the work queued to execute.
138+
*/
139+
CompletableFuture<Void> sendEmulateOAuthCards(boolean emulateOAuthCards);
132140
}

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/rest/RestUserToken.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ CompletableFuture<Response<ResponseBody>> getTokenStatus(
112112
@Query("channelId") String channelId,
113113
@Query("include") String include
114114
);
115+
116+
@Headers({ "Content-Type: application/json; charset=utf-8",
117+
"x-ms-logging-context: com.microsoft.bot.schema.UserTokens sendEmulateOAuthCards" })
118+
@POST("api/usertoken/emulateOAuthCards")
119+
CompletableFuture<Response<ResponseBody>> sendEmulateOAuthCards(
120+
@Query("emulate") boolean emulate
121+
);
115122
}
116123

117124
/**
@@ -532,4 +539,39 @@ private ServiceResponse<List<TokenStatus>> getTokenStatusDelegate(
532539
.registerError(ErrorResponseException.class)
533540
.build(response);
534541
}
542+
543+
/**
544+
* Send a dummy OAuth card when the bot is being used on the Emulator for testing without fetching a real token.
545+
*
546+
* @param emulateOAuthCards Indicates whether the Emulator should emulate the OAuth card.
547+
* @return A task that represents the work queued to execute.
548+
*/
549+
@Override
550+
public CompletableFuture<Void> sendEmulateOAuthCards(boolean emulateOAuthCards) {
551+
return service.sendEmulateOAuthCards(emulateOAuthCards)
552+
.thenApply(responseBodyResponse -> {
553+
try {
554+
return sendEmulateOAuthCardsDelegate(responseBodyResponse).body();
555+
} catch (ErrorResponseException e) {
556+
throw e;
557+
} catch (Throwable t) {
558+
throw new ErrorResponseException("sendEmulateOAuthCards", responseBodyResponse);
559+
}
560+
});
561+
}
562+
563+
private ServiceResponse<Void> sendEmulateOAuthCardsDelegate(
564+
Response<ResponseBody> response
565+
) throws ErrorResponseException, IOException, IllegalArgumentException {
566+
567+
return client.restClient()
568+
.responseBuilderFactory()
569+
.<Void, ErrorResponseException>newInstance(client.serializerAdapter())
570+
.register(HttpURLConnection.HTTP_OK, new TypeToken<Void>() {
571+
}.getType())
572+
.register(HttpURLConnection.HTTP_ACCEPTED, new TypeToken<Void>() {
573+
}.getType())
574+
.registerError(ErrorResponseException.class)
575+
.build(response);
576+
}
535577
}

0 commit comments

Comments
 (0)