Search in sources :

Example 1 with UserIdentityClaim

use of org.wso2.carbon.identity.governance.model.UserIdentityClaim in project carbon-identity-framework by wso2.

the class UserStoreBasedIdentityDataStore method load.

/**
 * This method loads identity and security questions from the user stores
 */
@Override
public UserIdentityClaimsDO load(String userName, UserStoreManager userStoreManager) {
    UserIdentityClaimsDO userIdentityDTO = super.load(userName, userStoreManager);
    if (userIdentityDTO != null) {
        return userIdentityDTO;
    }
    // which happen calling getUserClaimValues()
    if (TRUE_STRING.equals(userStoreInvoked.get())) {
        if (log.isDebugEnabled()) {
            log.debug("UserStoreBasedIdentityDataStore.load() already been called in the stack." + "Hence returning without processing load() again.");
        }
        return null;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Set flag to indicate method UserStoreBasedIdentityDataStore.load() been called");
        }
        userStoreInvoked.set(TRUE_STRING);
    }
    Map<String, String> userDataMap = new HashMap<String, String>();
    try {
        // reading all claims of the user
        Claim[] claims = ((AbstractUserStoreManager) userStoreManager).getUserClaimValues(userName, null);
        // select the security questions and identity claims
        if (claims != null) {
            for (Claim claim : claims) {
                String claimUri = claim.getClaimUri();
                if (claimUri.contains(UserCoreConstants.ClaimTypeURIs.IDENTITY_CLAIM_URI) || claimUri.contains(UserCoreConstants.ClaimTypeURIs.CHALLENGE_QUESTION_URI)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Adding UserIdentityClaim : " + claimUri + " with the value : " + claim.getValue());
                    }
                    userDataMap.put(claimUri, claim.getValue());
                }
            }
        } else {
            // null is returned when the user doesn't exist
            return null;
        }
    } catch (UserStoreException e) {
        if (!e.getMessage().startsWith(IdentityCoreConstants.USER_NOT_FOUND)) {
            log.error("Error while reading identity user data from user store", e);
        } else if (log.isDebugEnabled()) {
            String message = null;
            if (userStoreManager instanceof AbstractUserStoreManager) {
                String domain = ((AbstractUserStoreManager) userStoreManager).getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
                if (domain != null) {
                    message = "User: " + userName + " does not exist in " + domain;
                }
            }
            if (message == null) {
                message = "User: " + userName + " does not exist";
            }
            log.debug(message);
        }
        return null;
    } finally {
        // reset to initial value
        if (log.isDebugEnabled()) {
            log.debug("Reset flag to indicate method UserStoreBasedIdentityDataStore.load() being completing");
        }
        userStoreInvoked.set(FALSE_STRING);
    }
    userIdentityDTO = new UserIdentityClaimsDO(userName, userDataMap);
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    userIdentityDTO.setTenantId(tenantId);
    org.wso2.carbon.user.core.UserStoreManager store = (org.wso2.carbon.user.core.UserStoreManager) userStoreManager;
    String domainName = store.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
    try {
        super.store(userIdentityDTO, userStoreManager);
    } catch (IdentityException e) {
        log.error("Error while reading user identity data", e);
    }
    return userIdentityDTO;
}
Also used : HashMap(java.util.HashMap) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) ActiveDirectoryUserStoreManager(org.wso2.carbon.user.core.ldap.ActiveDirectoryUserStoreManager) ReadWriteLDAPUserStoreManager(org.wso2.carbon.user.core.ldap.ReadWriteLDAPUserStoreManager) JDBCUserStoreManager(org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager) IdentityException(org.wso2.carbon.identity.base.IdentityException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserIdentityClaimsDO(org.wso2.carbon.identity.mgt.dto.UserIdentityClaimsDO) Claim(org.wso2.carbon.user.core.claim.Claim)

Example 2 with UserIdentityClaim

use of org.wso2.carbon.identity.governance.model.UserIdentityClaim in project identity-governance by wso2-extensions.

the class InMemoryIdentityDataStore method load.

@Override
public UserIdentityClaim load(String userName, UserStoreManager userStoreManager) {
    try {
        if (userName != null) {
            userName = UserCoreUtil.removeDomainFromName(userName);
            if (userStoreManager instanceof org.wso2.carbon.user.core.UserStoreManager) {
                if (!IdentityUtil.isUserStoreCaseSensitive((org.wso2.carbon.user.core.UserStoreManager) userStoreManager)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Case insensitive user store found. Changing username from : " + userName + " to : " + userName.toLowerCase(Locale.ENGLISH));
                    }
                    userName = userName.toLowerCase(Locale.ENGLISH);
                } else if (!IdentityUtil.isUseCaseSensitiveUsernameForCacheKeys((org.wso2.carbon.user.core.UserStoreManager) userStoreManager)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Case insensitive username for cache key is used. Changing username from : " + userName + " to : " + userName.toLowerCase(Locale.ENGLISH));
                    }
                    userName = userName.toLowerCase(Locale.ENGLISH);
                }
            }
            org.wso2.carbon.user.core.UserStoreManager store = (org.wso2.carbon.user.core.UserStoreManager) userStoreManager;
            String domainName = store.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
            IdentityDataStoreCacheKey key = new IdentityDataStoreCacheKey(domainName, userName);
            int tenantId = userStoreManager.getTenantId();
            UserIdentityClaim userIdentityDTO = identityDataStoreCache.getValueFromCache(key, tenantId);
            if (userIdentityDTO != null && log.isDebugEnabled()) {
                StringBuilder data = new StringBuilder("{");
                if (userIdentityDTO.getUserIdentityDataMap() != null) {
                    for (Map.Entry<String, String> entry : userIdentityDTO.getUserIdentityDataMap().entrySet()) {
                        data.append("[").append(entry.getKey()).append(" = ").append(entry.getValue()).append("], ");
                    }
                }
                if (data.indexOf(",") >= 0) {
                    data.deleteCharAt(data.lastIndexOf(","));
                }
                data.append("}");
                log.debug("Loaded UserIdentityClaimsDO from cache for user :" + userName + " with claims: " + data);
            }
            return userIdentityDTO;
        }
    } catch (UserStoreException e) {
        log.error("Error while obtaining tenant ID from user store manager");
    }
    return null;
}
Also used : IdentityDataStoreCacheKey(org.wso2.carbon.identity.governance.internal.cache.IdentityDataStoreCacheKey) UserIdentityClaim(org.wso2.carbon.identity.governance.model.UserIdentityClaim) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Map(java.util.Map)

Example 3 with UserIdentityClaim

use of org.wso2.carbon.identity.governance.model.UserIdentityClaim in project identity-governance by wso2-extensions.

the class JDBCIdentityDataStore method load.

@Override
public UserIdentityClaim load(String userName, UserStoreManager userStoreManager) {
    String domainName = ((org.wso2.carbon.user.core.UserStoreManager) userStoreManager).getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
    userName = UserCoreUtil.addDomainToName(userName, domainName);
    // Getting from cache
    UserIdentityClaim dto = super.load(userName, userStoreManager);
    if (dto != null) {
        return dto;
    }
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    try {
        int tenantId = userStoreManager.getTenantId();
        Map<String, String> data = getUserDataValues(connection, userName, tenantId);
        IdentityDatabaseUtil.commitTransaction(connection);
        if (log.isDebugEnabled()) {
            log.debug("Retrieved identity data for:" + tenantId + ":" + userName);
            for (Map.Entry<String, String> dataEntry : data.entrySet()) {
                log.debug(dataEntry.getKey() + " : " + dataEntry.getValue());
            }
        }
        dto = new UserIdentityClaim(userName, data);
        dto.setTenantId(tenantId);
        try {
            super.store(dto, userStoreManager);
        } catch (IdentityException e) {
            log.error("Error while reading user identity data", e);
        }
        return dto;
    } catch (SQLException | UserStoreException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeConnection(connection);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserIdentityClaim(org.wso2.carbon.identity.governance.model.UserIdentityClaim) IdentityException(org.wso2.carbon.identity.base.IdentityException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with UserIdentityClaim

use of org.wso2.carbon.identity.governance.model.UserIdentityClaim in project identity-governance by wso2-extensions.

the class JDBCIdentityDataStore method store.

@Override
public void store(UserIdentityClaim userIdentityDTO, UserStoreManager userStoreManager) throws IdentityException {
    if (userIdentityDTO == null || userIdentityDTO.getUserIdentityDataMap().isEmpty()) {
        return;
    }
    // Putting into cache
    String userName = userIdentityDTO.getUserName();
    String domainName = ((org.wso2.carbon.user.core.UserStoreManager) userStoreManager).getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
    userName = UserCoreUtil.addDomainToName(userName, domainName);
    userIdentityDTO.setUserName(userName);
    super.store(userIdentityDTO, userStoreManager);
    int tenantId = MultitenantConstants.SUPER_TENANT_ID;
    try {
        tenantId = userStoreManager.getTenantId();
    } catch (UserStoreException e) {
        log.error("Error while getting tenant Id.", e);
    }
    Map<String, String> data = userIdentityDTO.getUserIdentityDataMap();
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    try {
        Map<String, String> existingDataValues = getUserDataValues(connection, userName, tenantId);
        Map<String, String> newClaims = new HashMap<>();
        Map<String, String> availableClaims = new HashMap<>();
        // Divide claim list to already available claims (need to update those) and new claims (need to add those)
        for (Map.Entry<String, String> entry : data.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (existingDataValues.containsKey(key)) {
                String existingValue = existingDataValues.get(key);
                if (existingValue == null || !existingValue.equals(value)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Key:" + key + ", Value:" + value + " to be updated for user:" + userName + " in JDBCIdentityDataStore");
                    }
                    availableClaims.put(key, value);
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Key:" + key + ", Value:" + value + " to be added for user:" + userName + " in " + "JDBCIdentityDataStore");
                }
                newClaims.put(key, value);
            }
        }
        addUserDataValues(connection, userName, tenantId, newClaims);
        updateUserDataValues(connection, userName, tenantId, availableClaims);
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Error while persisting user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Connection(java.sql.Connection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with UserIdentityClaim

use of org.wso2.carbon.identity.governance.model.UserIdentityClaim in project identity-governance by wso2-extensions.

the class UserStoreBasedIdentityDataStore method store.

/**
 * This method stores data in the read write user stores.
 */
@Override
public void store(UserIdentityClaim userIdentityDTO, UserStoreManager userStoreManager) throws IdentityException {
    UserIdentityClaim newIdentityClaimDO = new UserIdentityClaim(userIdentityDTO.getUserName(), userIdentityDTO.getUserIdentityDataMap());
    super.store(newIdentityClaimDO, userStoreManager);
    if (userIdentityDTO.getUserName() == null) {
        log.error("Error while persisting user data.  Null user name is provided.");
        return;
    }
    String username = UserCoreUtil.removeDomainFromName(userIdentityDTO.getUserName());
    try {
        // store then log a warn.
        if (!userStoreManager.isReadOnly()) {
            // Need to clone the map. If not iterative calls will refer the same map
            userStoreManager.setUserClaimValues(username, new HashMap<String, String>(userIdentityDTO.getUserIdentityDataMap()), null);
        } else {
            // If the user store is read only and still uses UserStoreBasedIdentityDataStore, then log a warn
            log.warn("User store is read only. Changes to identities are only stored in memory, " + "and not updated in user store.");
            return;
        }
    } catch (UserStoreException e) {
        if (!e.getMessage().startsWith(IdentityCoreConstants.USER_NOT_FOUND)) {
            throw IdentityException.error("Error while persisting identity user data in to user store", e);
        } else if (log.isDebugEnabled()) {
            String message = null;
            if (userStoreManager instanceof AbstractUserStoreManager) {
                String domain = ((AbstractUserStoreManager) userStoreManager).getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
                if (domain != null) {
                    message = "User: " + username + " does not exist in " + domain;
                }
            }
            if (message == null) {
                message = "User: " + username + " does not exist";
            }
            log.debug(message);
            return;
        }
    }
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserIdentityClaim(org.wso2.carbon.identity.governance.model.UserIdentityClaim) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager)

Aggregations

UserIdentityClaim (org.wso2.carbon.identity.governance.model.UserIdentityClaim)11 UserStoreException (org.wso2.carbon.user.api.UserStoreException)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 IdentityException (org.wso2.carbon.identity.base.IdentityException)4 UserStoreBasedIdentityDataStore (org.wso2.carbon.identity.governance.store.UserStoreBasedIdentityDataStore)4 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)4 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)3 UserClaimSearchEntry (org.wso2.carbon.user.core.model.UserClaimSearchEntry)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 IdentityDataStoreCacheKey (org.wso2.carbon.identity.governance.internal.cache.IdentityDataStoreCacheKey)2 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)2 Claim (org.wso2.carbon.user.core.claim.Claim)2 Field (java.lang.reflect.Field)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1