Search in sources :

Example 16 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class RepresentationToModel method createCredentials.

public static void createCredentials(UserRepresentation userRep, KeycloakSession session, RealmModel realm, UserModel user, boolean adminRequest) {
    convertDeprecatedCredentialsFormat(userRep);
    if (userRep.getCredentials() != null) {
        for (CredentialRepresentation cred : userRep.getCredentials()) {
            if (cred.getId() != null && session.userCredentialManager().getStoredCredentialById(realm, user, cred.getId()) != null) {
                continue;
            }
            if (cred.getValue() != null && !cred.getValue().isEmpty()) {
                RealmModel origRealm = session.getContext().getRealm();
                try {
                    session.getContext().setRealm(realm);
                    session.userCredentialManager().updateCredential(realm, user, UserCredentialModel.password(cred.getValue(), false));
                } catch (ModelException ex) {
                    throw new PasswordPolicyNotMetException(ex.getMessage(), user.getUsername(), ex);
                } finally {
                    session.getContext().setRealm(origRealm);
                }
            } else {
                session.userCredentialManager().createCredentialThroughProvider(realm, user, toModel(cred));
            }
        }
    }
}
Also used : CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) RealmModel(org.keycloak.models.RealmModel) ModelException(org.keycloak.models.ModelException) PasswordPolicyNotMetException(org.keycloak.policy.PasswordPolicyNotMetException)

Example 17 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class PersistentUserSessionAdapter method getUpdatedModel.

// Write updated model with latest serialized data
public PersistentUserSessionModel getUpdatedModel() {
    try {
        String updatedData = JsonSerialization.writeValueAsString(getData());
        this.model.setData(updatedData);
    } catch (IOException ioe) {
        throw new ModelException(ioe);
    }
    return this.model;
}
Also used : ModelException(org.keycloak.models.ModelException) IOException(java.io.IOException)

Example 18 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class AuthenticationFlowResolver method resolveDirectGrantFlow.

public static AuthenticationFlowModel resolveDirectGrantFlow(AuthenticationSessionModel authSession) {
    AuthenticationFlowModel flow = null;
    ClientModel client = authSession.getClient();
    String clientFlow = client.getAuthenticationFlowBindingOverride(AuthenticationFlowBindings.DIRECT_GRANT_BINDING);
    if (clientFlow != null) {
        flow = authSession.getRealm().getAuthenticationFlowById(clientFlow);
        if (flow == null) {
            throw new ModelException("Client " + client.getClientId() + " has direct grant flow override, but this flow does not exist");
        }
        return flow;
    }
    return authSession.getRealm().getDirectGrantFlow();
}
Also used : ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel)

Example 19 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class PersistentAuthenticatedClientSessionAdapter method getUpdatedModel.

// Write updated model with latest serialized data
public PersistentClientSessionModel getUpdatedModel() {
    try {
        String updatedData = JsonSerialization.writeValueAsString(getData());
        this.model.setData(updatedData);
    } catch (IOException ioe) {
        throw new ModelException(ioe);
    }
    return this.model;
}
Also used : ModelException(org.keycloak.models.ModelException) IOException(java.io.IOException)

Example 20 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class ClientPublicKeyLoader method loadKeys.

@Override
public Map<String, KeyWrapper> loadKeys() throws Exception {
    OIDCAdvancedConfigWrapper config = OIDCAdvancedConfigWrapper.fromClientModel(client);
    if (config.isUseJwksUrl()) {
        String jwksUrl = config.getJwksUrl();
        jwksUrl = ResolveRelative.resolveRelativeUri(session, client.getRootUrl(), jwksUrl);
        JSONWebKeySet jwks = JWKSHttpUtils.sendJwksRequest(session, jwksUrl);
        return JWKSUtils.getKeyWrappersForUse(jwks, keyUse);
    } else if (config.isUseJwksString()) {
        JSONWebKeySet jwks = JsonSerialization.readValue(config.getJwksString(), JSONWebKeySet.class);
        return JWKSUtils.getKeyWrappersForUse(jwks, keyUse);
    } else if (keyUse == JWK.Use.SIG) {
        try {
            CertificateRepresentation certInfo = CertificateInfoHelper.getCertificateFromClient(client, JWTClientAuthenticator.ATTR_PREFIX);
            KeyWrapper publicKey = getSignatureValidationKey(certInfo);
            return Collections.singletonMap(publicKey.getKid(), publicKey);
        } catch (ModelException me) {
            logger.warnf(me, "Unable to retrieve publicKey for verify signature of client '%s' . Error details: %s", client.getClientId(), me.getMessage());
            return Collections.emptyMap();
        }
    } else {
        logger.warnf("Unable to retrieve publicKey of client '%s' for the specified purpose other than verifying signature", client.getClientId());
        return Collections.emptyMap();
    }
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper) ModelException(org.keycloak.models.ModelException) OIDCAdvancedConfigWrapper(org.keycloak.protocol.oidc.OIDCAdvancedConfigWrapper) JSONWebKeySet(org.keycloak.jose.jwk.JSONWebKeySet) CertificateRepresentation(org.keycloak.representations.idm.CertificateRepresentation)

Aggregations

ModelException (org.keycloak.models.ModelException)74 RealmModel (org.keycloak.models.RealmModel)20 NamingException (javax.naming.NamingException)13 UserModel (org.keycloak.models.UserModel)13 ClientModel (org.keycloak.models.ClientModel)11 ComponentModel (org.keycloak.component.ComponentModel)10 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)10 IOException (java.io.IOException)9 Consumes (javax.ws.rs.Consumes)9 NotFoundException (javax.ws.rs.NotFoundException)8 BasicAttribute (javax.naming.directory.BasicAttribute)7 KeycloakSession (org.keycloak.models.KeycloakSession)7 RoleModel (org.keycloak.models.RoleModel)7 ErrorResponseException (org.keycloak.services.ErrorResponseException)7 ReadOnlyException (org.keycloak.storage.ReadOnlyException)7 POST (javax.ws.rs.POST)6 Path (javax.ws.rs.Path)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 AttributeInUseException (javax.naming.directory.AttributeInUseException)5