A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package org.ros.chatto.controller;
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.util.Map;
  6. import java.util.concurrent.ConcurrentHashMap;
  7. import java.util.concurrent.ThreadLocalRandom;
  8. import javax.imageio.ImageIO;
  9. import javax.validation.Valid;
  10. import org.ros.chatto.dto.UserRegistrationDTO;
  11. import org.ros.chatto.service.CaptchaService;
  12. import org.ros.chatto.service.UserService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.HttpHeaders;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.http.MediaType;
  17. import org.springframework.http.ResponseEntity;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.validation.BindingResult;
  21. import org.springframework.web.bind.annotation.GetMapping;
  22. import org.springframework.web.bind.annotation.ModelAttribute;
  23. import org.springframework.web.bind.annotation.PathVariable;
  24. import org.springframework.web.bind.annotation.PostMapping;
  25. import lombok.RequiredArgsConstructor;
  26. import lombok.extern.slf4j.Slf4j;
  27. @Controller
  28. @RequiredArgsConstructor
  29. @Slf4j
  30. public class RegistrationController {
  31. @Autowired
  32. private final UserService userService;
  33. @Autowired
  34. private final CaptchaService captchaService;
  35. private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
  36. @GetMapping("/registration")
  37. public String registrationForm(Model model) {
  38. UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
  39. String captchaText = captchaService.getRandomText();
  40. userRegistrationDTO.setCaptchaText(captchaText);
  41. log.debug("captcha text = {}", captchaText);
  42. Long captchaID = ThreadLocalRandom.current().nextLong();
  43. userRegistrationDTO.setCaptchaID(captchaID);
  44. captchaMap.put(captchaID, captchaText);
  45. model.addAttribute("userRegistrationDTO", userRegistrationDTO);
  46. return "registration";
  47. }
  48. @PostMapping("/perform_registration")
  49. public String performRegistration(
  50. @ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
  51. BindingResult bindingResult) {
  52. if (bindingResult.hasErrors()) {
  53. log.warn("Registration input has errors!");
  54. return "registration";
  55. }
  56. log.debug("Captcha text from user input = {}",
  57. userRegistrationDTO.getCaptchaInput());
  58. log.debug("Captcha text from captcha map = {}",
  59. captchaMap.get(userRegistrationDTO.getCaptchaID()));
  60. if (userRegistrationDTO.getCaptchaInput()
  61. .equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) {
  62. log.info("Registration captcha equal success");
  63. userService.createUser(userRegistrationDTO);
  64. return "redirect:registration?success";
  65. } else {
  66. log.warn("Registration captcha equal fail");
  67. return "redirect:registration?error";
  68. }
  69. }
  70. @GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE)
  71. public ResponseEntity<byte[]> getImage(
  72. @PathVariable("image_id") Long imageId) throws IOException {
  73. final String captchaText = captchaMap.get(imageId);
  74. final HttpHeaders headers = new HttpHeaders();
  75. headers.setContentType(MediaType.IMAGE_PNG);
  76. BufferedImage captchaBufferedImage = captchaService
  77. .createCaptchaImage(captchaText);
  78. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  79. ImageIO.write(captchaBufferedImage, "png", baos);
  80. byte[] imageBytes = baos.toByteArray();
  81. return new ResponseEntity<byte[]>(imageBytes, headers, HttpStatus.OK);
  82. }
  83. }