Search in sources :

Example 21 with PostAuthenticationFailedException

use of org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method getClaimsForTenant.

/**
 * To get the list of claims available in tenant.
 *
 * @param tenantDomain          Relevant tenant domain.
 * @param externalIdPConfigName External IDP config name.
 * @return list of cliams available in the tenant.
 * @throws PostAuthenticationFailedException PostAuthentication Failed Exception.
 */
private org.wso2.carbon.user.api.ClaimMapping[] getClaimsForTenant(String tenantDomain, String externalIdPConfigName) throws PostAuthenticationFailedException {
    RealmService realmService = FrameworkServiceComponent.getRealmService();
    UserRealm realm = null;
    try {
        int usersTenantId = IdentityTenantUtil.getTenantId(tenantDomain);
        realm = (UserRealm) realmService.getTenantUserRealm(usersTenantId);
    } catch (UserStoreException e) {
        handleExceptions(String.format(ERROR_WHILE_GETTING_REALM_IN_POST_AUTHENTICATION.getMessage(), tenantDomain), ERROR_WHILE_GETTING_REALM_IN_POST_AUTHENTICATION.getCode(), e);
    }
    org.wso2.carbon.user.api.ClaimMapping[] claimMappings = null;
    try {
        if (realm != null) {
            ClaimManager claimManager = realm.getClaimManager();
            if (claimManager != null) {
                claimMappings = claimManager.getAllClaimMappings();
            }
        }
    } catch (UserStoreException e) {
        handleExceptions(String.format(ERROR_WHILE_TRYING_TO_GET_CLAIMS_WHILE_TRYING_TO_PASSWORD_PROVISION.getMessage(), externalIdPConfigName), ERROR_WHILE_TRYING_TO_GET_CLAIMS_WHILE_TRYING_TO_PASSWORD_PROVISION.getCode(), e);
    }
    if (log.isDebugEnabled()) {
        if (!ArrayUtils.isEmpty(claimMappings)) {
            StringBuilder claimMappingString = new StringBuilder();
            for (org.wso2.carbon.user.api.ClaimMapping claimMapping : claimMappings) {
                claimMappingString.append(claimMapping.getClaim().getClaimUri()).append(" ");
            }
            log.debug("Claims in tenant " + tenantDomain + " : " + claimMappingString.toString());
        }
    }
    return claimMappings;
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) ClaimManager(org.wso2.carbon.user.core.claim.ClaimManager) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 22 with PostAuthenticationFailedException

use of org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method handle.

@Override
public PostAuthnHandlerFlowStatus handle(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) throws PostAuthenticationFailedException {
    if (!FrameworkUtils.isStepBasedSequenceHandlerExecuted(context)) {
        return SUCCESS_COMPLETED;
    }
    if (log.isDebugEnabled()) {
        AuthenticatedUser authenticatedUser = context.getSequenceConfig().getAuthenticatedUser();
        log.debug("Continuing with JIT flow for the user: " + authenticatedUser);
    }
    Object isProvisionUIRedirectionTriggered = context.getProperty(FrameworkConstants.PASSWORD_PROVISION_REDIRECTION_TRIGGERED);
    if (isProvisionUIRedirectionTriggered != null && (boolean) isProvisionUIRedirectionTriggered) {
        if (log.isDebugEnabled()) {
            AuthenticatedUser authenticatedUser = context.getSequenceConfig().getAuthenticatedUser();
            log.debug("The request has hit the response flow of JIT provisioning flow for the user: " + authenticatedUser.getLoggableUserId());
        }
        return handleResponseFlow(request, context);
    } else {
        return handleRequestFlow(request, response, context);
    }
}
Also used : JSONObject(org.json.JSONObject) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)

Example 23 with PostAuthenticationFailedException

use of org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method callDefaultProvisioningHandler.

/**
 * To call the default provisioning handler.
 *
 * @param username          Name of the user to be provisioning.
 * @param context           Authentication Context.
 * @param externalIdPConfig Relevant external IDP Config.
 * @param localClaimValues  Local Claim Values.
 * @param stepConfig        Step Config.
 * @throws PostAuthenticationFailedException Post Authentication Failed Exception.
 */
private void callDefaultProvisioningHandler(String username, AuthenticationContext context, ExternalIdPConfig externalIdPConfig, Map<String, String> localClaimValues, StepConfig stepConfig) throws PostAuthenticationFailedException {
    boolean useDefaultIdpDialect = externalIdPConfig.useDefaultLocalIdpDialect();
    ApplicationAuthenticator authenticator = stepConfig.getAuthenticatedAutenticator().getApplicationAuthenticator();
    String idPStandardDialect = authenticator.getClaimDialectURI();
    String idpRoleClaimUri = FrameworkUtils.getIdpRoleClaimUri(externalIdPConfig);
    Map<ClaimMapping, String> extAttrs = stepConfig.getAuthenticatedUser().getUserAttributes();
    Map<String, String> originalExternalAttributeValueMap = FrameworkUtils.getClaimMappings(extAttrs, false);
    Map<String, String> claimMapping = null;
    boolean excludeUnmappedRoles = false;
    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);
        }
    }
    if (claimMapping != null) {
        // Ex. Standard dialects like OIDC.
        idpRoleClaimUri = claimMapping.get(IdentityUtil.getLocalGroupsClaimURI());
    } else if (idPStandardDialect == null && !useDefaultIdpDialect) {
        // Ex. SAML custom claims.
        idpRoleClaimUri = FrameworkUtils.getIdpRoleClaimUri(externalIdPConfig);
    }
    /* Get the mapped user roles according to the mapping in the IDP configuration. Exclude the unmapped from the
         returned list.
         */
    if (StringUtils.isNotEmpty(IdentityUtil.getProperty(SEND_ONLY_LOCALLY_MAPPED_ROLES_OF_IDP))) {
        excludeUnmappedRoles = Boolean.parseBoolean(IdentityUtil.getProperty(SEND_ONLY_LOCALLY_MAPPED_ROLES_OF_IDP));
    }
    List<String> identityProviderMappedUserRolesUnmappedExclusive = FrameworkUtils.getIdentityProvideMappedUserRoles(externalIdPConfig, originalExternalAttributeValueMap, idpRoleClaimUri, excludeUnmappedRoles);
    localClaimValues.put(FrameworkConstants.ASSOCIATED_ID, stepConfig.getAuthenticatedUser().getAuthenticatedSubjectIdentifier());
    localClaimValues.put(FrameworkConstants.IDP_ID, stepConfig.getAuthenticatedIdP());
    /*
        If TOTP is enabled for federated users, the initial federated user login will be identified with the following
        check and will set the secret key claim for the federated user who is going to be provisioned.
         */
    if (context.getProperty(FrameworkConstants.SECRET_KEY_CLAIM_URL) != null) {
        localClaimValues.put(FrameworkConstants.SECRET_KEY_CLAIM_URL, context.getProperty(FrameworkConstants.SECRET_KEY_CLAIM_URL).toString());
    }
    // Remove role claim from local claims as roles are specifically handled.
    localClaimValues.remove(FrameworkUtils.getLocalClaimUriMappedForIdPRoleClaim(externalIdPConfig));
    localClaimValues.remove(UserCoreConstants.USER_STORE_GROUPS_CLAIM);
    try {
        FrameworkUtils.getStepBasedSequenceHandler().callJitProvisioning(username, context, identityProviderMappedUserRolesUnmappedExclusive, localClaimValues);
    } catch (FrameworkException e) {
        handleExceptions(String.format(ERROR_WHILE_TRYING_TO_PROVISION_USER_WITHOUT_PASSWORD_PROVISIONING.getMessage(), username, externalIdPConfig.getName()), ERROR_WHILE_TRYING_TO_PROVISION_USER_WITHOUT_PASSWORD_PROVISIONING.getCode(), e);
    }
}
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) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) PostAuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException)

Example 24 with PostAuthenticationFailedException

use of org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException in project carbon-identity-framework by wso2.

the class ConsentMgtPostAuthnHandler method processUserConsent.

private UserConsent processUserConsent(HttpServletRequest request, AuthenticationContext context) throws PostAuthenticationFailedException {
    String consentClaimsPrefix = "consent_";
    UserConsent userConsent = new UserConsent();
    ConsentClaimsData consentClaimsData = (ConsentClaimsData) context.getParameter(CONSENT_CLAIM_META_DATA);
    Map<String, String[]> requestParams = request.getParameterMap();
    List<ClaimMetaData> approvedClamMetaData = buildApprovedClaimList(consentClaimsPrefix, requestParams, consentClaimsData);
    List<ClaimMetaData> consentRequiredClaimMetaData = getConsentRequiredClaimMetaData(consentClaimsData);
    List<ClaimMetaData> disapprovedClaims = buildDisapprovedClaimList(consentRequiredClaimMetaData, approvedClamMetaData);
    if (isMandatoryClaimsDisapproved(consentClaimsData.getMandatoryClaims(), disapprovedClaims)) {
        throw new PostAuthenticationFailedException("Authentication failed. Consent denied for mandatory " + "attributes.", "User denied consent to share mandatory " + "attributes.");
    }
    userConsent.setApprovedClaims(approvedClamMetaData);
    userConsent.setDisapprovedClaims(disapprovedClaims);
    return userConsent;
}
Also used : StringUtils.defaultString(org.apache.commons.lang.StringUtils.defaultString) PostAuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException)

Example 25 with PostAuthenticationFailedException

use of org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException in project carbon-identity-framework by wso2.

the class ConsentMgtPostAuthnHandler method getSPMandatoryLocalClaims.

private List<String> getSPMandatoryLocalClaims(AuthenticationContext context) throws PostAuthenticationFailedException {
    List<String> spMandatoryLocalClaims = new ArrayList<>();
    ApplicationConfig applicationConfig = context.getSequenceConfig().getApplicationConfig();
    if (applicationConfig == null) {
        ServiceProvider serviceProvider = getServiceProvider(context);
        String error = "Application configs are null in AuthenticationContext for SP: " + serviceProvider.getApplicationName() + " in tenant domain: " + getSPTenantDomain(serviceProvider);
        throw new PostAuthenticationFailedException("Authentication failed. Error while processing application " + "claim configurations.", error);
    }
    Map<String, String> claimMappings = applicationConfig.getMandatoryClaimMappings();
    if (isNotEmpty(claimMappings) && isNotEmpty(claimMappings.values())) {
        spMandatoryLocalClaims = new ArrayList<>(claimMappings.values());
    }
    String subjectClaimUri = getSubjectClaimUri(applicationConfig);
    if (!spMandatoryLocalClaims.contains(subjectClaimUri)) {
        spMandatoryLocalClaims.add(subjectClaimUri);
    }
    if (isDebugEnabled()) {
        String message = String.format("Mandatory claims for SP: %s - " + spMandatoryLocalClaims, applicationConfig.getApplicationName());
        logDebug(message);
    }
    return spMandatoryLocalClaims;
}
Also used : ApplicationConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.ApplicationConfig) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) ArrayList(java.util.ArrayList) StringUtils.defaultString(org.apache.commons.lang.StringUtils.defaultString) PostAuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException)

Aggregations

PostAuthenticationFailedException (org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException)17 HashMap (java.util.HashMap)7 StringUtils.defaultString (org.apache.commons.lang.StringUtils.defaultString)7 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)7 Map (java.util.Map)6 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)6 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)6 ApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator)5 FederatedApplicationAuthenticator (org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator)5 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)5 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)4 StepConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig)4 UserRealm (org.wso2.carbon.user.core.UserRealm)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ApplicationConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.ApplicationConfig)3 AuthenticatorConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.AuthenticatorConfig)3 ClaimMetadataException (org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException)3 FederatedAssociationManager (org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager)3 FederatedAssociationManagerException (org.wso2.carbon.identity.user.profile.mgt.association.federation.exception.FederatedAssociationManagerException)3