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";
/**
* Key to store bot oauth client.
*/
public static final String OAUTH_CLIENT_KEY = "OAuthClient";
/**
* The collection of middleware in the adapter's pipeline.
*/

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

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