Search in sources :

Example 26 with IdentityProviderProperty

use of org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProviderProperty in project carbon-apimgt by wso2.

the class APIAdminImpl method mergeIdpWithKeyManagerConfiguration.

private void mergeIdpWithKeyManagerConfiguration(IdentityProvider identityProvider, KeyManagerConfigurationDTO keyManagerDTO) {
    keyManagerDTO.setDisplayName(identityProvider.getDisplayName());
    keyManagerDTO.setDescription(identityProvider.getIdentityProviderDescription());
    IdentityProviderProperty[] identityProviderProperties = identityProvider.getIdpProperties();
    if (identityProviderProperties.length > 0) {
        for (IdentityProviderProperty identityProviderProperty : identityProviderProperties) {
            if (StringUtils.equals(identityProviderProperty.getName(), APIConstants.JWKS_URI)) {
                keyManagerDTO.addProperty(APIConstants.KeyManager.CERTIFICATE_TYPE, APIConstants.KeyManager.CERTIFICATE_TYPE_JWKS_ENDPOINT);
                keyManagerDTO.addProperty(APIConstants.KeyManager.CERTIFICATE_VALUE, identityProviderProperty.getValue());
            }
            if (StringUtils.equals(identityProviderProperty.getName(), IdentityApplicationConstants.IDP_ISSUER_NAME)) {
                keyManagerDTO.addProperty(APIConstants.KeyManager.ISSUER, identityProviderProperty.getValue());
            }
        }
    } else if (StringUtils.isNotBlank(identityProvider.getCertificate())) {
        keyManagerDTO.addProperty(APIConstants.KeyManager.CERTIFICATE_TYPE, APIConstants.KeyManager.CERTIFICATE_TYPE_PEM_FILE);
        keyManagerDTO.addProperty(APIConstants.KeyManager.CERTIFICATE_VALUE, identityProvider.getCertificate());
    }
    keyManagerDTO.setEnabled(identityProvider.isEnable());
    keyManagerDTO.setAlias(identityProvider.getAlias());
    ClaimConfig claimConfig = identityProvider.getClaimConfig();
    JsonArray claimArray = new JsonArray();
    for (ClaimMapping claimMapping : claimConfig.getClaimMappings()) {
        JsonObject claimMappingEntryDTO = new JsonObject();
        claimMappingEntryDTO.addProperty("localClaim", claimMapping.getLocalClaim().getClaimUri());
        claimMappingEntryDTO.addProperty("remoteClaim", claimMapping.getRemoteClaim().getClaimUri());
        claimArray.add(claimMappingEntryDTO);
    }
    keyManagerDTO.addProperty(APIConstants.KeyManager.CLAIM_MAPPING, claimArray);
}
Also used : JsonArray(com.google.gson.JsonArray) ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.IdentityProviderProperty) ClaimConfig(org.wso2.carbon.identity.application.common.model.ClaimConfig) JsonObject(com.google.gson.JsonObject)

Example 27 with IdentityProviderProperty

use of org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProviderProperty in project carbon-apimgt by wso2.

the class APIAdminImpl method updatedIDP.

private IdentityProvider updatedIDP(IdentityProvider retrievedIDP, KeyManagerConfigurationDTO keyManagerConfigurationDTO) {
    IdentityProvider identityProvider = cloneIdentityProvider(retrievedIDP);
    String idpName = sanitizeName(getSubstringOfTen(keyManagerConfigurationDTO.getName()) + "_" + keyManagerConfigurationDTO.getOrganization() + "_" + keyManagerConfigurationDTO.getUuid());
    identityProvider.setIdentityProviderName(idpName);
    identityProvider.setDisplayName(keyManagerConfigurationDTO.getDisplayName());
    identityProvider.setPrimary(Boolean.FALSE);
    identityProvider.setIdentityProviderDescription(keyManagerConfigurationDTO.getDescription());
    identityProvider.setAlias(keyManagerConfigurationDTO.getAlias());
    String certificate = null;
    if (keyManagerConfigurationDTO.getAdditionalProperties().containsKey(APIConstants.KeyManager.CERTIFICATE_VALUE)) {
        certificate = (String) keyManagerConfigurationDTO.getAdditionalProperties().get(APIConstants.KeyManager.CERTIFICATE_VALUE);
    }
    String certificateType = null;
    if (keyManagerConfigurationDTO.getAdditionalProperties().containsKey(APIConstants.KeyManager.CERTIFICATE_TYPE)) {
        certificateType = (String) keyManagerConfigurationDTO.getAdditionalProperties().get(APIConstants.KeyManager.CERTIFICATE_TYPE);
    }
    List<IdentityProviderProperty> idpProperties = new ArrayList<>();
    if (StringUtils.isNotEmpty(certificate) && StringUtils.isNotEmpty(certificateType)) {
        if (APIConstants.KeyManager.CERTIFICATE_TYPE_JWKS_ENDPOINT.equals(certificateType)) {
            if (StringUtils.isNotBlank(certificate)) {
                IdentityProviderProperty jwksProperty = new IdentityProviderProperty();
                jwksProperty.setName(APIConstants.JWKS_URI);
                jwksProperty.setValue(certificate);
                idpProperties.add(jwksProperty);
            }
        } else if (APIConstants.KeyManager.CERTIFICATE_TYPE_PEM_FILE.equals(certificateType)) {
            identityProvider.setCertificate(String.join(certificate, ""));
        }
    }
    if (keyManagerConfigurationDTO.getProperty(APIConstants.KeyManager.ISSUER) != null) {
        IdentityProviderProperty identityProviderProperty = new IdentityProviderProperty();
        identityProviderProperty.setName(IdentityApplicationConstants.IDP_ISSUER_NAME);
        identityProviderProperty.setValue((String) keyManagerConfigurationDTO.getProperty(APIConstants.KeyManager.ISSUER));
        idpProperties.add(identityProviderProperty);
    }
    if (idpProperties.size() > 0) {
        identityProvider.setIdpProperties(idpProperties.toArray(new IdentityProviderProperty[0]));
    }
    identityProvider.setEnable(keyManagerConfigurationDTO.isEnabled());
    Object claims = keyManagerConfigurationDTO.getProperty(APIConstants.KeyManager.CLAIM_MAPPING);
    updateClaims(identityProvider, claims);
    return identityProvider;
}
Also used : IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.IdentityProviderProperty) ArrayList(java.util.ArrayList) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject)

Example 28 with IdentityProviderProperty

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

the class IdentityProvider method build.

public static IdentityProvider build(OMElement identityProviderOM) {
    IdentityProvider identityProvider = new IdentityProvider();
    Iterator<?> iter = identityProviderOM.getChildElements();
    String defaultAuthenticatorConfigName = null;
    String defaultProvisioningConfigName = null;
    ArrayList<IdentityProviderProperty> idpProperties = new ArrayList();
    while (iter.hasNext()) {
        OMElement element = (OMElement) (iter.next());
        String elementName = element.getLocalName();
        if (elementName.equals(FILE_ELEMENT_IDENTITY_PROVIDER_NAME)) {
            if (element.getText() != null) {
                identityProvider.setIdentityProviderName(element.getText());
            } else {
                log.error("Identity provider not loaded from the file system. Identity provider name must be " + "not null.");
                return null;
            }
        } else if (FILE_ELEMENT_IDENTITY_PROVIDER_DESCRIPTION.equals(elementName)) {
            identityProvider.setIdentityProviderDescription(element.getText());
        } else if (FILE_ELEMENT_ALIAS.equals(elementName)) {
            identityProvider.setAlias(element.getText());
        } else if (FILE_ELEMENT_DISPLAY_NAME.equals(elementName)) {
            identityProvider.setDisplayName(element.getText());
        } else if (FILE_ELEMENT_IS_PRIMARY.equals(elementName)) {
            if (element.getText() != null && element.getText().trim().length() > 0) {
                identityProvider.setPrimary(Boolean.parseBoolean(element.getText()));
            }
        } else if (FILE_ELEMENT_IS_ENABLED.equals(elementName)) {
            if (element.getText() != null && element.getText().trim().length() > 0) {
                identityProvider.setEnable((Boolean.parseBoolean(element.getText())));
            }
        } else if (FILE_ELEMENT_IS_FEDERATION_HUB.equals(elementName)) {
            if (element.getText() != null && element.getText().trim().length() > 0) {
                identityProvider.setFederationHub(Boolean.parseBoolean(element.getText()));
            }
        } else if (FILE_ELEMENT_HOME_REALM_ID.equals(elementName)) {
            identityProvider.setHomeRealmId(element.getText());
        } else if (FILE_ELEMENT_PROVISIONING_ROLE.equals(elementName)) {
            identityProvider.setProvisioningRole(element.getText());
        } else if (FILE_ELEMENT_ISSUER.equals(elementName)) {
            IdentityProviderProperty idpIssuer = new IdentityProviderProperty();
            idpIssuer.setName(IDP_ISSUER_NAME);
            idpIssuer.setValue(element.getText());
            idpProperties.add(idpIssuer);
        } else if (FILE_ELEMENT_JWKS_ENDPOINT.equals(elementName)) {
            IdentityProviderProperty jwksEndpoint = new IdentityProviderProperty();
            jwksEndpoint.setName(JWKS_URI);
            jwksEndpoint.setValue(element.getText());
            jwksEndpoint.setDisplayName(JWKS_DISPLAYNAME);
            idpProperties.add(jwksEndpoint);
        } else if (FILE_ELEMENT_FEDERATED_AUTHENTICATOR_CONFIGS.equals(elementName)) {
            Iterator<?> federatedAuthenticatorConfigsIter = element.getChildElements();
            if (federatedAuthenticatorConfigsIter == null) {
                continue;
            }
            List<FederatedAuthenticatorConfig> federatedAuthenticatorConfigsArrList;
            federatedAuthenticatorConfigsArrList = new ArrayList<FederatedAuthenticatorConfig>();
            while (federatedAuthenticatorConfigsIter.hasNext()) {
                OMElement federatedAuthenticatorConfigsElement = (OMElement) (federatedAuthenticatorConfigsIter.next());
                FederatedAuthenticatorConfig fedAuthConfig;
                fedAuthConfig = FederatedAuthenticatorConfig.build(federatedAuthenticatorConfigsElement);
                if (fedAuthConfig != null) {
                    federatedAuthenticatorConfigsArrList.add(fedAuthConfig);
                }
            }
            if (federatedAuthenticatorConfigsArrList.size() > 0) {
                FederatedAuthenticatorConfig[] federatedAuthenticatorConfigsArr;
                federatedAuthenticatorConfigsArr = federatedAuthenticatorConfigsArrList.toArray(new FederatedAuthenticatorConfig[0]);
                identityProvider.setFederatedAuthenticatorConfigs(federatedAuthenticatorConfigsArr);
            }
        } else if (FILE_ELEMENT_DEFAULT_AUTHENTICATOR_CONFIG.equals(elementName)) {
            if (element.getText().trim().isEmpty()) {
                FederatedAuthenticatorConfig defaultAuthenticatorConfig = FederatedAuthenticatorConfig.build(element);
                if (defaultAuthenticatorConfig != null) {
                    defaultAuthenticatorConfigName = defaultAuthenticatorConfig.getName();
                }
            } else {
                defaultAuthenticatorConfigName = element.getText();
            }
        } else if (FILE_ELEMENT_PROVISIONING_CONNECTOR_CONFIGS.equals(elementName)) {
            Iterator<?> provisioningConnectorConfigsIter = element.getChildElements();
            if (provisioningConnectorConfigsIter == null) {
                continue;
            }
            List<ProvisioningConnectorConfig> provisioningConnectorConfigsArrList;
            provisioningConnectorConfigsArrList = new ArrayList<ProvisioningConnectorConfig>();
            while (provisioningConnectorConfigsIter.hasNext()) {
                OMElement provisioningConnectorConfigsElement = (OMElement) (provisioningConnectorConfigsIter.next());
                ProvisioningConnectorConfig proConConfig = null;
                try {
                    proConConfig = ProvisioningConnectorConfig.build(provisioningConnectorConfigsElement);
                } catch (IdentityApplicationManagementException e) {
                    log.error("Error while building provisioningConnectorConfig for IDP " + identityProvider.getIdentityProviderName() + ". Cause : " + e.getMessage() + ". Building rest of the " + "IDP configs", e);
                }
                if (proConConfig != null) {
                    provisioningConnectorConfigsArrList.add(proConConfig);
                }
            }
            if (CollectionUtils.isNotEmpty(provisioningConnectorConfigsArrList)) {
                ProvisioningConnectorConfig[] provisioningConnectorConfigsArr;
                provisioningConnectorConfigsArr = provisioningConnectorConfigsArrList.toArray(new ProvisioningConnectorConfig[0]);
                identityProvider.setProvisioningConnectorConfigs(provisioningConnectorConfigsArr);
            }
        } else if (FILE_ELEMENT_DEFAULT_PROVISIONING_CONNECTOR_CONFIG.equals(elementName)) {
            if (element.getText().trim().isEmpty()) {
                try {
                    ProvisioningConnectorConfig proConConfig = ProvisioningConnectorConfig.build(element);
                    if (proConConfig != null) {
                        defaultProvisioningConfigName = proConConfig.getName();
                    }
                } catch (IdentityApplicationManagementException e) {
                    log.error(String.format("Error while building default provisioning connector config for IDP %s" + ". Cause : %s Building rest of the IDP configs", identityProvider.getIdentityProviderName(), e.getMessage()));
                }
            } else {
                defaultProvisioningConfigName = element.getText();
            }
        } else if (FILE_ELEMENT_CLAIM_CONFIG.equals(elementName)) {
            identityProvider.setClaimConfig(ClaimConfig.build(element));
        } else if (FILE_ELEMENT_CERTIFICATE.equals(elementName)) {
            identityProvider.setCertificate(element.getText());
        } else if (FILE_ELEMENT_PERMISSION_AND_ROLE_CONFIG.equals(elementName)) {
            identityProvider.setPermissionAndRoleConfig(PermissionsAndRoleConfig.build(element));
        } else if (FILE_ELEMENT_JUST_IN_TIME_PROVISIONING_CONFIG.equals(elementName)) {
            identityProvider.setJustInTimeProvisioningConfig(JustInTimeProvisioningConfig.build(element));
        } else if (FILE_ELEMENT_IMAGE_URL.equals(elementName)) {
            identityProvider.setImageUrl(element.getText());
        } else if (TEMPLATE_ID.equals(elementName)) {
            identityProvider.setTemplateId(element.getText());
        }
    }
    FederatedAuthenticatorConfig[] federatedAuthenticatorConfigs = identityProvider.getFederatedAuthenticatorConfigs();
    boolean foundDefaultAuthenticator = false;
    for (int i = 0; i < federatedAuthenticatorConfigs.length; i++) {
        if (StringUtils.equals(defaultAuthenticatorConfigName, federatedAuthenticatorConfigs[i].getName())) {
            identityProvider.setDefaultAuthenticatorConfig(federatedAuthenticatorConfigs[i]);
            foundDefaultAuthenticator = true;
            break;
        }
    }
    if ((!foundDefaultAuthenticator && federatedAuthenticatorConfigs.length > 0) || (federatedAuthenticatorConfigs.length == 0 && StringUtils.isNotBlank(defaultAuthenticatorConfigName))) {
        log.warn("No matching federated authentication config found with default authentication config name :  " + defaultAuthenticatorConfigName + " in identity provider : " + identityProvider.displayName + ".");
        return null;
    }
    ProvisioningConnectorConfig[] provisioningConnectorConfigs = identityProvider.getProvisioningConnectorConfigs();
    boolean foundDefaultProvisioningConfig = false;
    for (int i = 0; i < provisioningConnectorConfigs.length; i++) {
        if (StringUtils.equals(defaultProvisioningConfigName, provisioningConnectorConfigs[i].getName())) {
            identityProvider.setDefaultProvisioningConnectorConfig(provisioningConnectorConfigs[i]);
            foundDefaultProvisioningConfig = true;
            break;
        }
    }
    if ((!foundDefaultProvisioningConfig && provisioningConnectorConfigs.length > 0) || (provisioningConnectorConfigs.length == 0 && StringUtils.isNotBlank(defaultProvisioningConfigName))) {
        log.warn("No matching provisioning config found with default provisioning config name :  " + defaultProvisioningConfigName + " in identity provider : " + identityProvider.displayName + ".");
        identityProvider = null;
    }
    if (CollectionUtils.size(idpProperties) > 0) {
        identityProvider.setIdpProperties(idpProperties.toArray(new IdentityProviderProperty[0]));
    }
    return identityProvider;
}
Also used : IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with IdentityProviderProperty

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

the class IdPManagementDAO method getIdentityPropertiesByIdpId.

/**
 * Get Identity properties map.
 *
 * @param dbConnection database connection
 * @param idpId        IDP Id
 * @return Identity provider properties
 */
private List<IdentityProviderProperty> getIdentityPropertiesByIdpId(Connection dbConnection, int idpId) throws SQLException {
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    List<IdentityProviderProperty> idpProperties = new ArrayList<IdentityProviderProperty>();
    try {
        String sqlStmt = isH2DB() ? IdPManagementConstants.SQLQueries.GET_IDP_METADATA_BY_IDP_ID_H2 : IdPManagementConstants.SQLQueries.GET_IDP_METADATA_BY_IDP_ID;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, idpId);
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            IdentityProviderProperty property = new IdentityProviderProperty();
            property.setName(rs.getString("NAME"));
            property.setValue(rs.getString("VALUE"));
            property.setDisplayName(rs.getString("DISPLAY_NAME"));
            idpProperties.add(property);
        }
    } catch (DataAccessException e) {
        throw new SQLException("Error while retrieving IDP properties for IDP ID: " + idpId, e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(null, rs, prepStmt);
    }
    return idpProperties;
}
Also used : IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.IdentityProviderProperty) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 30 with IdentityProviderProperty

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

the class IdPManagementDAO method addIdentityProviderProperties.

/**
 * Add Identity provider properties
 *
 * @param dbConnection
 * @param idpId
 * @param properties
 * @throws SQLException
 */
private void addIdentityProviderProperties(Connection dbConnection, int idpId, List<IdentityProviderProperty> properties, int tenantId) throws SQLException {
    PreparedStatement prepStmt = null;
    try {
        String sqlStmt = isH2DB() ? IdPManagementConstants.SQLQueries.ADD_IDP_METADATA_H2 : IdPManagementConstants.SQLQueries.ADD_IDP_METADATA;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        for (IdentityProviderProperty property : properties) {
            if (property.getValue() != null) {
                prepStmt.setInt(1, idpId);
                prepStmt.setString(2, property.getName());
                prepStmt.setString(3, property.getValue());
                prepStmt.setString(4, property.getDisplayName());
                prepStmt.setInt(5, tenantId);
                prepStmt.addBatch();
            } else {
                if (log.isDebugEnabled()) {
                    String msg = "IDP property '%s' of IDP with id:%d of tenantId:%d is null. " + "Not adding the property to 'IDP_METADATA' table.";
                    log.debug(String.format(msg, property.getName(), idpId, tenantId));
                }
            }
        }
        prepStmt.executeBatch();
    } catch (DataAccessException e) {
        String errorMsg = "Error while adding IDP properties for IDP ID: " + idpId + " and tenant ID:" + tenantId;
        throw new SQLException(errorMsg, e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
Also used : IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.IdentityProviderProperty) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Aggregations

IdentityProviderProperty (org.wso2.carbon.identity.application.common.model.IdentityProviderProperty)43 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)30 ArrayList (java.util.ArrayList)20 IdentityProviderManagementException (org.wso2.carbon.idp.mgt.IdentityProviderManagementException)14 PreparedStatement (java.sql.PreparedStatement)9 SQLException (java.sql.SQLException)9 FederatedAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig)9 ClaimConfig (org.wso2.carbon.identity.application.common.model.ClaimConfig)8 IdentityProviderProperty (org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProviderProperty)8 ResultSet (java.sql.ResultSet)7 HashMap (java.util.HashMap)7 Property (org.wso2.carbon.identity.application.common.model.Property)7 ProvisioningConnectorConfig (org.wso2.carbon.identity.application.common.model.ProvisioningConnectorConfig)7 Test (org.testng.annotations.Test)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)5 PostAuthnHandlerFlowStatus (org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthnHandlerFlowStatus)5 IOException (java.io.IOException)4 List (java.util.List)4 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)4