Search in sources :

Example 1 with UserSelfRegistrationHandler

use of org.wso2.carbon.identity.recovery.handler.UserSelfRegistrationHandler in project identity-governance by wso2-extensions.

the class IdentityRecoveryServiceComponent method activate.

@Activate
protected void activate(ComponentContext context) {
    try {
        BundleContext bundleContext = context.getBundleContext();
        bundleContext.registerService(NotificationPasswordRecoveryManager.class.getName(), NotificationPasswordRecoveryManager.getInstance(), null);
        bundleContext.registerService(SecurityQuestionPasswordRecoveryManager.class.getName(), SecurityQuestionPasswordRecoveryManager.getInstance(), null);
        bundleContext.registerService(NotificationUsernameRecoveryManager.class.getName(), NotificationUsernameRecoveryManager.getInstance(), null);
        bundleContext.registerService(UserSelfRegistrationManager.class.getName(), UserSelfRegistrationManager.getInstance(), null);
        bundleContext.registerService(ChallengeQuestionManager.class.getName(), ChallengeQuestionManager.getInstance(), null);
        bundleContext.registerService(ResendConfirmationManager.class.getName(), ResendConfirmationManager.getInstance(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new AccountConfirmationValidationHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new UserSelfRegistrationHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new LiteUserRegistrationHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new UserEmailVerificationHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new MobileNumberVerificationHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new AdminForcedPasswordResetHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new TenantRegistrationVerificationHandler(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new IdentityUserMetadataMgtHandler(), null);
        bundleContext.registerService(IdentityConnectorConfig.class.getName(), new RecoveryConfigImpl(), null);
        bundleContext.registerService(IdentityConnectorConfig.class.getName(), new SelfRegistrationConfigImpl(), null);
        bundleContext.registerService(IdentityConnectorConfig.class.getName(), new LiteRegistrationConfigImpl(), null);
        bundleContext.registerService(IdentityConnectorConfig.class.getName(), new UserEmailVerificationConfigImpl(), null);
        bundleContext.registerService(IdentityConnectorConfig.class.getName(), new UserClaimUpdateConfigImpl(), null);
        bundleContext.registerService(IdentityConnectorConfig.class.getName(), new AdminForcedPasswordResetConfigImpl(), null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new CodeInvalidationHandler(), null);
        UsernameRecoveryManager usernameRecoveryManager = new UsernameRecoveryManagerImpl();
        bundleContext.registerService(UsernameRecoveryManager.class.getName(), usernameRecoveryManager, null);
        PasswordRecoveryManager passwordRecoveryManager = new PasswordRecoveryManagerImpl();
        bundleContext.registerService(PasswordRecoveryManager.class.getName(), passwordRecoveryManager, null);
        // Registering missing challenge question handler as a post authn handler
        PostAuthenticationHandler postAuthnMissingChallengeQuestions = PostAuthnMissingChallengeQuestionsHandler.getInstance();
        bundleContext.registerService(PostAuthenticationHandler.class.getName(), postAuthnMissingChallengeQuestions, null);
        bundleContext.registerService(AbstractEventHandler.class.getName(), new ChallengeAnswerValidationHandler(), null);
    } catch (Exception e) {
        log.error("Error while activating identity governance component.", e);
    }
    // register the tenant management listener
    TenantMgtListener tenantMgtListener = new TenantManagementListener();
    context.getBundleContext().registerService(TenantMgtListener.class.getName(), tenantMgtListener, null);
    // register default challenge questions
    try {
        if (log.isDebugEnabled()) {
            log.debug("Loading default challenge questions for super tenant.");
        }
        loadDefaultChallengeQuestions();
    // new ChallengeQuestionManager().getAllChallengeQuestions("carbon.super", "lk_LK");
    } catch (IdentityRecoveryException e) {
        log.error("Error persisting challenge question for super tenant.", e);
    }
}
Also used : NotificationPasswordRecoveryManager(org.wso2.carbon.identity.recovery.password.NotificationPasswordRecoveryManager) TenantManagementListener(org.wso2.carbon.identity.recovery.listener.TenantManagementListener) UsernameRecoveryManagerImpl(org.wso2.carbon.identity.recovery.internal.service.impl.username.UsernameRecoveryManagerImpl) UserSelfRegistrationManager(org.wso2.carbon.identity.recovery.signup.UserSelfRegistrationManager) IdentityUserMetadataMgtHandler(org.wso2.carbon.identity.recovery.handler.IdentityUserMetadataMgtHandler) NotificationUsernameRecoveryManager(org.wso2.carbon.identity.recovery.username.NotificationUsernameRecoveryManager) ResendConfirmationManager(org.wso2.carbon.identity.recovery.confirmation.ResendConfirmationManager) SecurityQuestionPasswordRecoveryManager(org.wso2.carbon.identity.recovery.password.SecurityQuestionPasswordRecoveryManager) LiteRegistrationConfigImpl(org.wso2.carbon.identity.recovery.connector.LiteRegistrationConfigImpl) TenantMgtListener(org.wso2.carbon.stratos.common.listeners.TenantMgtListener) AdminForcedPasswordResetHandler(org.wso2.carbon.identity.recovery.handler.AdminForcedPasswordResetHandler) IdentityConnectorConfig(org.wso2.carbon.identity.governance.common.IdentityConnectorConfig) ChallengeAnswerValidationHandler(org.wso2.carbon.identity.recovery.handler.ChallengeAnswerValidationHandler) AccountConfirmationValidationHandler(org.wso2.carbon.identity.recovery.handler.AccountConfirmationValidationHandler) NotificationUsernameRecoveryManager(org.wso2.carbon.identity.recovery.username.NotificationUsernameRecoveryManager) UsernameRecoveryManager(org.wso2.carbon.identity.recovery.services.username.UsernameRecoveryManager) TenantRegistrationVerificationHandler(org.wso2.carbon.identity.recovery.handler.TenantRegistrationVerificationHandler) RecoveryConfigImpl(org.wso2.carbon.identity.recovery.connector.RecoveryConfigImpl) CodeInvalidationHandler(org.wso2.carbon.identity.recovery.handler.CodeInvalidationHandler) UserEmailVerificationHandler(org.wso2.carbon.identity.recovery.handler.UserEmailVerificationHandler) MobileNumberVerificationHandler(org.wso2.carbon.identity.recovery.handler.MobileNumberVerificationHandler) UserEmailVerificationConfigImpl(org.wso2.carbon.identity.recovery.connector.UserEmailVerificationConfigImpl) UserSelfRegistrationHandler(org.wso2.carbon.identity.recovery.handler.UserSelfRegistrationHandler) ChallengeQuestionManager(org.wso2.carbon.identity.recovery.ChallengeQuestionManager) PasswordRecoveryManager(org.wso2.carbon.identity.recovery.services.password.PasswordRecoveryManager) SecurityQuestionPasswordRecoveryManager(org.wso2.carbon.identity.recovery.password.SecurityQuestionPasswordRecoveryManager) NotificationPasswordRecoveryManager(org.wso2.carbon.identity.recovery.password.NotificationPasswordRecoveryManager) PasswordRecoveryManagerImpl(org.wso2.carbon.identity.recovery.internal.service.impl.password.PasswordRecoveryManagerImpl) IdentityRecoveryException(org.wso2.carbon.identity.recovery.IdentityRecoveryException) UserClaimUpdateConfigImpl(org.wso2.carbon.identity.recovery.connector.UserClaimUpdateConfigImpl) AbstractEventHandler(org.wso2.carbon.identity.event.handler.AbstractEventHandler) AdminForcedPasswordResetConfigImpl(org.wso2.carbon.identity.recovery.connector.AdminForcedPasswordResetConfigImpl) LiteUserRegistrationHandler(org.wso2.carbon.identity.recovery.handler.LiteUserRegistrationHandler) SelfRegistrationConfigImpl(org.wso2.carbon.identity.recovery.connector.SelfRegistrationConfigImpl) IdentityRecoveryException(org.wso2.carbon.identity.recovery.IdentityRecoveryException) BundleContext(org.osgi.framework.BundleContext) PostAuthenticationHandler(org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthenticationHandler) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with UserSelfRegistrationHandler

use of org.wso2.carbon.identity.recovery.handler.UserSelfRegistrationHandler in project identity-governance by wso2-extensions.

the class UserSelfRegistrationManager method registerUser.

public NotificationResponseBean registerUser(User user, String password, Claim[] claims, Property[] properties) throws IdentityRecoveryException {
    publishEvent(user, claims, properties, IdentityEventConstants.Event.PRE_SELF_SIGNUP_REGISTER);
    String consent = getPropertyValue(properties, IdentityRecoveryConstants.Consent.CONSENT);
    String tenantDomain = user.getTenantDomain();
    if (StringUtils.isEmpty(tenantDomain)) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    // Callback URL validation
    String callbackURL = null;
    try {
        callbackURL = Utils.getCallbackURLFromRegistration(properties);
        if (StringUtils.isNotBlank(callbackURL) && !Utils.validateCallbackURL(callbackURL, tenantDomain, IdentityRecoveryConstants.ConnectorConfig.SELF_REGISTRATION_CALLBACK_REGEX)) {
            throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_CALLBACK_URL_NOT_VALID, callbackURL);
        }
    } catch (MalformedURLException | UnsupportedEncodingException | IdentityEventException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_CALLBACK_URL_NOT_VALID, callbackURL);
    }
    if (StringUtils.isBlank(user.getTenantDomain())) {
        user.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        log.info("registerUser :Tenant domain is not in the request. set to default for user : " + user.getUserName());
    }
    if (StringUtils.isBlank(user.getUserStoreDomain())) {
        user.setUserStoreDomain(IdentityUtil.getPrimaryDomainName());
        log.info("registerUser :User store domain is not in the request. set to default for user : " + user.getUserName());
    }
    boolean enable = Boolean.parseBoolean(Utils.getSignUpConfigs(IdentityRecoveryConstants.ConnectorConfig.ENABLE_SELF_SIGNUP, user.getTenantDomain()));
    if (!enable) {
        throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_DISABLE_SELF_SIGN_UP, user.getUserName());
    }
    NotificationResponseBean notificationResponseBean;
    try {
        RealmService realmService = IdentityRecoveryServiceDataHolder.getInstance().getRealmService();
        UserStoreManager userStoreManager;
        try {
            userStoreManager = realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(user.getTenantDomain())).getUserStoreManager();
        } catch (UserStoreException e) {
            throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, user.getUserName(), e);
        }
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantId(IdentityTenantUtil.getTenantId(user.getTenantDomain()));
        carbonContext.setTenantDomain(user.getTenantDomain());
        Map<String, String> claimsMap = new HashMap<>();
        for (Claim claim : claims) {
            claimsMap.put(claim.getClaimUri(), claim.getValue());
        }
        // Set arbitrary properties to use in UserSelfRegistrationHandler
        Utils.setArbitraryProperties(properties);
        validateAndFilterFromReceipt(consent, claimsMap);
        // User preferred notification channel.
        String preferredChannel;
        try {
            // TODO It is required to add this role before tenant creation. And also, this role should not not be able remove.
            if (!userStoreManager.isExistingRole(IdentityRecoveryConstants.SELF_SIGNUP_ROLE)) {
                Permission permission = new Permission("/permission/admin/login", IdentityRecoveryConstants.EXECUTE_ACTION);
                userStoreManager.addRole(IdentityRecoveryConstants.SELF_SIGNUP_ROLE, null, new Permission[] { permission });
            }
            String[] userRoles = new String[] { IdentityRecoveryConstants.SELF_SIGNUP_ROLE };
            try {
                NotificationChannelManager notificationChannelManager = Utils.getNotificationChannelManager();
                preferredChannel = notificationChannelManager.resolveCommunicationChannel(user.getUserName(), user.getTenantDomain(), user.getUserStoreDomain(), claimsMap);
            } catch (NotificationChannelManagerException e) {
                throw mapNotificationChannelManagerException(e, user);
            }
            // resolved channel is not empty.
            if (StringUtils.isEmpty(claimsMap.get(IdentityRecoveryConstants.PREFERRED_CHANNEL_CLAIM)) && StringUtils.isNotEmpty(preferredChannel)) {
                claimsMap.put(IdentityRecoveryConstants.PREFERRED_CHANNEL_CLAIM, preferredChannel);
            }
            userStoreManager.addUser(IdentityUtil.addDomainToName(user.getUserName(), user.getUserStoreDomain()), password, userRoles, claimsMap, null);
        } catch (UserStoreException e) {
            Throwable cause = e;
            while (cause != null) {
                if (cause instanceof PolicyViolationException) {
                    throw IdentityException.error(IdentityRecoveryClientException.class, IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_POLICY_VIOLATION.getCode(), cause.getMessage(), e);
                }
                cause = cause.getCause();
            }
            Utils.checkPasswordPatternViolation(e, user);
            return handleClientException(user, e);
        }
        addUserConsent(consent, tenantDomain);
        // Build the notification response.
        notificationResponseBean = buildNotificationResponseBean(user, preferredChannel, claimsMap);
    } finally {
        Utils.clearArbitraryProperties();
        PrivilegedCarbonContext.endTenantFlow();
    }
    publishEvent(user, claims, properties, IdentityEventConstants.Event.POST_SELF_SIGNUP_REGISTER);
    return notificationResponseBean;
}
Also used : MalformedURLException(java.net.MalformedURLException) NotificationChannelManager(org.wso2.carbon.identity.governance.service.notification.NotificationChannelManager) NotificationChannelManagerException(org.wso2.carbon.identity.governance.exceptions.notiification.NotificationChannelManagerException) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) NotificationResponseBean(org.wso2.carbon.identity.recovery.bean.NotificationResponseBean) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Permission(org.wso2.carbon.user.core.Permission) PolicyViolationException(org.wso2.carbon.identity.mgt.policy.PolicyViolationException) Claim(org.wso2.carbon.user.api.Claim) IdentityRecoveryClientException(org.wso2.carbon.identity.recovery.IdentityRecoveryClientException)

Example 3 with UserSelfRegistrationHandler

use of org.wso2.carbon.identity.recovery.handler.UserSelfRegistrationHandler in project identity-governance by wso2-extensions.

the class UserSelfRegistrationManager method registerLiteUser.

public NotificationResponseBean registerLiteUser(User user, Claim[] claims, Property[] properties) throws IdentityRecoveryException {
    String consent = getPropertyValue(properties, IdentityRecoveryConstants.Consent.CONSENT);
    String tenantDomain = user.getTenantDomain();
    if (StringUtils.isEmpty(tenantDomain)) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    // Callback URL validation
    String callbackURL = null;
    try {
        callbackURL = Utils.getCallbackURLFromRegistration(properties);
        if (StringUtils.isNotBlank(callbackURL) && !Utils.validateCallbackURL(callbackURL, tenantDomain, IdentityRecoveryConstants.ConnectorConfig.SELF_REGISTRATION_CALLBACK_REGEX)) {
            throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_CALLBACK_URL_NOT_VALID, callbackURL);
        }
    } catch (MalformedURLException | UnsupportedEncodingException | IdentityEventException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_CALLBACK_URL_NOT_VALID, callbackURL);
    }
    if (StringUtils.isBlank(user.getTenantDomain())) {
        user.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        log.info("registerUser :Tenant domain is not in the request. set to default for user : " + user.getUserName());
    }
    if (StringUtils.isBlank(user.getUserStoreDomain())) {
        user.setUserStoreDomain(IdentityUtil.getPrimaryDomainName());
        log.info("registerUser :User store domain is not in the request. set to default for user : " + user.getUserName());
    }
    boolean enable = Boolean.parseBoolean(Utils.getSignUpConfigs(IdentityRecoveryConstants.ConnectorConfig.ENABLE_LITE_SIGN_UP, user.getTenantDomain()));
    if (!enable) {
        throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_DISABLE_LITE_SIGN_UP, user.getUserName());
    }
    NotificationResponseBean notificationResponseBean;
    try {
        RealmService realmService = IdentityRecoveryServiceDataHolder.getInstance().getRealmService();
        UserStoreManager userStoreManager;
        try {
            userStoreManager = realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(user.getTenantDomain())).getUserStoreManager();
        } catch (UserStoreException e) {
            throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, user.getUserName(), e);
        }
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantId(IdentityTenantUtil.getTenantId(user.getTenantDomain()));
        carbonContext.setTenantDomain(user.getTenantDomain());
        Map<String, String> claimsMap = new HashMap<>();
        for (Claim claim : claims) {
            claimsMap.put(claim.getClaimUri(), claim.getValue());
        }
        // Set lite user sign up claim to indicate the profile
        claimsMap.put(IdentityRecoveryConstants.LITE_USER_CLAIM, Boolean.TRUE.toString());
        // Set arbitrary properties to use in UserSelfRegistrationHandler
        Utils.setArbitraryProperties(properties);
        validateAndFilterFromReceipt(consent, claimsMap);
        // User preferred notification channel.
        String preferredChannel;
        try {
            String[] userRoles = new String[] {};
            try {
                NotificationChannelManager notificationChannelManager = Utils.getNotificationChannelManager();
                preferredChannel = notificationChannelManager.resolveCommunicationChannel(user.getUserName(), user.getTenantDomain(), user.getUserStoreDomain(), claimsMap);
            } catch (NotificationChannelManagerException e) {
                throw mapNotificationChannelManagerException(e, user);
            }
            // resolved channel is not empty.
            if (StringUtils.isEmpty(claimsMap.get(IdentityRecoveryConstants.PREFERRED_CHANNEL_CLAIM)) && StringUtils.isNotEmpty(preferredChannel)) {
                claimsMap.put(IdentityRecoveryConstants.PREFERRED_CHANNEL_CLAIM, preferredChannel);
            }
            userStoreManager.addUser(IdentityUtil.addDomainToName(user.getUserName(), user.getUserStoreDomain()), Utils.generateRandomPassword(12), userRoles, claimsMap, null);
        } catch (UserStoreException e) {
            Throwable cause = e;
            while (cause != null) {
                if (cause instanceof PolicyViolationException) {
                    throw IdentityException.error(IdentityRecoveryClientException.class, IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_POLICY_VIOLATION.getCode(), cause.getMessage(), e);
                }
                cause = cause.getCause();
            }
            return handleClientException(user, e);
        }
        addUserConsent(consent, tenantDomain);
        // Build the notification response for lite user.
        notificationResponseBean = buildLiteNotificationResponseBean(user, preferredChannel, claimsMap);
    } finally {
        Utils.clearArbitraryProperties();
        PrivilegedCarbonContext.endTenantFlow();
    }
    return notificationResponseBean;
}
Also used : MalformedURLException(java.net.MalformedURLException) NotificationChannelManager(org.wso2.carbon.identity.governance.service.notification.NotificationChannelManager) NotificationChannelManagerException(org.wso2.carbon.identity.governance.exceptions.notiification.NotificationChannelManagerException) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) NotificationResponseBean(org.wso2.carbon.identity.recovery.bean.NotificationResponseBean) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PolicyViolationException(org.wso2.carbon.identity.mgt.policy.PolicyViolationException) Claim(org.wso2.carbon.user.api.Claim) IdentityRecoveryClientException(org.wso2.carbon.identity.recovery.IdentityRecoveryClientException)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)2 NotificationChannelManagerException (org.wso2.carbon.identity.governance.exceptions.notiification.NotificationChannelManagerException)2 NotificationChannelManager (org.wso2.carbon.identity.governance.service.notification.NotificationChannelManager)2 PolicyViolationException (org.wso2.carbon.identity.mgt.policy.PolicyViolationException)2 IdentityRecoveryClientException (org.wso2.carbon.identity.recovery.IdentityRecoveryClientException)2 NotificationResponseBean (org.wso2.carbon.identity.recovery.bean.NotificationResponseBean)2 Claim (org.wso2.carbon.user.api.Claim)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)2 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)2 RealmService (org.wso2.carbon.user.core.service.RealmService)2 BundleContext (org.osgi.framework.BundleContext)1 Activate (org.osgi.service.component.annotations.Activate)1 PostAuthenticationHandler (org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthenticationHandler)1 AbstractEventHandler (org.wso2.carbon.identity.event.handler.AbstractEventHandler)1 IdentityConnectorConfig (org.wso2.carbon.identity.governance.common.IdentityConnectorConfig)1