Search in sources :

Example 1 with FederatedUserCredentialEntity

use of org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method createCredential.

@Override
public CredentialModel createCredential(RealmModel realm, String userId, CredentialModel cred) {
    createIndex(realm, userId);
    FederatedUserCredentialEntity entity = new FederatedUserCredentialEntity();
    String id = cred.getId() == null ? KeycloakModelUtils.generateId() : cred.getId();
    entity.setId(id);
    entity.setCreatedDate(cred.getCreatedDate());
    entity.setType(cred.getType());
    entity.setCredentialData(cred.getCredentialData());
    entity.setSecretData(cred.getSecretData());
    entity.setUserLabel(cred.getUserLabel());
    entity.setUserId(userId);
    entity.setRealmId(realm.getId());
    entity.setStorageProviderId(new StorageId(userId).getProviderId());
    // add in linkedlist to last position
    List<FederatedUserCredentialEntity> credentials = getStoredCredentialEntitiesStream(userId).collect(Collectors.toList());
    int priority = credentials.isEmpty() ? JpaUserCredentialStore.PRIORITY_DIFFERENCE : credentials.get(credentials.size() - 1).getPriority() + JpaUserCredentialStore.PRIORITY_DIFFERENCE;
    entity.setPriority(priority);
    em.persist(entity);
    return toModel(entity);
}
Also used : FederatedUserCredentialEntity(org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity) StorageId(org.keycloak.storage.StorageId)

Example 2 with FederatedUserCredentialEntity

use of org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method getStoredCredentialById.

@Override
public CredentialModel getStoredCredentialById(RealmModel realm, String userId, String id) {
    FederatedUserCredentialEntity entity = em.find(FederatedUserCredentialEntity.class, id);
    if (!checkCredentialEntity(entity, userId))
        return null;
    CredentialModel model = toModel(entity);
    return model;
}
Also used : CredentialModel(org.keycloak.credential.CredentialModel) FederatedUserCredentialEntity(org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity)

Example 3 with FederatedUserCredentialEntity

use of org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method moveCredentialTo.

@Override
public boolean moveCredentialTo(RealmModel realm, UserModel user, String id, String newPreviousCredentialId) {
    // 1 - Create new list and move everything to it.
    List<FederatedUserCredentialEntity> newList = this.getStoredCredentialEntitiesStream(user.getId()).collect(Collectors.toList());
    // 2 - Find indexes of our and newPrevious credential
    int ourCredentialIndex = -1;
    int newPreviousCredentialIndex = -1;
    FederatedUserCredentialEntity ourCredential = null;
    int i = 0;
    for (FederatedUserCredentialEntity credential : newList) {
        if (id.equals(credential.getId())) {
            ourCredentialIndex = i;
            ourCredential = credential;
        } else if (newPreviousCredentialId != null && newPreviousCredentialId.equals(credential.getId())) {
            newPreviousCredentialIndex = i;
        }
        i++;
    }
    if (ourCredentialIndex == -1) {
        logger.warnf("Not found credential with id [%s] of user [%s]", id, user.getUsername());
        return false;
    }
    if (newPreviousCredentialId != null && newPreviousCredentialIndex == -1) {
        logger.warnf("Can't move up credential with id [%s] of user [%s]", id, user.getUsername());
        return false;
    }
    // 3 - Compute index where we move our credential
    int toMoveIndex = newPreviousCredentialId == null ? 0 : newPreviousCredentialIndex + 1;
    // 4 - Insert our credential to new position, remove it from the old position
    newList.add(toMoveIndex, ourCredential);
    int indexToRemove = toMoveIndex < ourCredentialIndex ? ourCredentialIndex + 1 : ourCredentialIndex;
    newList.remove(indexToRemove);
    // 5 - newList contains credentials in requested order now. Iterate through whole list and change priorities accordingly.
    int expectedPriority = 0;
    for (FederatedUserCredentialEntity credential : newList) {
        expectedPriority += JpaUserCredentialStore.PRIORITY_DIFFERENCE;
        if (credential.getPriority() != expectedPriority) {
            credential.setPriority(expectedPriority);
            logger.tracef("Priority of credential [%s] of user [%s] changed to [%d]", credential.getId(), user.getUsername(), expectedPriority);
        }
    }
    return true;
}
Also used : FederatedUserCredentialEntity(org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity)

Example 4 with FederatedUserCredentialEntity

use of org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method removeStoredCredential.

@Override
public boolean removeStoredCredential(RealmModel realm, String userId, String id) {
    FederatedUserCredentialEntity entity = em.find(FederatedUserCredentialEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
    if (!checkCredentialEntity(entity, userId))
        return false;
    int currentPriority = entity.getPriority();
    this.getStoredCredentialEntitiesStream(userId).filter(credentialEntity -> credentialEntity.getPriority() > currentPriority).forEach(credentialEntity -> credentialEntity.setPriority(credentialEntity.getPriority() - JpaUserCredentialStore.PRIORITY_DIFFERENCE));
    em.remove(entity);
    return true;
}
Also used : ClientModel(org.keycloak.models.ClientModel) ClientStorageProvider(org.keycloak.storage.client.ClientStorageProvider) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) FederatedUser(org.keycloak.storage.jpa.entity.FederatedUser) KeycloakModelUtils(org.keycloak.models.utils.KeycloakModelUtils) Logger(org.jboss.logging.Logger) FederatedUserConsentEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentEntity) TypedQuery(javax.persistence.TypedQuery) StorageId(org.keycloak.storage.StorageId) HashSet(java.util.HashSet) Base64(org.keycloak.common.util.Base64) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) FederatedUserRoleMappingEntity(org.keycloak.storage.jpa.entity.FederatedUserRoleMappingEntity) UserModel(org.keycloak.models.UserModel) UserConsentModel(org.keycloak.models.UserConsentModel) ComponentModel(org.keycloak.component.ComponentModel) FederatedUserConsentClientScopeEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentClientScopeEntity) GroupModel(org.keycloak.models.GroupModel) Time(org.keycloak.common.util.Time) BrokerLinkEntity(org.keycloak.storage.jpa.entity.BrokerLinkEntity) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) JpaUserCredentialStore(org.keycloak.models.jpa.JpaUserCredentialStore) FederatedUserCredentialEntity(org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity) FederatedUserRequiredActionEntity(org.keycloak.storage.jpa.entity.FederatedUserRequiredActionEntity) FederatedUserAttributeEntity(org.keycloak.storage.jpa.entity.FederatedUserAttributeEntity) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) CredentialModel(org.keycloak.credential.CredentialModel) UserCredentialStore(org.keycloak.credential.UserCredentialStore) UserStorageProvider(org.keycloak.storage.UserStorageProvider) Collection(java.util.Collection) StreamsUtil.closing(org.keycloak.utils.StreamsUtil.closing) KeycloakSession(org.keycloak.models.KeycloakSession) RoleModel(org.keycloak.models.RoleModel) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Key(org.keycloak.storage.jpa.entity.FederatedUserRequiredActionEntity.Key) Objects(java.util.Objects) PaginationUtils.paginateQuery(org.keycloak.models.jpa.PaginationUtils.paginateQuery) List(java.util.List) Stream(java.util.stream.Stream) ModelException(org.keycloak.models.ModelException) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) LockModeType(javax.persistence.LockModeType) FederatedUserGroupMembershipEntity(org.keycloak.storage.jpa.entity.FederatedUserGroupMembershipEntity) FederatedUserCredentialEntity(org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity)

Example 5 with FederatedUserCredentialEntity

use of org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method updateCredential.

@Override
public void updateCredential(RealmModel realm, String userId, CredentialModel cred) {
    FederatedUserCredentialEntity entity = em.find(FederatedUserCredentialEntity.class, cred.getId());
    if (!checkCredentialEntity(entity, userId))
        return;
    createIndex(realm, userId);
    entity.setCreatedDate(cred.getCreatedDate());
    entity.setType(cred.getType());
    entity.setCredentialData(cred.getCredentialData());
    entity.setSecretData(cred.getSecretData());
    cred.setUserLabel(entity.getUserLabel());
}
Also used : FederatedUserCredentialEntity(org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity)

Aggregations

FederatedUserCredentialEntity (org.keycloak.storage.jpa.entity.FederatedUserCredentialEntity)5 CredentialModel (org.keycloak.credential.CredentialModel)2 StorageId (org.keycloak.storage.StorageId)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 EntityManager (javax.persistence.EntityManager)1 LockModeType (javax.persistence.LockModeType)1 TypedQuery (javax.persistence.TypedQuery)1 Logger (org.jboss.logging.Logger)1 Base64 (org.keycloak.common.util.Base64)1 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)1 Time (org.keycloak.common.util.Time)1 ComponentModel (org.keycloak.component.ComponentModel)1 UserCredentialStore (org.keycloak.credential.UserCredentialStore)1 ClientModel (org.keycloak.models.ClientModel)1 ClientScopeModel (org.keycloak.models.ClientScopeModel)1