use of org.wso2.carbon.captcha.mgt.beans.xsd.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);
}
use of org.wso2.carbon.captcha.mgt.beans.xsd.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);
}
}
use of org.wso2.carbon.captcha.mgt.beans.xsd.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());
}
}
}
use of org.wso2.carbon.captcha.mgt.beans.xsd.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.");
}
use of org.wso2.carbon.captcha.mgt.beans.xsd.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.");
}
}
Aggregations