Search in sources :

Example 1 with IntermediaryScopeRepresentation

use of org.keycloak.protocol.oidc.rar.model.IntermediaryScopeRepresentation in project keycloak by keycloak.

the class ClientScopeAuthorizationRequestParser method getMatchingClientScope.

/**
 * Gets one of the requested OAuth scopes and obtains the list of all the optional client scope models for the current client and searches whether
 * there is a match.
 * Dynamic scopes are matching using the registered Regexp, while static scopes are matched by name.
 * It returns an Optional of a {@link IntermediaryScopeRepresentation} with either a static scope datra, a dynamic scope data or an empty Optional
 * if there was no match for the regexp.
 *
 * @param requestScope one of the requested OAuth scopes
 * @return see description
 */
private Optional<IntermediaryScopeRepresentation> getMatchingClientScope(String requestScope, Collection<ClientScopeModel> optionalScopes) {
    for (ClientScopeModel clientScopeModel : optionalScopes) {
        if (clientScopeModel.isDynamicScope()) {
            // The regexp has been stored without a capture group to simplify how it's shown to the user, need to transform it now
            // to capture the parameter value
            Pattern p = Pattern.compile(clientScopeModel.getDynamicScopeRegexp().replace("*", "(.*)"));
            Matcher m = p.matcher(requestScope);
            if (m.matches()) {
                return Optional.of(new IntermediaryScopeRepresentation(clientScopeModel, m.group(1), requestScope));
            }
        } else {
            if (requestScope.equalsIgnoreCase(clientScopeModel.getName())) {
                return Optional.of(new IntermediaryScopeRepresentation(clientScopeModel));
            }
        }
    }
    // Nothing matched, returning an empty Optional to avoid working with Nulls
    return Optional.empty();
}
Also used : IntermediaryScopeRepresentation(org.keycloak.protocol.oidc.rar.model.IntermediaryScopeRepresentation) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ClientScopeModel(org.keycloak.models.ClientScopeModel)

Aggregations

Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ClientScopeModel (org.keycloak.models.ClientScopeModel)1 IntermediaryScopeRepresentation (org.keycloak.protocol.oidc.rar.model.IntermediaryScopeRepresentation)1