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

Fixes Unauthorized error when calling ContinueConversation #905

Merged
merged 1 commit into from
Jan 15, 2021
Merged
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 @@ -286,10 +286,6 @@ public CompletableFuture<Void> continueConversation(
ConversationReference reference,
BotCallbackHandler callback
) {
if (StringUtils.isEmpty(botAppId)) {
throw new IllegalArgumentException("botAppId");
}

if (reference == null) {
throw new IllegalArgumentException("reference");
}
Expand All @@ -298,14 +294,14 @@ public CompletableFuture<Void> continueConversation(
throw new IllegalArgumentException("callback");
}

botAppId = botAppId == null ? "" : botAppId;

// Hand craft Claims Identity.
HashMap<String, String> claims = new HashMap<String, String>() {
{
// Adding claims for both Emulator and Channel.
put(AuthenticationConstants.AUDIENCE_CLAIM, botAppId);
put(AuthenticationConstants.APPID_CLAIM, botAppId);
}
};
// Adding claims for both Emulator and Channel.
HashMap<String, String> claims = new HashMap<String, String>();
claims.put(AuthenticationConstants.AUDIENCE_CLAIM, botAppId);
claims.put(AuthenticationConstants.APPID_CLAIM, botAppId);

ClaimsIdentity claimsIdentity = new ClaimsIdentity("ExternalBearer", claims);

String audience = getBotFrameworkOAuthScope();
Expand Down Expand Up @@ -382,12 +378,22 @@ public CompletableFuture<Void> continueConversation(
context.getTurnState().add(BOT_IDENTITY_KEY, claimsIdentity);
context.getTurnState().add(OAUTH_SCOPE_KEY, audience);

pipelineResult = createConnectorClient(
reference.getServiceUrl(), claimsIdentity, audience
).thenCompose(connectorClient -> {
context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient);
return runPipeline(context, callback);
});
String appIdFromClaims = JwtTokenValidation.getAppIdFromClaims(claimsIdentity.claims());
return credentialProvider.isValidAppId(appIdFromClaims)
.thenCompose(isValidAppId -> {
// If we receive a valid app id in the incoming token claims, add the
// channel service URL to the trusted services list so we can send messages back.
if (!StringUtils.isEmpty(appIdFromClaims) && isValidAppId) {
AppCredentials.trustServiceUrl(reference.getServiceUrl());
}

return createConnectorClient(
reference.getServiceUrl(), claimsIdentity, audience
).thenCompose(connectorClient -> {
context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient);
return runPipeline(context, callback);
});
});
} catch (Exception e) {
pipelineResult.completeExceptionally(e);
}
Expand Down