Added email support to login

This commit is contained in:
Claudio Maggioni 2020-03-03 10:55:35 +01:00
parent eede89b7be
commit c66017c984
5 changed files with 39 additions and 28 deletions

View File

@ -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);
}

View File

@ -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() {

View File

@ -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 */

View File

@ -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");
}
}

View File

@ -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 {
* </code>, 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)