|
|
@@ -1,28 +1,105 @@
|
|
|
package org.example.controller;
|
|
|
|
|
|
-import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
+
|
|
|
+import org.apache.commons.validator.routines.EmailValidator;
|
|
|
import org.example.domain.LoginRequest;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.example.domain.LoginRequestCaptcha;
|
|
|
+import org.example.domain.Role;
|
|
|
+import org.example.domain.User;
|
|
|
+import org.example.repository.RoleRepository;
|
|
|
+import org.example.repository.UserRepository;
|
|
|
+import org.example.service.EmailServiceImpl;
|
|
|
+import org.example.service.ReCaptchaService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
|
|
|
@RestController
|
|
|
public class LoginController {
|
|
|
- @RequestMapping(value="/login")
|
|
|
- public String login() {
|
|
|
- long currentTimeMillis = System.currentTimeMillis();
|
|
|
- return Jwts.builder()
|
|
|
- .setSubject("user.getLogin()")
|
|
|
- .claim("roles","user")
|
|
|
- .setIssuedAt(new Date(currentTimeMillis))
|
|
|
- .setExpiration(new Date(currentTimeMillis + 60 * 60 * 1000)) //60min
|
|
|
- .signWith(SignatureAlgorithm.HS256, "example")
|
|
|
- .compact();
|
|
|
+ @Autowired
|
|
|
+ UserRepository userRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ EmailServiceImpl emailServiceImpl;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleRepository roleRepository;
|
|
|
+ ReCaptchaService reCaptchaService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setReCaptchaService(ReCaptchaService reCaptchaService) {
|
|
|
+ this.reCaptchaService = reCaptchaService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/login")
|
|
|
+ public String login(@RequestBody LoginRequestCaptcha user) {
|
|
|
+ BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
|
|
+ User foundUser = userRepository.findByLogin(user.getLogin());
|
|
|
+
|
|
|
+ if (foundUser == null || !foundUser.isActive() || !validateLogin(user)) {
|
|
|
+ return "";
|
|
|
+ } else if (bCryptPasswordEncoder.matches(user.getPassword(), foundUser.getPassword()) && reCaptchaService.verify(user.getCaptcha())) {
|
|
|
+ long currentTimeMillis = System.currentTimeMillis();
|
|
|
+ return Jwts.builder().setSubject(user.getLogin()).claim("roles", "user").setIssuedAt(new Date(currentTimeMillis)).setExpiration(new Date(currentTimeMillis + 60 * 60 * 1000)) //60min
|
|
|
+ .signWith(SignatureAlgorithm.HS256, "example").compact();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean validateLogin(LoginRequestCaptcha user) {
|
|
|
+ if (user.getLogin() == null || user.getPassword() == null) return false;
|
|
|
+ else return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/register")
|
|
|
+ public ResponseEntity<String> register(@RequestBody LoginRequest user) {
|
|
|
+ if (!validateRegister(user))
|
|
|
+ return new ResponseEntity<>("Invalid data", HttpStatus.BAD_REQUEST);
|
|
|
+ User foundUser = userRepository.findByLogin(user.getLogin());
|
|
|
+ if (foundUser == null) {
|
|
|
+ BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
|
|
+
|
|
|
+ User newUser = new User(user.getLogin(), bCryptPasswordEncoder.encode(user.getPassword()), user.getEmail(), new ArrayList<>(), new ArrayList<>());
|
|
|
+ userRepository.save(newUser);
|
|
|
+
|
|
|
+ Role userRole = roleRepository.findByRoleName("User");
|
|
|
+ newUser.getRoles().add(userRole);
|
|
|
+
|
|
|
+ userRepository.save(newUser);
|
|
|
+ emailServiceImpl.sendMail(newUser.getEmail(), "http://localhost:8080/activation?user=" + newUser.getLogin() + "&token=" +
|
|
|
+ newUser.getActivationToken(), "activation");
|
|
|
+
|
|
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
+ } else {
|
|
|
+ return new ResponseEntity<>("User with that name was found", HttpStatus.BAD_REQUEST);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean validateRegister(LoginRequest user) {
|
|
|
+ EmailValidator emailValidator = EmailValidator.getInstance();
|
|
|
+
|
|
|
+ if (user.getLogin() == null || user.getEmail() == null || user.getPassword() == null) return false;
|
|
|
+ else if (user.getLogin().length() < 4 || user.getLogin().length() > 18) return false;
|
|
|
+ else if (user.getPassword().length() < 3 || user.getPassword().length() > 18) return false;
|
|
|
+ else if (user.getEmail().length() < 4 || user.getEmail().length() > 25) return false;
|
|
|
+ else if (!emailValidator.isValid(user.getEmail())) return false;
|
|
|
+ else return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/activation")
|
|
|
+ public void activation(@RequestParam String user, @RequestParam String token) {
|
|
|
+ User foundUser = userRepository.findByLogin(user);
|
|
|
+ if (foundUser.getActivationToken().equals(token)) {
|
|
|
+ foundUser.setActive(true);
|
|
|
+ userRepository.save(foundUser);
|
|
|
+ }
|
|
|
}
|
|
|
}
|