Search in sources :

Example 61 with StoreFactory

use of org.keycloak.authorization.store.StoreFactory in project keycloak by keycloak.

the class RepresentationToModel method importPolicies.

private static Policy importPolicies(AuthorizationProvider authorization, ResourceServer resourceServer, List<PolicyRepresentation> policiesToImport, String parentPolicyName) {
    StoreFactory storeFactory = authorization.getStoreFactory();
    for (PolicyRepresentation policyRepresentation : policiesToImport) {
        if (parentPolicyName != null && !parentPolicyName.equals(policyRepresentation.getName())) {
            continue;
        }
        Map<String, String> config = policyRepresentation.getConfig();
        String applyPolicies = config.get("applyPolicies");
        if (applyPolicies != null && !applyPolicies.isEmpty()) {
            PolicyStore policyStore = storeFactory.getPolicyStore();
            try {
                List<String> policies = (List<String>) JsonSerialization.readValue(applyPolicies, List.class);
                Set<String> policyIds = new HashSet<>();
                for (String policyName : policies) {
                    Policy policy = policyStore.findByName(policyName, resourceServer.getId());
                    if (policy == null) {
                        policy = policyStore.findById(policyName, resourceServer.getId());
                    }
                    if (policy == null) {
                        policy = importPolicies(authorization, resourceServer, policiesToImport, policyName);
                        if (policy == null) {
                            throw new RuntimeException("Policy with name [" + policyName + "] not defined.");
                        }
                    }
                    policyIds.add(policy.getId());
                }
                config.put("applyPolicies", JsonSerialization.writeValueAsString(policyIds));
            } catch (Exception e) {
                throw new RuntimeException("Error while importing policy [" + policyRepresentation.getName() + "].", e);
            }
        }
        PolicyStore policyStore = storeFactory.getPolicyStore();
        Policy policy = policyStore.findById(policyRepresentation.getId(), resourceServer.getId());
        if (policy == null) {
            policy = policyStore.findByName(policyRepresentation.getName(), resourceServer.getId());
        }
        if (policy == null) {
            policy = policyStore.create(policyRepresentation, resourceServer);
        } else {
            policy = toModel(policyRepresentation, authorization, policy);
        }
        if (parentPolicyName != null && parentPolicyName.equals(policyRepresentation.getName())) {
            return policy;
        }
    }
    return null;
}
Also used : AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) WebAuthnPolicy(org.keycloak.models.WebAuthnPolicy) OTPPolicy(org.keycloak.models.OTPPolicy) Policy(org.keycloak.authorization.model.Policy) PasswordPolicy(org.keycloak.models.PasswordPolicy) PolicyStore(org.keycloak.authorization.store.PolicyStore) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) StoreFactory(org.keycloak.authorization.store.StoreFactory) IOException(java.io.IOException) PasswordPolicyNotMetException(org.keycloak.policy.PasswordPolicyNotMetException) ModelException(org.keycloak.models.ModelException) HashSet(java.util.HashSet)

Aggregations

StoreFactory (org.keycloak.authorization.store.StoreFactory)61 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)33 ResourceServer (org.keycloak.authorization.model.ResourceServer)32 Policy (org.keycloak.authorization.model.Policy)31 Resource (org.keycloak.authorization.model.Resource)26 ClientModel (org.keycloak.models.ClientModel)21 Scope (org.keycloak.authorization.model.Scope)20 PolicyStore (org.keycloak.authorization.store.PolicyStore)20 Map (java.util.Map)19 List (java.util.List)17 ResourceStore (org.keycloak.authorization.store.ResourceStore)17 Path (javax.ws.rs.Path)15 Produces (javax.ws.rs.Produces)15 ArrayList (java.util.ArrayList)14 EnumMap (java.util.EnumMap)12 HashMap (java.util.HashMap)12 GET (javax.ws.rs.GET)12 KeycloakSession (org.keycloak.models.KeycloakSession)11 UserModel (org.keycloak.models.UserModel)11 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)11