From c66017c984a95022e8cb90550df9011316f45032 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 3 Mar 2020 10:55:35 +0100 Subject: [PATCH] Added email support to login --- .../controller/AuthenticationController.java | 24 +++++++++++++++---- .../sanmarinoes/smarthut/dto/JWTRequest.java | 10 ++++---- .../smarthut/dto/UserRegistrationRequest.java | 3 +++ .../smarthut/error/UserNotFoundException.java | 11 +++++++++ .../sa4/sanmarinoes/smarthut/models/User.java | 19 --------------- 5 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UserNotFoundException.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java index cc9ed2e..5fd000b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java @@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtil; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserUpdateRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import io.swagger.annotations.Authorization; import java.security.Principal; @@ -35,8 +36,7 @@ public class AuthenticationController { AuthenticationManager authenticationManager, UserRepository userRepository, JWTTokenUtil jwtTokenUtil, - JWTUserDetailsService userDetailsService, - UserRepository users) { + JWTUserDetailsService userDetailsService) { this.authenticationManager = authenticationManager; this.userRepository = userRepository; this.jwtTokenUtil = jwtTokenUtil; @@ -45,9 +45,25 @@ public class AuthenticationController { @PostMapping("/login") public JWTResponse login(@RequestBody JWTRequest authenticationRequest) throws Exception { - authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword()); + if (authenticationRequest.getUsernameOrEmail().contains("@")) { + // usernameOrEmail contains an email, so fetch the corresponding username + final User user = + userRepository.findByEmailIgnoreCase( + authenticationRequest.getUsernameOrEmail()); + if (user == null) { + throw new UserNotFoundException(); + } + + authenticate(user.getUsername(), authenticationRequest.getPassword()); + } else { + // usernameOrEmail contains a username, authenticate with that then + authenticate( + authenticationRequest.getUsernameOrEmail(), + authenticationRequest.getPassword()); + } + final UserDetails userDetails = - userDetailsService.loadUserByUsername(authenticationRequest.getUsername()); + userDetailsService.loadUserByUsername(authenticationRequest.getUsernameOrEmail()); final String token = jwtTokenUtil.generateToken(userDetails); return new JWTResponse(token); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java index d750a50..a032c05 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java @@ -1,15 +1,15 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; public class JWTRequest { - private String username; + private String usernameOrEmail; private String password; - public String getUsername() { - return this.username; + public String getUsernameOrEmail() { + return this.usernameOrEmail; } - public void setUsername(String username) { - this.username = username; + public void setUsernameOrEmail(String usernameOrEmail) { + this.usernameOrEmail = usernameOrEmail; } public String getPassword() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/UserRegistrationRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/UserRegistrationRequest.java index b41720a..785d408 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/UserRegistrationRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/UserRegistrationRequest.java @@ -12,6 +12,9 @@ public class UserRegistrationRequest { /** The full name of the user */ @NotNull @NotEmpty(message = "Please provide a username") + @Pattern( + regexp = "[A-Za-z0-9_\\-]+", + message = "Username can contain only letters, numbers, '_' and '-'") private String username; /** A properly salted way to store the password */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UserNotFoundException.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UserNotFoundException.java new file mode 100644 index 0000000..d2e93f6 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UserNotFoundException.java @@ -0,0 +1,11 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.error; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.BAD_REQUEST) +public class UserNotFoundException extends Exception { + public UserNotFoundException() { + super("No user found with given email"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java index 67f95d4..ba86712 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -2,11 +2,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import io.swagger.annotations.ApiModelProperty; import javax.persistence.*; -import javax.validation.constraints.Email; -import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Pattern; -import javax.validation.constraints.Size; /** A user of the Smarthut application */ @Entity(name = "smarthutuser") @@ -19,25 +14,15 @@ public class User { private Long id; /** The full name of the user */ - @NotNull @Column(nullable = false) - @NotEmpty(message = "Please provide a full name") private String name; /** The full name of the user */ - @NotNull @Column(nullable = false) - @NotEmpty(message = "Please provide a username") private String username; /** A properly salted way to store the password */ - @NotNull @Column(nullable = false) - @NotEmpty(message = "Please provide a password") - @Size( - min = 6, - max = 255, - message = "Your password should be at least 6 characters long and up to 255 chars long") private String password; /** @@ -45,10 +30,6 @@ public class User { * , technically not RFC 5322 compliant */ @Column(nullable = false, unique = true) - @NotNull - @NotEmpty(message = "Please provide an email") - @Email(message = "Please provide a valid email address") - @Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address") private String email; @Column(nullable = false)