Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ public CompletableFuture<Void> continueConversation(
BotCallbackHandler callback
) {
return continueConversation(
claimsIdentity,
reference,
getBotFrameworkOAuthScope(),
callback
claimsIdentity, reference, getBotFrameworkOAuthScope(), callback
);
}

Expand Down Expand Up @@ -380,9 +377,7 @@ public CompletableFuture<Void> continueConversation(
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);
Expand Down Expand Up @@ -430,11 +425,7 @@ public CompletableFuture<InvokeResponse> processActivity(
BotAssert.activityNotNull(activity);

return JwtTokenValidation.authenticateRequest(
activity,
authHeader,
credentialProvider,
channelProvider,
authConfiguration
activity, authHeader, credentialProvider, channelProvider, authConfiguration
).thenCompose(claimsIdentity -> processActivity(claimsIdentity, activity, callback));
}

Expand Down Expand Up @@ -897,7 +888,7 @@ public CompletableFuture<TokenResponse> getUserToken(
|| StringUtils.isEmpty(context.getActivity().getFrom().getId())
) {
throw new IllegalArgumentException(
"BotFrameworkAdapter.GetuserToken(): missing from or from.id"
"BotFrameworkAdapter.getUserToken(): missing from or from.id"
);
}

Expand All @@ -908,10 +899,8 @@ public CompletableFuture<TokenResponse> getUserToken(
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
);
});
}
Expand Down Expand Up @@ -1053,8 +1042,7 @@ public CompletableFuture<Void> signOutUser(
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);
Expand Down Expand Up @@ -1269,11 +1257,7 @@ public CompletableFuture<Void> createConversation(
}

return createConversation(
channelId,
serviceUrl,
credentials,
conversationParameters,
callback
channelId, serviceUrl, credentials, conversationParameters, callback
);
}

Expand All @@ -1300,7 +1284,6 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
.equalsIgnoreCase(turnContext.getActivity().getChannelId(), Channels.EMULATOR)
&& credentialProvider.isAuthenticationDisabled().join()
) {

OAuthClientConfig.emulateOAuthCards = true;
}

Expand All @@ -1310,17 +1293,26 @@ protected CompletableFuture<OAuthClient> createOAuthClient(
? 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);
}

/**
Expand Down Expand Up @@ -1394,8 +1386,7 @@ protected CompletableFuture<ConnectorClient> getOrCreateConnectorClient(
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
);

Expand Down Expand Up @@ -1553,15 +1544,17 @@ public CompletableFuture<Void> onTurn(TurnContext turnContext, NextDelegate next
}

/**
* Get the AppCredentials cache. For unit testing.
* Get the AppCredentials cache. For unit testing.
*
* @return The AppCredentials cache.
*/
protected Map<String, AppCredentials> getCredentialsCache() {
return Collections.unmodifiableMap(appCredentialMap);
}

/**
* Get the ConnectorClient cache. For unit testing.
* Get the ConnectorClient cache. For unit testing.
*
* @return The ConnectorClient cache.
*/
protected Map<String, ConnectorClient> getConnectorClientCache() {
Expand Down