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

Commit e4baf6f

Browse files
authored
Fixes Unauthorized error when calling ContinueConversation (#905)
1 parent b9ba6fe commit e4baf6f

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ public CompletableFuture<Void> continueConversation(
286286
ConversationReference reference,
287287
BotCallbackHandler callback
288288
) {
289-
if (StringUtils.isEmpty(botAppId)) {
290-
throw new IllegalArgumentException("botAppId");
291-
}
292-
293289
if (reference == null) {
294290
throw new IllegalArgumentException("reference");
295291
}
@@ -298,14 +294,14 @@ public CompletableFuture<Void> continueConversation(
298294
throw new IllegalArgumentException("callback");
299295
}
300296

297+
botAppId = botAppId == null ? "" : botAppId;
298+
301299
// Hand craft Claims Identity.
302-
HashMap<String, String> claims = new HashMap<String, String>() {
303-
{
304-
// Adding claims for both Emulator and Channel.
305-
put(AuthenticationConstants.AUDIENCE_CLAIM, botAppId);
306-
put(AuthenticationConstants.APPID_CLAIM, botAppId);
307-
}
308-
};
300+
// Adding claims for both Emulator and Channel.
301+
HashMap<String, String> claims = new HashMap<String, String>();
302+
claims.put(AuthenticationConstants.AUDIENCE_CLAIM, botAppId);
303+
claims.put(AuthenticationConstants.APPID_CLAIM, botAppId);
304+
309305
ClaimsIdentity claimsIdentity = new ClaimsIdentity("ExternalBearer", claims);
310306

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

385-
pipelineResult = createConnectorClient(
386-
reference.getServiceUrl(), claimsIdentity, audience
387-
).thenCompose(connectorClient -> {
388-
context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient);
389-
return runPipeline(context, callback);
390-
});
381+
String appIdFromClaims = JwtTokenValidation.getAppIdFromClaims(claimsIdentity.claims());
382+
return credentialProvider.isValidAppId(appIdFromClaims)
383+
.thenCompose(isValidAppId -> {
384+
// If we receive a valid app id in the incoming token claims, add the
385+
// channel service URL to the trusted services list so we can send messages back.
386+
if (!StringUtils.isEmpty(appIdFromClaims) && isValidAppId) {
387+
AppCredentials.trustServiceUrl(reference.getServiceUrl());
388+
}
389+
390+
return createConnectorClient(
391+
reference.getServiceUrl(), claimsIdentity, audience
392+
).thenCompose(connectorClient -> {
393+
context.getTurnState().add(CONNECTOR_CLIENT_KEY, connectorClient);
394+
return runPipeline(context, callback);
395+
});
396+
});
391397
} catch (Exception e) {
392398
pipelineResult.completeExceptionally(e);
393399
}

0 commit comments

Comments
 (0)