Adding OAuthClient to TurnState

This commit is contained in:
tracyboehrer 2020-04-23 15:00:02 -05:00
Родитель fb21b86d90
Коммит 67dfca1639
2 изменённых файлов: 31 добавлений и 33 удалений

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

@ -44,6 +44,11 @@ public abstract class BotAdapter {
*/ */
public static final String OAUTH_SCOPE_KEY = "Microsoft.Bot.Builder.BotAdapter.OAuthScope"; public static final String OAUTH_SCOPE_KEY = "Microsoft.Bot.Builder.BotAdapter.OAuthScope";
/**
* Key to store bot oauth client.
*/
public static final String OAUTH_CLIENT_KEY = "OAuthClient";
/** /**
* The collection of middleware in the adapter's pipeline. * The collection of middleware in the adapter's pipeline.
*/ */

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

@ -327,10 +327,7 @@ public class BotFrameworkAdapter extends BotAdapter
BotCallbackHandler callback BotCallbackHandler callback
) { ) {
return continueConversation( return continueConversation(
claimsIdentity, claimsIdentity, reference, getBotFrameworkOAuthScope(), callback
reference,
getBotFrameworkOAuthScope(),
callback
); );
} }
@ -380,9 +377,7 @@ public class BotFrameworkAdapter extends BotAdapter
context.getTurnState().add(OAUTH_SCOPE_KEY, audience); context.getTurnState().add(OAUTH_SCOPE_KEY, audience);
pipelineResult = createConnectorClient( pipelineResult = createConnectorClient(
reference.getServiceUrl(), reference.getServiceUrl(), claimsIdentity, audience
claimsIdentity,
audience
).thenCompose(connectorClient -> { ).thenCompose(connectorClient -> {
context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient); context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient);
return runPipeline(context, callback); return runPipeline(context, callback);
@ -430,11 +425,7 @@ public class BotFrameworkAdapter extends BotAdapter
BotAssert.activityNotNull(activity); BotAssert.activityNotNull(activity);
return JwtTokenValidation.authenticateRequest( return JwtTokenValidation.authenticateRequest(
activity, activity, authHeader, credentialProvider, channelProvider, authConfiguration
authHeader,
credentialProvider,
channelProvider,
authConfiguration
).thenCompose(claimsIdentity -> processActivity(claimsIdentity, activity, callback)); ).thenCompose(claimsIdentity -> processActivity(claimsIdentity, activity, callback));
} }
@ -897,7 +888,7 @@ public class BotFrameworkAdapter extends BotAdapter
|| StringUtils.isEmpty(context.getActivity().getFrom().getId()) || StringUtils.isEmpty(context.getActivity().getFrom().getId())
) { ) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"BotFrameworkAdapter.GetuserToken(): missing from or from.id" "BotFrameworkAdapter.getUserToken(): missing from or from.id"
); );
} }
@ -908,10 +899,8 @@ public class BotFrameworkAdapter extends BotAdapter
return createOAuthClient(context, null).thenCompose(oAuthClient -> { return createOAuthClient(context, null).thenCompose(oAuthClient -> {
return oAuthClient.getUserToken() return oAuthClient.getUserToken()
.getToken( .getToken(
context.getActivity().getFrom().getId(), context.getActivity().getFrom().getId(), connectionName,
connectionName, context.getActivity().getChannelId(), magicCode
context.getActivity().getChannelId(),
magicCode
); );
}); });
} }
@ -1053,8 +1042,7 @@ public class BotFrameworkAdapter extends BotAdapter
return createOAuthClient(context, null).thenCompose(oAuthClient -> { return createOAuthClient(context, null).thenCompose(oAuthClient -> {
return oAuthClient.getUserToken() return oAuthClient.getUserToken()
.signOut( .signOut(
context.getActivity().getFrom().getId(), context.getActivity().getFrom().getId(), connectionName,
connectionName,
context.getActivity().getChannelId() context.getActivity().getChannelId()
); );
}).thenApply(signOutResult -> null); }).thenApply(signOutResult -> null);
@ -1269,11 +1257,7 @@ public class BotFrameworkAdapter extends BotAdapter
} }
return createConversation( return createConversation(
channelId, channelId, serviceUrl, credentials, conversationParameters, callback
serviceUrl,
credentials,
conversationParameters,
callback
); );
} }
@ -1300,7 +1284,6 @@ public class BotFrameworkAdapter extends BotAdapter
.equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR) .equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR)
&& credentialProvider.isAuthenticationDisabled().join() && credentialProvider.isAuthenticationDisabled().join()
) { ) {
OAuthClientConfig.emulateOAuthCards = true; OAuthClientConfig.emulateOAuthCards = true;
} }
@ -1310,17 +1293,26 @@ public class BotFrameworkAdapter extends BotAdapter
? oAuthAppCredentials ? oAuthAppCredentials
: getAppCredentials(appId, oAuthScope).join(); : getAppCredentials(appId, oAuthScope).join();
OAuthClient oAuthClient = 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
turnContext.getTurnState().add(BotAdapter.OAUTH_CLIENT_KEY, oAuthClient);
if (OAuthClientConfig.emulateOAuthCards) { if (OAuthClientConfig.emulateOAuthCards) {
// do not join task - we want this to run in the background. // do not join task - we want this to run in the background.
OAuthClient oAuthClient =
new RestOAuthClient(turnContext.getActivity().getServiceUrl(), credentials);
return OAuthClientConfig return OAuthClientConfig
.sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards) .sendEmulateOAuthCards(oAuthClient, OAuthClientConfig.emulateOAuthCards)
.thenApply(result -> oAuthClient); .thenApply(result -> oAuthClient);
} }
return CompletableFuture return CompletableFuture.completedFuture(oAuthClient);
.completedFuture(new RestOAuthClient(OAuthClientConfig.OAUTHENDPOINT, credentials));
} }
/** /**
@ -1394,8 +1386,7 @@ public class BotFrameworkAdapter extends BotAdapter
CompletableFuture<ConnectorClient> result = new CompletableFuture<>(); CompletableFuture<ConnectorClient> result = new CompletableFuture<>();
String clientKey = keyForConnectorClient( String clientKey = keyForConnectorClient(
serviceUrl, serviceUrl, usingAppCredentials != null ? usingAppCredentials.getAppId() : null,
usingAppCredentials != null ? usingAppCredentials.getAppId() : null,
usingAppCredentials != null ? usingAppCredentials.oAuthScope() : null usingAppCredentials != null ? usingAppCredentials.oAuthScope() : null
); );
@ -1553,7 +1544,8 @@ public class BotFrameworkAdapter extends BotAdapter
} }
/** /**
* Get the AppCredentials cache. For unit testing. * Get the AppCredentials cache. For unit testing.
*
* @return The AppCredentials cache. * @return The AppCredentials cache.
*/ */
protected Map<String, AppCredentials> getCredentialsCache() { protected Map<String, AppCredentials> getCredentialsCache() {
@ -1561,7 +1553,8 @@ public class BotFrameworkAdapter extends BotAdapter
} }
/** /**
* Get the ConnectorClient cache. For unit testing. * Get the ConnectorClient cache. For unit testing.
*
* @return The ConnectorClient cache. * @return The ConnectorClient cache.
*/ */
protected Map<String, ConnectorClient> getConnectorClientCache() { protected Map<String, ConnectorClient> getConnectorClientCache() {