Skip to content

Commit 236bd35

Browse files
committed
CIMD - Performance: Eliminate double URI parsing in ClientIdUriSchemeCondition.applyPolicy()
closes keycloak#46703 Signed-off-by: Takashi Norimatsu <takashi.norimatsu.ws@hitachi.com>
1 parent 33ff9f1 commit 236bd35

1 file changed

Lines changed: 18 additions & 26 deletions

File tree

services/src/main/java/org/keycloak/protocol/oauth2/cimd/clientpolicy/condition/ClientIdUriSchemeCondition.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,47 +72,38 @@ public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPo
7272
switch (context.getEvent()) {
7373
case PRE_AUTHORIZATION_REQUEST:
7474
PreAuthorizationRequestContext paContext = (PreAuthorizationRequestContext) context;
75-
String clientId = ((PreAuthorizationRequestContext) context).getRequestParameters().getFirst(OAuth2Constants.CLIENT_ID);
76-
if (isUriSchemeMatched(clientId) && isTrustedDomainMatched(clientId)) return ClientPolicyVote.YES;
75+
String clientId = paContext.getRequestParameters().getFirst(OAuth2Constants.CLIENT_ID);
76+
if (clientId == null || configuration.getClientIdUriSchemes() == null || configuration.getClientIdUriSchemes().isEmpty()) {
77+
return ClientPolicyVote.NO;
78+
}
79+
final URI uri;
80+
try {
81+
uri = new URI(clientId);
82+
} catch (URISyntaxException e) {
83+
logger.debugv("not URL: clientId = {0}", clientId);
84+
return ClientPolicyVote.NO;
85+
}
86+
if (isUriSchemeMatched(uri) && isTrustedDomainMatched(uri)) return ClientPolicyVote.YES;
87+
7788
return ClientPolicyVote.NO;
7889
default:
7990
return ClientPolicyVote.ABSTAIN;
8091
}
8192
}
8293

83-
private boolean isUriSchemeMatched(String clientId) {
84-
if (clientId == null || configuration.getClientIdUriSchemes() == null || configuration.getClientIdUriSchemes().isEmpty()) {
85-
return false;
86-
}
87-
88-
final URI uri;
89-
try {
90-
uri = new URI(clientId);
91-
} catch (URISyntaxException e) {
92-
logger.debugv("not URL: clientId = {0}", clientId);
93-
return false;
94-
}
95-
94+
private boolean isUriSchemeMatched(URI uri) {
9695
return configuration.getClientIdUriSchemes().stream().anyMatch(i->i.equals(uri.getScheme()));
9796
}
9897

99-
private boolean isTrustedDomainMatched(String clientId) {
98+
private boolean isTrustedDomainMatched(URI uri) {
10099
List<String> trustedDomains = convertContentFilledList(configuration.getTrustedDomains());
101-
if (trustedDomains == null || trustedDomains.isEmpty()) {
100+
if (trustedDomains.isEmpty()) {
102101
logger.debug("trusted domain list is vacant.");
103102
return false;
104103
}
105104

106-
final URI uri;
107-
try {
108-
uri = new URI(clientId);
109-
} catch (URISyntaxException e) {
110-
logger.warnv("Malformed URL: {0}", clientId);
111-
return false;
112-
}
113-
114105
if (uri.getHost() == null) {
115-
logger.warnv("not trusted domain: host = {0}", uri.getHost());
106+
logger.warn("not trusted domain: host = null");
116107
return false;
117108
}
118109

@@ -124,6 +115,7 @@ private boolean isTrustedDomainMatched(String clientId) {
124115
return true;
125116
}
126117

118+
// return a list with non-null, non-blank, and distinct values. If the input list is null, return an empty list.
127119
private List<String> convertContentFilledList(List<String> list) {
128120
if (list == null) {
129121
return Collections.emptyList();

0 commit comments

Comments
 (0)