Search in sources :

Example 16 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class RegistryRecoveryDataStore method load.

@Override
public UserRecoveryDataDO load(String code) throws IdentityException {
    Registry registry = null;
    UserRecoveryDataDO dataDO = new UserRecoveryDataDO();
    try {
        int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
        IdentityTenantUtil.initializeRegistry(tenantId);
        registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
        registry.beginTransaction();
        String secretKeyPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_DATA + RegistryConstants.PATH_SEPARATOR + code.toLowerCase();
        if (registry.resourceExists(secretKeyPath)) {
            Resource resource = registry.get(secretKeyPath);
            Properties props = resource.getProperties();
            for (Object o : props.keySet()) {
                String key = (String) o;
                if (key.equals(USER_ID)) {
                    dataDO.setUserName(resource.getProperty(key));
                } else if (key.equals(SECRET_KEY)) {
                    dataDO.setSecret(resource.getProperty(key));
                } else if (key.equals(EXPIRE_TIME)) {
                    String time = resource.getProperty(key);
                    dataDO.setExpireTime(time);
                    if (System.currentTimeMillis() > Long.parseLong(time)) {
                        dataDO.setValid(false);
                        break;
                    } else {
                        dataDO.setValid(true);
                    }
                }
            }
        } else {
            return null;
        }
    } catch (RegistryException e) {
        log.error(e);
        throw IdentityException.error("Error while loading user recovery data for code : " + code);
    } finally {
        if (registry != null) {
            try {
                registry.commitTransaction();
            } catch (RegistryException e) {
                log.error("Error while processing registry transaction", e);
            }
        }
    }
    return dataDO;
}
Also used : UserRecoveryDataDO(org.wso2.carbon.identity.mgt.dto.UserRecoveryDataDO) Resource(org.wso2.carbon.registry.core.Resource) Registry(org.wso2.carbon.registry.core.Registry) Properties(java.util.Properties) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 17 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class UserIdentityMetadataStore method invalidateMetadataSet.

/**
 * @param metadataSet
 * @throws IdentityException
 */
public void invalidateMetadataSet(IdentityMetadataDO[] metadataSet) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.INVALIDATE_METADATA);
        for (IdentityMetadataDO metadata : metadataSet) {
            prepStmt.setString(1, metadata.getUserName());
            prepStmt.setInt(2, metadata.getTenantId());
            prepStmt.setString(3, metadata.getMetadataType());
            prepStmt.setString(4, metadata.getMetadata());
            prepStmt.addBatch();
        }
        prepStmt.executeBatch();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw IdentityException.error("Error while invalidating user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
Also used : IdentityMetadataDO(org.wso2.carbon.identity.mgt.dto.IdentityMetadataDO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 18 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class UserIdentityMetadataStore method loadMetadata.

/**
 * Can be used to return primary security questions etc
 *
 * @param userName
 * @param tenantId
 * @param metadataType
 * @return
 * @throws IdentityException
 */
public IdentityMetadataDO[] loadMetadata(String userName, int tenantId, String metadataType) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;
    ResultSet results = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.LOAD_TENANT_METADATA);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, metadataType);
        results = prepStmt.executeQuery();
        List<IdentityMetadataDO> metada = new ArrayList<IdentityMetadataDO>();
        while (results.next()) {
            metada.add(new IdentityMetadataDO(results.getString(1), results.getInt(2), results.getString(3), results.getString(4), Boolean.parseBoolean(results.getString(5))));
        }
        IdentityMetadataDO[] resultMetadata = new IdentityMetadataDO[metada.size()];
        return metada.toArray(resultMetadata);
    } catch (SQLException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(results);
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
Also used : IdentityMetadataDO(org.wso2.carbon.identity.mgt.dto.IdentityMetadataDO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 19 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException 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 20 with IdentityException

use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.

the class UserStoreBasedIdentityDataStore method store.

/**
 * This method stores data in the read write user stores.
 */
@Override
public void store(UserIdentityClaimsDO userIdentityDTO, UserStoreManager userStoreManager) throws IdentityException {
    UserIdentityClaimsDO newIdentityClaimDO = new UserIdentityClaimsDO(userIdentityDTO.getUserName(), userIdentityDTO.getUserDataMap());
    int tenantId;
    try {
        tenantId = userStoreManager.getTenantId();
    } catch (UserStoreException e) {
        throw IdentityException.error("Error while getting tenant Id.", e);
    }
    newIdentityClaimDO.setTenantId(tenantId);
    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
            setUserClaimsValuesInUserStore(userStoreManager, username, new HashMap<>(userIdentityDTO.getUserDataMap()), 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) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserIdentityClaimsDO(org.wso2.carbon.identity.mgt.dto.UserIdentityClaimsDO)

Aggregations

IdentityException (org.wso2.carbon.identity.base.IdentityException)132 UserStoreException (org.wso2.carbon.user.api.UserStoreException)62 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)38 Test (org.testng.annotations.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)35 HashMap (java.util.HashMap)30 ArrayList (java.util.ArrayList)29 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)23 IdentityMgtServiceException (org.wso2.carbon.identity.mgt.IdentityMgtServiceException)22 Resource (org.wso2.carbon.registry.core.Resource)22 PreparedStatement (java.sql.PreparedStatement)21 UserDTO (org.wso2.carbon.identity.mgt.dto.UserDTO)20 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)19 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)18 VerificationBean (org.wso2.carbon.identity.mgt.beans.VerificationBean)18 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)17 DCRDataHolder (org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder)17 Map (java.util.Map)15 RecoveryProcessor (org.wso2.carbon.identity.mgt.RecoveryProcessor)15