Search in sources :

Example 6 with Captcha

use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.

the class CaptchaApiImpl method generateCaptcha.

@Override
public GenerateCaptchaResult generateCaptcha(GenerateCaptchaRequest generateCaptchaRequest) {
    Preconditions.checkNotNull(generateCaptchaRequest);
    Captcha captcha = captchaService.generate();
    GenerateCaptchaResult result = new GenerateCaptchaResult();
    result.token = captcha.getToken();
    result.pngImageDataBase64 = BaseEncoding.base64().encode(captcha.getPngImageData());
    return result;
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) GenerateCaptchaResult(org.haiku.haikudepotserver.api1.model.captcha.GenerateCaptchaResult)

Example 7 with Captcha

use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.

the class SimpleMathProblemCaptchaAlgorithm method generate.

@Override
public synchronized Captcha generate() {
    int addend1 = Math.abs(random.nextInt() % 25);
    int addend2 = Math.abs(random.nextInt() % 25);
    String problem = String.format("%d + %d", addend1, addend2);
    String response = Integer.toString(addend1 + addend2);
    byte[] pngImageData;
    synchronized (this) {
        // reset the image.
        bufferedImageGraphics.setColor(Color.DARK_GRAY);
        bufferedImageGraphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
        // now render a small image using java 2d with this text in it.
        bufferedImageGraphics.setColor(Color.WHITE);
        bufferedImageGraphics.drawString(problem, (bufferedImage.getWidth() - fontMetrics.stringWidth(problem)) / 2, (bufferedImage.getHeight() + fontMetrics.getAscent()) / 2);
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
            pngImageData = byteArrayOutputStream.toByteArray();
        } catch (IOException ioe) {
            throw new IllegalStateException("unable to write png data in memory", ioe);
        }
    }
    Captcha captcha = new Captcha();
    captcha.setToken(UUID.randomUUID().toString());
    captcha.setResponse(response);
    captcha.setPngImageData(pngImageData);
    return captcha;
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Captcha (org.haiku.haikudepotserver.captcha.model.Captcha)7 ObjectContext (org.apache.cayenne.ObjectContext)4 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)4 Test (org.junit.jupiter.api.Test)4 User (org.haiku.haikudepotserver.dataobjects.User)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 List (java.util.List)1 Optional (java.util.Optional)1 GenerateCaptchaResult (org.haiku.haikudepotserver.api1.model.captcha.GenerateCaptchaResult)1 ChangePasswordRequest (org.haiku.haikudepotserver.api1.model.user.ChangePasswordRequest)1 CompletePasswordResetRequest (org.haiku.haikudepotserver.api1.model.user.CompletePasswordResetRequest)1 CreateUserRequest (org.haiku.haikudepotserver.api1.model.user.CreateUserRequest)1 CreateUserResult (org.haiku.haikudepotserver.api1.model.user.CreateUserResult)1 InitiatePasswordResetRequest (org.haiku.haikudepotserver.api1.model.user.InitiatePasswordResetRequest)1 UserPasswordResetToken (org.haiku.haikudepotserver.dataobjects.UserPasswordResetToken)1 PasswordResetException (org.haiku.haikudepotserver.passwordreset.PasswordResetException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1