* Added sendEmulateOAuthCards

* Removed unused imports

Co-authored-by: Lee Parrish <30470292+LeeParrishMSFT@users.noreply.github.com>
This commit is contained in:
tracyboehrer 2021-03-12 14:35:24 -06:00 коммит произвёл GitHub
Родитель 2574aa61f5
Коммит 06c3fdc0ed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 74 добавлений и 35 удалений

Просмотреть файл

@ -49,6 +49,7 @@ import com.microsoft.bot.schema.TokenResponse;
import com.microsoft.bot.schema.TokenStatus;
import com.microsoft.bot.restclient.retry.RetryStrategy;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang3.StringUtils;
import java.net.HttpURLConnection;
@ -1069,41 +1070,47 @@ public class BotFrameworkAdapter extends BotAdapter implements
* If null, the default credentials will be used.
* @return An OAuth client for the bot.
*/
protected CompletableFuture<OAuthClient> createOAuthAPIClient(TurnContext turnContext,
AppCredentials oAuthAppCredentials) {
protected CompletableFuture<OAuthClient> createOAuthAPIClient(
TurnContext turnContext,
AppCredentials oAuthAppCredentials
) {
if (!OAuthClientConfig.emulateOAuthCards
&& StringUtils.equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR)
&& credentialProvider.isAuthenticationDisabled().join()) {
&& credentialProvider.isAuthenticationDisabled().join()
) {
OAuthClientConfig.emulateOAuthCards = true;
}
AtomicBoolean sendEmulateOAuthCards = new AtomicBoolean(false);
String appId = getBotAppId(turnContext);
String cacheKey = appId + (oAuthAppCredentials != null ? oAuthAppCredentials.getAppId() : "");
String oAuthScope = getBotFrameworkOAuthScope();
AppCredentials credentials = oAuthAppCredentials != null ? oAuthAppCredentials
: getAppCredentials(appId, oAuthScope).join();
OAuthClient client = oAuthClients.computeIfAbsent(cacheKey, key -> {
OAuthClient oAuthClient = new RestOAuthClient(
OAuthClientConfig.emulateOAuthCards ? turnContext.getActivity().getServiceUrl()
: OAuthClientConfig.OAUTHENDPOINT,
credentials);
sendEmulateOAuthCards.set(OAuthClientConfig.emulateOAuthCards);
if (OAuthClientConfig.emulateOAuthCards) {
// do not join task - we want this to run in the background.
OAuthClientConfig.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards);
}
String oAuthScope = getBotFrameworkOAuthScope();
AppCredentials credentials = oAuthAppCredentials != null
? oAuthAppCredentials
: getAppCredentials(appId, oAuthScope).join();
return oAuthClient;
return new RestOAuthClient(
OAuthClientConfig.emulateOAuthCards
? turnContext.getActivity().getServiceUrl()
: OAuthClientConfig.OAUTHENDPOINT,
credentials
);
});
// adding the oAuthClient into the TurnState
// TokenResolver.cs will use it get the correct credentials to poll for
// token for streaming scenario
if (turnContext.getTurnState().get(BotAdapter.OAUTH_CLIENT_KEY) == null) {
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, client);
}
if (sendEmulateOAuthCards.get()) {
return client.getUserToken().sendEmulateOAuthCards(true)
.thenApply(voidresult -> client);
}
return CompletableFuture.completedFuture(client);
}

Просмотреть файл

@ -4,9 +4,6 @@
package com.microsoft.bot.connector;
import com.microsoft.bot.connector.authentication.AuthenticationConstants;
import org.apache.commons.lang3.NotImplementedException;
import java.util.concurrent.CompletableFuture;
/**
* OAuthClient config.
@ -27,19 +24,4 @@ public final class OAuthClientConfig {
*/
@SuppressWarnings("checkstyle:VisibilityModifier")
public static boolean emulateOAuthCards = false;
/**
* Send a dummy OAuth card when the bot is being used on the Emulator for
* testing without fetching a real token.
*
* @param client The OAuth client.
* @param emulate Indicates whether the Emulator should emulate the OAuth card.
* @return A task that represents the work queued to execute.
*/
public static CompletableFuture<Void> sendEmulateOAuthCards(
OAuthClient client,
boolean emulate
) {
throw new NotImplementedException("sendEmulateOAuthCards");
}
}

Просмотреть файл

@ -129,4 +129,12 @@ public interface UserToken {
String channelId,
String include
);
/**
* Send a dummy OAuth card when the bot is being used on the Emulator for testing without fetching a real token.
*
* @param emulateOAuthCards Indicates whether the Emulator should emulate the OAuth card.
* @return A task that represents the work queued to execute.
*/
CompletableFuture<Void> sendEmulateOAuthCards(boolean emulateOAuthCards);
}

Просмотреть файл

@ -112,6 +112,13 @@ public class RestUserToken implements UserToken {
@Query("channelId") String channelId,
@Query("include") String include
);
@Headers({ "Content-Type: application/json; charset=utf-8",
"x-ms-logging-context: com.microsoft.bot.schema.UserTokens sendEmulateOAuthCards" })
@POST("api/usertoken/emulateOAuthCards")
CompletableFuture<Response<ResponseBody>> sendEmulateOAuthCards(
@Query("emulate") boolean emulate
);
}
/**
@ -532,4 +539,39 @@ public class RestUserToken implements UserToken {
.registerError(ErrorResponseException.class)
.build(response);
}
/**
* Send a dummy OAuth card when the bot is being used on the Emulator for testing without fetching a real token.
*
* @param emulateOAuthCards Indicates whether the Emulator should emulate the OAuth card.
* @return A task that represents the work queued to execute.
*/
@Override
public CompletableFuture<Void> sendEmulateOAuthCards(boolean emulateOAuthCards) {
return service.sendEmulateOAuthCards(emulateOAuthCards)
.thenApply(responseBodyResponse -> {
try {
return sendEmulateOAuthCardsDelegate(responseBodyResponse).body();
} catch (ErrorResponseException e) {
throw e;
} catch (Throwable t) {
throw new ErrorResponseException("sendEmulateOAuthCards", responseBodyResponse);
}
});
}
private ServiceResponse<Void> sendEmulateOAuthCardsDelegate(
Response<ResponseBody> response
) throws ErrorResponseException, IOException, IllegalArgumentException {
return client.restClient()
.responseBuilderFactory()
.<Void, ErrorResponseException>newInstance(client.serializerAdapter())
.register(HttpURLConnection.HTTP_OK, new TypeToken<Void>() {
}.getType())
.register(HttpURLConnection.HTTP_ACCEPTED, new TypeToken<Void>() {
}.getType())
.registerError(ErrorResponseException.class)
.build(response);
}
}