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.

103 lines
3.7 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. // FIXME must change this to a timeout base cache otherwise memory leak!
  36. private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
  37. @GetMapping("/registration")
  38. public String registrationForm(Model model) {
  39. UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
  40. String captchaText = captchaService.getRandomText();
  41. userRegistrationDTO.setCaptchaText(captchaText);
  42. log.debug("captcha text = {}", captchaText);
  43. Long captchaID = ThreadLocalRandom.current().nextLong();
  44. userRegistrationDTO.setCaptchaID(captchaID);
  45. captchaMap.put(captchaID, captchaText);
  46. model.addAttribute("userRegistrationDTO", userRegistrationDTO);
  47. return "registration";
  48. }
  49. @PostMapping("/perform_registration")
  50. public String performRegistration(
  51. @ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
  52. BindingResult bindingResult) {
  53. if (bindingResult.hasErrors()) {
  54. log.warn("Registration input has errors!");
  55. return "redirect:registration?error";
  56. }
  57. if (userService.getUser(userRegistrationDTO.getUserName())
  58. .isPresent()) {
  59. return "redirect:registration?error&duplicate=true";
  60. }
  61. log.debug("Captcha text from user input = {}",
  62. userRegistrationDTO.getCaptchaInput());
  63. log.debug("Captcha text from captcha map = {}",
  64. captchaMap.get(userRegistrationDTO.getCaptchaID()));
  65. if (userRegistrationDTO.getCaptchaInput()
  66. .equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) {
  67. log.info("Registration captcha equal success");
  68. userService.createUser(userRegistrationDTO);
  69. return "redirect:registration?success";
  70. } else {
  71. log.warn("Registration captcha equal fail");
  72. return "redirect:registration?error&captchaError=true";
  73. }
  74. }
  75. @GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE)
  76. public ResponseEntity<byte[]> getImage(
  77. @PathVariable("image_id") Long imageId) throws IOException {
  78. final String captchaText = captchaMap.get(imageId);
  79. final HttpHeaders headers = new HttpHeaders();
  80. headers.setContentType(MediaType.IMAGE_PNG);
  81. BufferedImage captchaBufferedImage = captchaService
  82. .createCaptchaImage(captchaText);
  83. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  84. ImageIO.write(captchaBufferedImage, "png", baos);
  85. byte[] imageBytes = baos.toByteArray();
  86. return new ResponseEntity<byte[]>(imageBytes, headers, HttpStatus.OK);
  87. }
  88. }