Search in sources :

Example 91 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class JDBCUserStoreCountRetriever method countClaim.

@Override
public Long countClaim(String claimURI, String valueFilter) throws UserStoreCounterException {
    Connection dbConnection = null;
    String sqlStmt = null;
    PreparedStatement prepStmt = null;
    ResultSet resultSet = null;
    String mappedAttribute = null;
    try {
        String domainName = realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
        if (StringUtils.isEmpty(domainName)) {
            domainName = UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME;
        }
        UserRealm userRealm = UserStoreCountDSComponent.getRealmService().getTenantUserRealm(tenantId);
        if (StringUtils.isNotEmpty(claimURI)) {
            mappedAttribute = userRealm.getClaimManager().getAttributeName(domainName, claimURI);
        }
        dbConnection = getDBConnection(realmConfiguration);
        sqlStmt = JDBCUserStoreMetricsConstants.COUNT_CLAIM_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setString(1, mappedAttribute);
        prepStmt.setInt(2, tenantId);
        prepStmt.setString(3, "%" + valueFilter + "%");
        prepStmt.setString(4, UserCoreConstants.DEFAULT_PROFILE);
        prepStmt.setQueryTimeout(searchTime);
        resultSet = prepStmt.executeQuery();
        dbConnection.commit();
        if (resultSet.next()) {
            return resultSet.getLong("RESULT");
        } else {
            log.error("No claim count is retrieved from the user store.");
            return Long.valueOf(-1);
        }
    } catch (SQLException e) {
        rollbackTransaction(dbConnection);
        if (log.isDebugEnabled()) {
            log.debug("Using sql : " + sqlStmt);
        }
        throw new UserStoreCounterException(e.getMessage(), e);
    } catch (Exception e) {
        throw new UserStoreCounterException(e.getMessage(), e);
    } finally {
        DatabaseUtil.closeAllConnections(dbConnection, resultSet, prepStmt);
    }
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) UserStoreCounterException(org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreCounterException(org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException) SQLException(java.sql.SQLException)

Example 92 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class UserStoreCountService method countClaim.

/**
 * Get the count of users having claim values matching the given filter for the given claim URI
 *
 * @param claimURI    the claim URI
 * @param valueFilter filter for the claim values
 * @return the number of users matching the given claim and filter by each domain
 */
public PairDTO[] countClaim(String claimURI, String valueFilter) throws UserStoreCounterException {
    Set<String> userStoreDomains = UserStoreCountUtils.getCountEnabledUserStores();
    PairDTO[] claimCounts = new PairDTO[userStoreDomains.size()];
    int i = 0;
    for (String userStoreDomain : userStoreDomains) {
        long count = -1L;
        String filterWithDomain = getFilterWithDomain(userStoreDomain, valueFilter);
        try {
            count = getUserCountWithClaims(claimURI, filterWithDomain);
        } catch (UserStoreCounterException e) {
            log.error("Error while getting user count with claim : " + claimURI + ", from user store domain : " + userStoreDomain, e);
        }
        claimCounts[i] = new PairDTO(userStoreDomain, Long.toString(count));
        i++;
    }
    return claimCounts;
}
Also used : PairDTO(org.wso2.carbon.identity.user.store.count.dto.PairDTO) UserStoreCounterException(org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException)

Example 93 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method getLocalClaimValuesOfIDPInNonAttributeSelectionStep.

/**
 * Uses to get local claim values of an authenticated user from an IDP in non attribute selection steps.
 *
 * @param context           Authentication Context.
 * @param stepConfig        Current step configuration.
 * @param externalIdPConfig Identity providers config.
 * @return Mapped federated user values to local claims.
 * @throws PostAuthenticationFailedException Post Authentication failed exception.
 */
private Map<String, String> getLocalClaimValuesOfIDPInNonAttributeSelectionStep(AuthenticationContext context, StepConfig stepConfig, ExternalIdPConfig externalIdPConfig) throws PostAuthenticationFailedException {
    boolean useDefaultIdpDialect = externalIdPConfig.useDefaultLocalIdpDialect();
    ApplicationAuthenticator authenticator = stepConfig.getAuthenticatedAutenticator().getApplicationAuthenticator();
    String idPStandardDialect = authenticator.getClaimDialectURI();
    Map<ClaimMapping, String> extAttrs = stepConfig.getAuthenticatedUser().getUserAttributes();
    Map<String, String> originalExternalAttributeValueMap = FrameworkUtils.getClaimMappings(extAttrs, false);
    Map<String, String> claimMapping = new HashMap<>();
    Map<String, String> localClaimValues = new HashMap<>();
    if (useDefaultIdpDialect && StringUtils.isNotBlank(idPStandardDialect)) {
        try {
            claimMapping = ClaimMetadataHandler.getInstance().getMappingsMapFromOtherDialectToCarbon(idPStandardDialect, originalExternalAttributeValueMap.keySet(), context.getTenantDomain(), true);
        } catch (ClaimMetadataException e) {
            throw new PostAuthenticationFailedException(ErrorMessages.ERROR_WHILE_HANDLING_CLAIM_MAPPINGS.getCode(), ErrorMessages.ERROR_WHILE_HANDLING_CLAIM_MAPPINGS.getMessage(), e);
        }
    } else {
        ClaimMapping[] customClaimMapping = context.getExternalIdP().getClaimMappings();
        for (ClaimMapping externalClaim : customClaimMapping) {
            if (originalExternalAttributeValueMap.containsKey(externalClaim.getRemoteClaim().getClaimUri())) {
                claimMapping.put(externalClaim.getLocalClaim().getClaimUri(), externalClaim.getRemoteClaim().getClaimUri());
            }
        }
    }
    if (claimMapping != null && claimMapping.size() > 0) {
        for (Map.Entry<String, String> entry : claimMapping.entrySet()) {
            if (originalExternalAttributeValueMap.containsKey(entry.getValue()) && originalExternalAttributeValueMap.get(entry.getValue()) != null) {
                localClaimValues.put(entry.getKey(), originalExternalAttributeValueMap.get(entry.getValue()));
            }
        }
    }
    return localClaimValues;
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) FederatedApplicationAuthenticator(org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator) ApplicationAuthenticator(org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator) HashMap(java.util.HashMap) PostAuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 94 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method getUserIdClaimUriInLocalDialect.

private String getUserIdClaimUriInLocalDialect(ExternalIdPConfig idPConfig) {
    // get external identity provider user id claim URI.
    String userIdClaimUri = idPConfig.getUserIdClaimUri();
    if (StringUtils.isBlank(userIdClaimUri)) {
        return null;
    }
    boolean useDefaultLocalIdpDialect = idPConfig.useDefaultLocalIdpDialect();
    if (useDefaultLocalIdpDialect) {
        return userIdClaimUri;
    } else {
        ClaimMapping[] claimMappings = idPConfig.getClaimMappings();
        if (!ArrayUtils.isEmpty(claimMappings)) {
            for (ClaimMapping claimMapping : claimMappings) {
                if (userIdClaimUri.equals(claimMapping.getRemoteClaim().getClaimUri())) {
                    return claimMapping.getLocalClaim().getClaimUri();
                }
            }
        }
    }
    return null;
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping)

Example 95 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method isAccountDisabled.

/**
 * Uses to check whether associated users account is disabled or not.
 *
 * @param username Username of the associated user.
 * @return Whether user is disabled or not.
 * @throws PostAuthenticationFailedException When getting claim value.
 */
private boolean isAccountDisabled(String username, String tenantDomain) throws PostAuthenticationFailedException {
    try {
        UserRealm realm = (UserRealm) FrameworkServiceDataHolder.getInstance().getRealmService().getTenantUserRealm(IdentityTenantUtil.getTenantId(tenantDomain));
        UserStoreManager userStoreManager = realm.getUserStoreManager();
        Map<String, String> claimValues = userStoreManager.getUserClaimValues(username, new String[] { AccountConstants.ACCOUNT_DISABLED_CLAIM }, UserCoreConstants.DEFAULT_PROFILE);
        if (claimValues != null && claimValues.size() > 0) {
            String accountDisabledClaim = claimValues.get(AccountConstants.ACCOUNT_DISABLED_CLAIM);
            return Boolean.parseBoolean(accountDisabledClaim);
        }
    } catch (UserStoreException e) {
        throw new PostAuthenticationFailedException(ErrorMessages.ERROR_WHILE_CHECKING_ACCOUNT_DISABLE_STATUS.getCode(), String.format(ErrorMessages.ERROR_WHILE_CHECKING_ACCOUNT_DISABLE_STATUS.getMessage(), username), e);
    }
    return false;
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) PostAuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException)

Aggregations

HashMap (java.util.HashMap)112 ArrayList (java.util.ArrayList)89 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)66 UserStoreException (org.wso2.carbon.user.api.UserStoreException)65 Test (org.testng.annotations.Test)63 ClaimMetadataException (org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException)55 Map (java.util.Map)49 PreparedStatement (java.sql.PreparedStatement)48 SQLException (java.sql.SQLException)43 LocalClaim (org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim)34 RealmService (org.wso2.carbon.user.core.service.RealmService)30 UserRealm (org.wso2.carbon.user.core.UserRealm)29 Claim (org.wso2.carbon.user.api.Claim)28 UserStoreException (org.wso2.carbon.user.core.UserStoreException)28 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)28 ResultSet (java.sql.ResultSet)27 Connection (java.sql.Connection)25 ClaimConfig (org.wso2.carbon.identity.application.common.model.ClaimConfig)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 Claim (org.wso2.carbon.identity.application.common.model.Claim)24