-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
AllowedSkillsClaimsValidator.java
58 lines (48 loc) · 2.34 KB
/
AllowedSkillsClaimsValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MT License.
package com.microsoft.bot.sample.dialogrootbot.authentication;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import com.microsoft.bot.builder.skills.BotFrameworkSkill;
import com.microsoft.bot.connector.Async;
import com.microsoft.bot.connector.authentication.ClaimsValidator;
import com.microsoft.bot.connector.authentication.JwtTokenValidation;
import com.microsoft.bot.connector.authentication.SkillValidation;
import com.microsoft.bot.sample.dialogrootbot.SkillsConfiguration;
/**
* Sample claims validator that loads an allowed list from configuration if
* presentand checks that requests are coming from allowed parent bots.
*/
public class AllowedSkillsClaimsValidator extends ClaimsValidator {
private final List<String> allowedSkills;
public AllowedSkillsClaimsValidator(SkillsConfiguration skillsConfig) {
if (skillsConfig == null) {
throw new IllegalArgumentException("config cannot be null.");
}
// Load the appIds for the configured skills (we will only allow responses from skills we have configured).
allowedSkills = new ArrayList<String>();
for (Map.Entry<String, BotFrameworkSkill> configuration : skillsConfig.getSkills().entrySet()) {
allowedSkills.add(configuration.getValue().getAppId());
}
}
@Override
public CompletableFuture<Void> validateClaims(Map<String, String> claims) {
// If _allowedCallers contains an "*", we allow all callers.
if (SkillValidation.isSkillClaim(claims)) {
// Check that the appId claim in the skill request instanceof in the list of callers
// configured for this bot.
String appId = JwtTokenValidation.getAppIdFromClaims(claims);
if (!allowedSkills.contains(appId)) {
return Async.completeExceptionally(
new RuntimeException(
String.format("Received a request from an application with an appID of \"%s\". "
+ "To enable requests from this skill, add the skill to your configuration file.", appId)
)
);
}
}
return CompletableFuture.completedFuture(null);
}
}