Search in sources :

Example 1 with CaptchaInfoBean

use of org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean in project carbon-identity-framework by wso2.

the class UserIdentityManagementService method updateCredential.

/**
 * proceed updating credentials of user
 *
 * @param captchaInfoBean bean class that contains captcha information
 * @return True, if successful in verifying and hence updating the credentials.
 */
public VerificationBean updateCredential(String userName, String confirmation, String password, CaptchaInfoBean captchaInfoBean) {
    RecoveryProcessor recoveryProcessor = IdentityMgtServiceComponent.getRecoveryProcessor();
    if (IdentityMgtConfig.getInstance().isCaptchaVerificationInternallyManaged()) {
        try {
            CaptchaUtil.processCaptchaInfoBean(captchaInfoBean);
        } catch (Exception e) {
            log.error("Error while processing captcha bean.", e);
            return new VerificationBean(VerificationBean.ERROR_CODE_INVALID_CAPTCHA);
        }
    }
    try {
        UserDTO userDTO = Utils.processUserId(userName);
        if (recoveryProcessor.verifyConfirmationKey(confirmation).isVerified()) {
            Utils.updatePassword(userDTO.getUserId(), userDTO.getTenantId(), password);
            log.info("Credential is updated for user : " + userDTO.getUserId() + " and tenant domain : " + userDTO.getTenantDomain());
            return new VerificationBean(true);
        } else {
            log.warn("Invalid user tried to update credential with user Id : " + userDTO.getUserId() + " and tenant domain : " + userDTO.getTenantDomain());
        }
    } catch (Exception e) {
        log.error("Error while updating credential for user : " + userName, e);
    }
    return new VerificationBean(VerificationBean.ERROR_CODE_UNEXPECTED);
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 2 with CaptchaInfoBean

use of org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method getCaptcha.

public CaptchaInfoBean getCaptcha() throws IdentityMgtServiceException {
    if (log.isDebugEnabled()) {
        log.debug("User get captcha image request received");
    }
    try {
        CaptchaUtil.cleanOldCaptchas();
        CaptchaInfoBean bean = CaptchaUtil.generateCaptchaImage();
        if (log.isDebugEnabled()) {
            log.debug("Captcha stored: " + bean.getImagePath());
            log.debug("Captcha generated successfully");
        }
        return bean;
    } catch (Exception e) {
        String errorMessage = "Error while generating captcha";
        log.error(errorMessage, e);
        throw new IdentityMgtServiceException(errorMessage);
    }
}
Also used : CaptchaInfoBean(org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 3 with CaptchaInfoBean

use of org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean in project carbon-identity-framework by wso2.

the class CaptchaUtil method generateCaptchaImage.

/**
 * Generate the captcha image.
 *
 * @return CaptchaInfoBean
 * @throws Exception - no exception handling here.
 *                   Exceptions in generating the captcha are thrown as they are.
 */
public static CaptchaInfoBean generateCaptchaImage() throws Exception {
    // random string for the captcha.
    String randomSecretKey = UUID.randomUUID().toString();
    String imagePath = CaptchaMgtConstants.CAPTCHA_IMAGES_PATH + RegistryConstants.PATH_SEPARATOR + randomSecretKey + ".jpg";
    Config config = new Config(new Properties());
    Producer captchaProducer = config.getProducerImpl();
    String captchaText = captchaProducer.createText();
    BufferedImage image = captchaProducer.createImage(captchaText);
    File tempFile = File.createTempFile("temp-", ".jpg");
    try {
        ImageIO.write(image, "jpg", tempFile);
        byte[] imageBytes = CarbonUtils.getBytesFromFile(tempFile);
        // saving the image
        Registry superTenantRegistry = CaptchaMgtServiceComponent.getConfigSystemRegistry(MultitenantConstants.SUPER_TENANT_ID);
        Resource imageResource = superTenantRegistry.newResource();
        imageResource.setContent(imageBytes);
        superTenantRegistry.put(imagePath, imageResource);
        // prepare the captcha info bean
        CaptchaInfoBean captchaInfoBean = new CaptchaInfoBean();
        // random generated value as secret key
        captchaInfoBean.setSecretKey(randomSecretKey);
        captchaInfoBean.setImagePath("registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.CONFIG_REGISTRY_BASE_PATH + imagePath);
        // now create an entry in the registry on the captcha
        Resource recordResource = superTenantRegistry.newResource();
        // no need to version
        ((ResourceImpl) recordResource).setVersionableChange(false);
        recordResource.setProperty(CaptchaMgtConstants.CAPTCHA_TEXT_PROPERTY_KEY, captchaText);
        recordResource.setProperty(CaptchaMgtConstants.CAPTCHA_PATH_PROPERTY_KEY, imagePath);
        superTenantRegistry.put(CaptchaMgtConstants.CAPTCHA_DETAILS_PATH + RegistryConstants.PATH_SEPARATOR + randomSecretKey, recordResource);
        if (log.isDebugEnabled()) {
            log.debug("Successfully generated the captcha image.");
        }
        return captchaInfoBean;
    } finally {
        if (!tempFile.delete()) {
            log.warn("Could not delete " + tempFile.getAbsolutePath());
        }
    }
}
Also used : CaptchaInfoBean(org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean) Producer(com.google.code.kaptcha.Producer) Config(com.google.code.kaptcha.util.Config) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Properties(java.util.Properties) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 4 with CaptchaInfoBean

use of org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean in project product-is by wso2.

the class UserInformationRecoveryServiceTestCase method testGetCaptcha.

@SetEnvironment(executionEnvironments = { ExecutionEnvironment.ALL })
@Test(groups = "wso2.is", description = "Check get captcha", dependsOnMethods = "testListUsers")
public void testGetCaptcha() throws Exception {
    CaptchaInfoBean bean = infoRecoveryClient.getCaptcha();
    Assert.assertNotNull(bean, "Getting the captcha call failed with null return");
    Assert.assertNotNull(bean.getImagePath(), "Getting image path from captcha has failed.");
    Assert.assertNotNull(bean.getSecretKey(), "Getting secret key from captcha has failed.");
}
Also used : CaptchaInfoBean(org.wso2.carbon.captcha.mgt.beans.xsd.CaptchaInfoBean) SetEnvironment(org.wso2.carbon.automation.engine.annotations.SetEnvironment) Test(org.testng.annotations.Test) ISIntegrationTest(org.wso2.identity.integration.common.utils.ISIntegrationTest)

Example 5 with CaptchaInfoBean

use of org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean in project carbon-identity-framework by wso2.

the class CaptchaUtil method validateCaptcha.

/**
 * Validates that the user entered the correct string displayed in the captcha.
 *
 * @param captchaInfoBean Captcha details
 * @throws Exception, if the captcha validation fails, it will throw exception.
 *                    Method completes successfully if the user input matches the captcha image shown.
 */
public static void validateCaptcha(CaptchaInfoBean captchaInfoBean) throws Exception {
    // user's answer for the captcha
    String userAnswer = captchaInfoBean.getUserAnswer();
    if (userAnswer.equals("")) {
        // if no user answer given we will throw an error
        String msg = CaptchaMgtConstants.CAPTCHA_ERROR_MSG + " User has not answered to captcha text.";
        log.error(msg);
        throw new Exception(msg);
    }
    // gets the random generated secret key.
    String secretKey = captchaInfoBean.getSecretKey();
    String recordPath = CaptchaMgtConstants.CAPTCHA_DETAILS_PATH + RegistryConstants.PATH_SEPARATOR + secretKey;
    Registry superTenantRegistry = CaptchaMgtServiceComponent.getConfigSystemRegistry(MultitenantConstants.SUPER_TENANT_ID);
    if (!superTenantRegistry.resourceExists(recordPath)) {
        String msg = "The captcha details are not available.";
        log.error(msg);
        throw new Exception(msg);
    }
    Resource resource = superTenantRegistry.get(recordPath);
    String captchaText = resource.getProperty(CaptchaMgtConstants.CAPTCHA_TEXT_PROPERTY_KEY);
    if (captchaText == null) {
        String msg = "The captcha details are not available.";
        log.error(msg);
        throw new Exception(msg);
    }
    if (!captchaText.equals(userAnswer)) {
        // wrong user input
        String msg = CaptchaMgtConstants.CAPTCHA_ERROR_MSG + " The user's answer doesn't match the captcha text.";
        log.error(msg);
        throw new Exception(msg);
    }
    // if all goes well, we will reach here.
    if (log.isDebugEnabled()) {
        log.debug("Successfully validated the captcha.");
    }
}
Also used : UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) UserStoreException(org.wso2.carbon.user.core.UserStoreException)

Aggregations

IdentityException (org.wso2.carbon.identity.base.IdentityException)6 IdentityMgtServiceException (org.wso2.carbon.identity.mgt.IdentityMgtServiceException)6 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 RecoveryProcessor (org.wso2.carbon.identity.mgt.RecoveryProcessor)5 VerificationBean (org.wso2.carbon.identity.mgt.beans.VerificationBean)5 UserDTO (org.wso2.carbon.identity.mgt.dto.UserDTO)5 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)3 CaptchaInfoBean (org.wso2.carbon.captcha.mgt.beans.CaptchaInfoBean)2 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)2 Producer (com.google.code.kaptcha.Producer)1 Config (com.google.code.kaptcha.util.Config)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 Properties (java.util.Properties)1 Test (org.testng.annotations.Test)1 SetEnvironment (org.wso2.carbon.automation.engine.annotations.SetEnvironment)1 CaptchaInfoBean (org.wso2.carbon.captcha.mgt.beans.xsd.CaptchaInfoBean)1 NotificationDataDTO (org.wso2.carbon.identity.mgt.dto.NotificationDataDTO)1 UserRecoveryDTO (org.wso2.carbon.identity.mgt.dto.UserRecoveryDTO)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1