From e8a55ae13185c844fa6fbd2749a66a9908bb618e Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 5 Mar 2020 12:42:53 +0100 Subject: [PATCH 1/5] Started work on tests --- build.gradle | 15 +- .../controller/AuthenticationController.java | 11 +- .../smarthut/error/UnauthorizedException.java | 18 ++ .../smarthut/AuthenticationTests.java | 171 ++++++++++++++++++ .../sanmarinoes/smarthut/SmartHutTest.java | 25 +++ .../smarthut/SmarthutApplicationTests.java | 25 ++- src/test/resources/application.properties | 27 +++ 7 files changed, 277 insertions(+), 15 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UnauthorizedException.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java create mode 100644 src/test/resources/application.properties diff --git a/build.gradle b/build.gradle index 086613b..25e4080 100644 --- a/build.gradle +++ b/build.gradle @@ -23,16 +23,23 @@ dependencies { implementation 'io.jsonwebtoken:jjwt:0.9.1' implementation 'org.springframework.security:spring-security-web' implementation 'org.postgresql:postgresql' - compile "io.springfox:springfox-swagger2:2.9.2" - compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2' - + implementation 'com.google.code.gson:gson' + compile 'io.springfox:springfox-swagger2:2.9.2' + compile 'io.springfox:springfox-swagger-ui:2.9.2' + implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json' } - implementation 'com.google.code.gson:gson' + testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } + + testImplementation 'org.springframework.security:spring-security-test' + testImplementation 'com.h2database:h2:1.3.148' + + // Fixes https://stackoverflow.com/a/60455550 + testImplementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.11' } test { 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 f0e8d7d..1a1e266 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.UnauthorizedException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import io.swagger.annotations.Authorization; @@ -44,7 +45,7 @@ public class AuthenticationController { @PostMapping("/login") public JWTResponse login(@Valid @RequestBody JWTRequest authenticationRequest) - throws Exception { + throws UnauthorizedException, UserNotFoundException { final UserDetails userDetails; if (authenticationRequest.getUsernameOrEmail().contains("@")) { // usernameOrEmail contains an email, so fetch the corresponding username @@ -86,16 +87,14 @@ public class AuthenticationController { return userRepository.save(oldUser); } - private void authenticate(String username, String password) throws Exception { + private void authenticate(String username, String password) throws UnauthorizedException { try { authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(username, password)); } catch (DisabledException e) { - e.printStackTrace(); - throw new Exception("USER_DISABLED", e); + throw new UnauthorizedException(true); } catch (BadCredentialsException e) { - e.printStackTrace(); - throw new Exception("INVALID_CREDENTIALS", e); + throw new UnauthorizedException(false); } } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UnauthorizedException.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UnauthorizedException.java new file mode 100644 index 0000000..9176df6 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/error/UnauthorizedException.java @@ -0,0 +1,18 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.error; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.UNAUTHORIZED) +public class UnauthorizedException extends Exception { + private final boolean isUserDisabled; + + public UnauthorizedException(boolean isDisabled) { + super("Access denied: " + (isDisabled ? "user is disabled" : "wrong credentials")); + this.isUserDisabled = isDisabled; + } + + public boolean isUserDisabled() { + return isUserDisabled; + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java new file mode 100644 index 0000000..96b67ee --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java @@ -0,0 +1,171 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.assertj.core.api.Assertions.assertThat; + +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.OkResponse; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@AutoConfigureMockMvc +public class AuthenticationTests extends SmartHutTest { + + @Autowired private TestRestTemplate restTemplate; + + @Override + protected void setUp() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setName("Disabled User"); + request.setEmail("disabled@example.com"); + request.setUsername("disabled"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(this.url("/register"), request, OkResponse.class); + assertThat(res.getStatusCode().equals(HttpStatus.OK)); + + final UserRegistrationRequest request2 = new UserRegistrationRequest(); + request2.setName("Enabled User"); + request2.setEmail("enabled@example.com"); + request2.setUsername("enabled"); + request2.setPassword("password"); + + final ResponseEntity res2 = + this.restTemplate.postForEntity(this.url("/register"), request, OkResponse.class); + assertThat(res2.getStatusCode().equals(HttpStatus.OK)); + + // TODO: email confirmation for res2 + } + + @Test + public void registrationShouldReturnBadRequestWithIncorrectFields() { + final Map badJSON = Map.of("luciano", "goretti", "danilo", "malusa"); + + assertThat( + this.restTemplate + .postForEntity(url("/register"), badJSON, JWTResponse.class) + .getStatusCode() + .equals(HttpStatus.BAD_REQUEST)); + } + + @Test + public void registrationShouldReturnBadRequestWithShortPassword() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setName("Mario Goretti"); + request.setEmail("test@example.com"); + request.setUsername("mgo"); + request.setPassword("passw"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + + final JsonArray errors = res.getBody().getAsJsonArray("errors"); + assertThat(errors.size() == 1); + assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("password")); + } + + @Test + public void registrationShouldReturnBadRequestWithWrongEmail() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setName("Mario Goretti"); + request.setEmail("test@example"); + request.setUsername("mgo"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + + final JsonArray errors = res.getBody().getAsJsonArray("errors"); + assertThat(errors.size() == 1); + assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("email")); + } + + @Test + public void registrationShouldReturnBadRequestWithNoName() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setEmail("test@example.com"); + request.setUsername("mgo"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + + final JsonArray errors = res.getBody().getAsJsonArray("errors"); + assertThat(errors.size() == 1); + assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("name")); + } + + @Test + public void registrationShouldReturnBadRequestWithNoUsername() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setName("Mario Goretti"); + request.setEmail("test@example.com"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/register"), request, JsonObject.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + + final JsonArray errors = res.getBody().getAsJsonArray("errors"); + assertThat(errors.size() == 1); + assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("username")); + } + + @Test + public void loginShouldReturnBadRequestWithIncorrectFields() { + final Map badJSON = Map.of("badkey", 3, "password", "ciaomamma"); + + assertThat( + this.restTemplate + .postForEntity(url("/auth/login"), badJSON, JWTResponse.class) + .getStatusCode() + .equals(HttpStatus.BAD_REQUEST)); + } + + @Test + public void loginShouldReturnUnauthorizedWithNonExistantUser() { + final JWTRequest request = new JWTRequest(); + request.setUsernameOrEmail("roberto"); + request.setPassword("ciaomamma"); + + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/auth/login"), request, UnauthorizedException.class); + assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)); + assertThat(res.getBody() != null); + assertThat(!res.getBody().isUserDisabled()); + } + + @Test + public void loginShouldReturnUnauthorizedWithDisabledUser() { + final JWTRequest request = new JWTRequest(); + request.setUsernameOrEmail("disabled"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/auth/login"), request, UnauthorizedException.class); + assertThat(res.getStatusCode().equals(HttpStatus.UNAUTHORIZED)); + assertThat(res.getBody() != null); + assertThat(res.getBody().isUserDisabled()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java new file mode 100644 index 0000000..5c6e097 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java @@ -0,0 +1,25 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import org.junit.jupiter.api.BeforeEach; + +public abstract class SmartHutTest { + private boolean setupDone = false; + + protected final String getBaseURL() { + return "http://localhost:2000/"; + } + + protected final String url(final String url) { + return getBaseURL() + url; + } + + protected void setUp() {} + + @BeforeEach + void setUpHack() { + if (!setupDone) { + setUp(); + setupDone = true; + } + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java index 5f1f8fd..dbd7e21 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java @@ -1,11 +1,26 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; +import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest -class SmarthutApplicationTests { +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@AutoConfigureMockMvc +public class SmarthutApplicationTests extends SmartHutTest { + + @Autowired private TestRestTemplate restTemplate; @Test - void contextLoads() {} + public void anonymousGreetingShouldNotBeAuthorized() throws Exception { + assertThat( + this.restTemplate + .getForEntity(getBaseURL(), Void.class) + .getStatusCode() + .equals(HttpStatus.UNAUTHORIZED)); + } } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..5022558 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,27 @@ +spring.http.converters.preferred-json-mapper=gson +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +spring.datasource.username=sa +spring.datasource.password=sa + +# Hibernate properties +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl +spring.jpa.properties.hibernate.format_sql=true + +jwt.secret=thiskeymustbeverylongorthethingcomplainssoiamjustgoingtowritehereabunchofgarbageciaomamma + +spring.mail.test-connection=true +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.username=smarthut.sm@gmail.com +spring.mail.password=dcadvbagqfkwbfts +spring.mail.properties.mail.smtp.starttls.required=true +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.connectiontimeout=5000 +spring.mail.properties.mail.smtp.timeout=5000 +spring.mail.properties.mail.smtp.writetimeout=5000 + +server.port = 2000 \ No newline at end of file From 748c5ba4b4cc9a4427b00f9c7344723d695046b1 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 5 Mar 2020 15:03:02 +0100 Subject: [PATCH 2/5] Added more tests on login/registration --- .../sanmarinoes/smarthut/dto/JWTRequest.java | 18 ++-- .../smarthut/AuthenticationTests.java | 92 ++++++++++++++++--- 2 files changed, 89 insertions(+), 21 deletions(-) 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 52fa5c3..da11bc3 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,8 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; +import javax.validation.constraints.NotNull; + public class JWTRequest { - private String usernameOrEmail; - private String password; + @NotNull private String usernameOrEmail; + @NotNull private String password; public String getUsernameOrEmail() { return this.usernameOrEmail; @@ -22,9 +24,13 @@ public class JWTRequest { @Override public String toString() { - return "JWTRequest{" + - "usernameOrEmail='" + usernameOrEmail + '\'' + - ", password='" + password + '\'' + - '}'; + return "JWTRequest{" + + "usernameOrEmail='" + + usernameOrEmail + + '\'' + + ", password='" + + password + + '\'' + + '}'; } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java index 96b67ee..60761cd 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java @@ -6,6 +6,7 @@ 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.OkResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateRegistrationException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -24,29 +25,37 @@ public class AuthenticationTests extends SmartHutTest { @Autowired private TestRestTemplate restTemplate; + private UserRegistrationRequest getDisabledUser() { + final UserRegistrationRequest disabledUser = new UserRegistrationRequest(); + disabledUser.setName("Disabled User"); + disabledUser.setEmail("disabled@example.com"); + disabledUser.setUsername("disabled"); + disabledUser.setPassword("password"); + return disabledUser; + } + + private static final UserRegistrationRequest enabledUser = new UserRegistrationRequest(); + + static { + enabledUser.setName("Enabled User"); + enabledUser.setEmail("enabled@example.com"); + enabledUser.setUsername("enabled"); + enabledUser.setPassword("password"); + } + @Override protected void setUp() { - final UserRegistrationRequest request = new UserRegistrationRequest(); - request.setName("Disabled User"); - request.setEmail("disabled@example.com"); - request.setUsername("disabled"); - request.setPassword("password"); - final ResponseEntity res = - this.restTemplate.postForEntity(this.url("/register"), request, OkResponse.class); + this.restTemplate.postForEntity( + this.url("/register"), getDisabledUser(), OkResponse.class); assertThat(res.getStatusCode().equals(HttpStatus.OK)); - final UserRegistrationRequest request2 = new UserRegistrationRequest(); - request2.setName("Enabled User"); - request2.setEmail("enabled@example.com"); - request2.setUsername("enabled"); - request2.setPassword("password"); - final ResponseEntity res2 = - this.restTemplate.postForEntity(this.url("/register"), request, OkResponse.class); + this.restTemplate.postForEntity( + this.url("/register"), enabledUser, OkResponse.class); assertThat(res2.getStatusCode().equals(HttpStatus.OK)); - // TODO: email confirmation for res2 + // TODO: email confirmation for enabledUser } @Test @@ -130,6 +139,59 @@ public class AuthenticationTests extends SmartHutTest { assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("username")); } + @Test + public void registrationShouldReturnBadRequestWithDuplicateData() { + { + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/register"), + getDisabledUser(), + DuplicateRegistrationException.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + } + + { + final UserRegistrationRequest disabledUserDifferentMail = getDisabledUser(); + enabledUser.setEmail("another@example.com"); + + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/register"), + disabledUserDifferentMail, + DuplicateRegistrationException.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + } + + { + final UserRegistrationRequest disabledUserDifferentUsername = getDisabledUser(); + enabledUser.setUsername("another"); + + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/register"), + disabledUserDifferentUsername, + DuplicateRegistrationException.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + } + } + + @Test + public void registrationShouldReturnOkWithCorrectData() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setName("Registration Test"); + request.setUsername("smarthut"); + request.setEmail("smarthut.sm@example.com"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/register"), request, OkResponse.class); + assertThat(res.getStatusCode().equals(HttpStatus.OK)); + assertThat(res.getBody() != null); + } + @Test public void loginShouldReturnBadRequestWithIncorrectFields() { final Map badJSON = Map.of("badkey", 3, "password", "ciaomamma"); From 7396e6f0d949cecb48f63663855c25aee43a673c Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 5 Mar 2020 17:07:29 +0100 Subject: [PATCH 3/5] Added support for password reset (still needs testing) --- .../smarthut/config/WebSecurityConfig.java | 3 +- .../controller/UserAccountController.java | 110 ++++++++++++++---- .../dto/InitPasswordResetRequest.java | 25 ++++ .../smarthut/dto/PasswordResetRequest.java | 34 ++++++ .../smarthut/models/ConfirmationToken.java | 14 ++- src/main/resources/application.properties | 10 +- 6 files changed, 171 insertions(+), 25 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java index e38d0df..253998d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java @@ -52,10 +52,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .authorizeRequests() .antMatchers( "/auth/login", - "/auth/register", "/swagger-ui.html", "/register", "/register/confirm-account", + "/register/init-reset-password", + "/register/reset-password", "/v2/api-docs", "/webjars/**", "/swagger-resources/**", diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java index e238f16..4959e79 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java @@ -1,9 +1,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.InitPasswordResetRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.OkResponse; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.PasswordResetRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateRegistrationException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.EmailTokenNotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationToken; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; @@ -12,15 +15,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.service.EmailSenderService; import javax.validation.Valid; import javax.validation.constraints.NotNull; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.mail.SimpleMailMessage; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.web.bind.annotation.GetMapping; -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.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration @@ -35,6 +34,36 @@ public class UserAccountController { @Autowired private BCryptPasswordEncoder encoder; + @Value("email.registrationsubject") + private String emailRegistrationSubject; + + @Value("email.resetpasswordsubject") + private String resetPasswordSubject; + + @Value("email.registration") + private String emailRegistrationText; + + @Value("email.resetpassword") + private String resetPasswordText; + + @Value("email.serverhost") + private String serverHost; + + private void sendEmail(String email, ConfirmationToken token, boolean isRegistration) { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setTo(email); + mailMessage.setSubject(isRegistration ? emailRegistrationSubject : resetPasswordSubject); + mailMessage.setFrom("smarthut.sm@gmail.com"); + mailMessage.setText( + (isRegistration ? emailRegistrationText : resetPasswordText) + + " " + + serverHost + + "/register/confirm-account?token=" + + token.getConfirmationToken()); + + emailSenderService.sendEmail(mailMessage); + } + @PostMapping public OkResponse registerUser(@Valid @RequestBody UserRegistrationRequest registrationData) throws DuplicateRegistrationException { @@ -60,35 +89,72 @@ public class UserAccountController { toSave.setEmail(registrationData.getEmail()); userRepository.save(toSave); - ConfirmationToken confirmationToken = new ConfirmationToken(toSave); + ConfirmationToken token; + do { + token = new ConfirmationToken(toSave); + } while (confirmationTokenRepository.findByConfirmationToken( + token.getConfirmationToken()) + != null); - confirmationTokenRepository.save(confirmationToken); + confirmationTokenRepository.save(token); - SimpleMailMessage mailMessage = new SimpleMailMessage(); - mailMessage.setTo(registrationData.getEmail()); - mailMessage.setSubject("Complete Registration!"); - mailMessage.setFrom("smarthut.sm@gmail.com"); - mailMessage.setText( - "To confirm your account, please click here : " - + "http://localhost:8080/register/confirm-account?token=" - + confirmationToken.getConfirmationToken()); - - emailSenderService.sendEmail(mailMessage); + sendEmail(toSave.getEmail(), token, true); return new OkResponse(); } } + @PostMapping("/init-reset-password") + public OkResponse initResetPassword(@Valid @RequestBody InitPasswordResetRequest resetRequest) + throws UserNotFoundException { + final User toReset = userRepository.findByEmailIgnoreCase(resetRequest.getEmail()); + + // Check if an User with the same email already exists + if (toReset == null) { + throw new UserNotFoundException(); + } + + ConfirmationToken token; + do { + token = new ConfirmationToken(toReset); + token.setResetPassword(true); + } while (confirmationTokenRepository.findByConfirmationToken(token.getConfirmationToken()) + != null); + + confirmationTokenRepository.save(token); + + sendEmail(toReset.getEmail(), token, false); + + return new OkResponse(); + } + + @PutMapping("/reset-password") + public OkResponse resetPassword(@Valid @RequestBody PasswordResetRequest resetRequest) + throws EmailTokenNotFoundException { + final ConfirmationToken token = + confirmationTokenRepository.findByConfirmationToken( + resetRequest.getConfirmationToken()); + + if (token == null || token.getResetPassword()) { + throw new EmailTokenNotFoundException(); + } + + final User user = token.getUser(); + user.setPassword(encoder.encode(resetRequest.getPassword())); + userRepository.save(user); + + return new OkResponse(); + } + @GetMapping(value = "/confirm-account") public OkResponse confirmUserAccount(@RequestParam("token") @NotNull String confirmationToken) throws EmailTokenNotFoundException { final ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken(confirmationToken); - if (token != null) { - final User user = userRepository.findByEmailIgnoreCase(token.getUser().getEmail()); - user.setEnabled(true); - userRepository.save(user); + if (token != null && !token.getResetPassword()) { + token.getUser().setEnabled(true); + userRepository.save(token.getUser()); // TODO: redirect to frontend return new OkResponse(); } else { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java new file mode 100644 index 0000000..d82c4f0 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java @@ -0,0 +1,25 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.Pattern; + +/** DTO for password reset request */ +public class InitPasswordResetRequest { + /** + * The user's email (validated according to criteria used in >input type="email"<> + * , technically not RFC 5322 compliant + */ + @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; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java new file mode 100644 index 0000000..bf5bccf --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java @@ -0,0 +1,34 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.*; + +/** DTO for password reset request */ +public class PasswordResetRequest { + + @NotNull private String confirmationToken; + + /** A properly salted way to store the password */ + @NotNull + @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; + + public String getConfirmationToken() { + return confirmationToken; + } + + public void setConfirmationToken(String confirmationToken) { + this.confirmationToken = confirmationToken; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java index f6c86a0..d324724 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java @@ -21,7 +21,7 @@ public class ConfirmationToken { @Column(name = "id", updatable = false, nullable = false) private Long id; - @Column(name = "confirmation_token") + @Column(name = "confirmation_token", unique = true) private String confirmationToken; @Temporal(TemporalType.TIMESTAMP) @@ -31,10 +31,14 @@ public class ConfirmationToken { @JoinColumn(nullable = false, name = "user_id") private User user; + @Column(nullable = false) + private Boolean resetPassword; + public ConfirmationToken(User user) { this.user = user; createdDate = new Date(); confirmationToken = UUID.randomUUID().toString(); + resetPassword = false; } /** Constructor for hibernate reflective stuff things whatever */ @@ -71,4 +75,12 @@ public class ConfirmationToken { public void setUser(User user) { this.user = user; } + + public Boolean getResetPassword() { + return resetPassword; + } + + public void setResetPassword(Boolean resetPassword) { + this.resetPassword = resetPassword; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9bfe2a7..26d5e8e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,4 +22,12 @@ spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 -spring.mail.properties.mail.smtp.writetimeout=5000 \ No newline at end of file +spring.mail.properties.mail.smtp.writetimeout=5000 + +email.registrationsubject=Complete your SmartHut.sm registration +email.registration=To confirm your registration, please click here: + +email.resetpasswordsubject=SmartHut.sm password reset +email.resetpassword=To reset your password, please click here: + +email.serverhost=http://localhost:8080/ \ No newline at end of file From a0fe5f989a0e97728392b08f785324be63190d80 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Fri, 6 Mar 2020 10:21:36 +0100 Subject: [PATCH 4/5] Revert "Added support for password reset (still needs testing)" This reverts commit 7396e6f0d949cecb48f63663855c25aee43a673c. --- .../smarthut/config/WebSecurityConfig.java | 3 +- .../controller/UserAccountController.java | 110 ++++-------------- .../dto/InitPasswordResetRequest.java | 25 ---- .../smarthut/dto/PasswordResetRequest.java | 34 ------ .../smarthut/models/ConfirmationToken.java | 14 +-- src/main/resources/application.properties | 10 +- 6 files changed, 25 insertions(+), 171 deletions(-) delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java index 253998d..e38d0df 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/WebSecurityConfig.java @@ -52,11 +52,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .authorizeRequests() .antMatchers( "/auth/login", + "/auth/register", "/swagger-ui.html", "/register", "/register/confirm-account", - "/register/init-reset-password", - "/register/reset-password", "/v2/api-docs", "/webjars/**", "/swagger-resources/**", diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java index 4959e79..e238f16 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java @@ -1,12 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.InitPasswordResetRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.OkResponse; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.PasswordResetRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateRegistrationException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.EmailTokenNotFoundException; -import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationToken; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; @@ -15,11 +12,15 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.service.EmailSenderService; import javax.validation.Valid; import javax.validation.constraints.NotNull; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.mail.SimpleMailMessage; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration @@ -34,36 +35,6 @@ public class UserAccountController { @Autowired private BCryptPasswordEncoder encoder; - @Value("email.registrationsubject") - private String emailRegistrationSubject; - - @Value("email.resetpasswordsubject") - private String resetPasswordSubject; - - @Value("email.registration") - private String emailRegistrationText; - - @Value("email.resetpassword") - private String resetPasswordText; - - @Value("email.serverhost") - private String serverHost; - - private void sendEmail(String email, ConfirmationToken token, boolean isRegistration) { - SimpleMailMessage mailMessage = new SimpleMailMessage(); - mailMessage.setTo(email); - mailMessage.setSubject(isRegistration ? emailRegistrationSubject : resetPasswordSubject); - mailMessage.setFrom("smarthut.sm@gmail.com"); - mailMessage.setText( - (isRegistration ? emailRegistrationText : resetPasswordText) - + " " - + serverHost - + "/register/confirm-account?token=" - + token.getConfirmationToken()); - - emailSenderService.sendEmail(mailMessage); - } - @PostMapping public OkResponse registerUser(@Valid @RequestBody UserRegistrationRequest registrationData) throws DuplicateRegistrationException { @@ -89,72 +60,35 @@ public class UserAccountController { toSave.setEmail(registrationData.getEmail()); userRepository.save(toSave); - ConfirmationToken token; - do { - token = new ConfirmationToken(toSave); - } while (confirmationTokenRepository.findByConfirmationToken( - token.getConfirmationToken()) - != null); + ConfirmationToken confirmationToken = new ConfirmationToken(toSave); - confirmationTokenRepository.save(token); + confirmationTokenRepository.save(confirmationToken); - sendEmail(toSave.getEmail(), token, true); + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setTo(registrationData.getEmail()); + mailMessage.setSubject("Complete Registration!"); + mailMessage.setFrom("smarthut.sm@gmail.com"); + mailMessage.setText( + "To confirm your account, please click here : " + + "http://localhost:8080/register/confirm-account?token=" + + confirmationToken.getConfirmationToken()); + + emailSenderService.sendEmail(mailMessage); return new OkResponse(); } } - @PostMapping("/init-reset-password") - public OkResponse initResetPassword(@Valid @RequestBody InitPasswordResetRequest resetRequest) - throws UserNotFoundException { - final User toReset = userRepository.findByEmailIgnoreCase(resetRequest.getEmail()); - - // Check if an User with the same email already exists - if (toReset == null) { - throw new UserNotFoundException(); - } - - ConfirmationToken token; - do { - token = new ConfirmationToken(toReset); - token.setResetPassword(true); - } while (confirmationTokenRepository.findByConfirmationToken(token.getConfirmationToken()) - != null); - - confirmationTokenRepository.save(token); - - sendEmail(toReset.getEmail(), token, false); - - return new OkResponse(); - } - - @PutMapping("/reset-password") - public OkResponse resetPassword(@Valid @RequestBody PasswordResetRequest resetRequest) - throws EmailTokenNotFoundException { - final ConfirmationToken token = - confirmationTokenRepository.findByConfirmationToken( - resetRequest.getConfirmationToken()); - - if (token == null || token.getResetPassword()) { - throw new EmailTokenNotFoundException(); - } - - final User user = token.getUser(); - user.setPassword(encoder.encode(resetRequest.getPassword())); - userRepository.save(user); - - return new OkResponse(); - } - @GetMapping(value = "/confirm-account") public OkResponse confirmUserAccount(@RequestParam("token") @NotNull String confirmationToken) throws EmailTokenNotFoundException { final ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken(confirmationToken); - if (token != null && !token.getResetPassword()) { - token.getUser().setEnabled(true); - userRepository.save(token.getUser()); + if (token != null) { + final User user = userRepository.findByEmailIgnoreCase(token.getUser().getEmail()); + user.setEnabled(true); + userRepository.save(user); // TODO: redirect to frontend return new OkResponse(); } else { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java deleted file mode 100644 index d82c4f0..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/InitPasswordResetRequest.java +++ /dev/null @@ -1,25 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.Email; -import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.Pattern; - -/** DTO for password reset request */ -public class InitPasswordResetRequest { - /** - * The user's email (validated according to criteria used in >input type="email"<> - * , technically not RFC 5322 compliant - */ - @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; - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java deleted file mode 100644 index bf5bccf..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/PasswordResetRequest.java +++ /dev/null @@ -1,34 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.*; - -/** DTO for password reset request */ -public class PasswordResetRequest { - - @NotNull private String confirmationToken; - - /** A properly salted way to store the password */ - @NotNull - @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; - - public String getConfirmationToken() { - return confirmationToken; - } - - public void setConfirmationToken(String confirmationToken) { - this.confirmationToken = confirmationToken; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java index d324724..f6c86a0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java @@ -21,7 +21,7 @@ public class ConfirmationToken { @Column(name = "id", updatable = false, nullable = false) private Long id; - @Column(name = "confirmation_token", unique = true) + @Column(name = "confirmation_token") private String confirmationToken; @Temporal(TemporalType.TIMESTAMP) @@ -31,14 +31,10 @@ public class ConfirmationToken { @JoinColumn(nullable = false, name = "user_id") private User user; - @Column(nullable = false) - private Boolean resetPassword; - public ConfirmationToken(User user) { this.user = user; createdDate = new Date(); confirmationToken = UUID.randomUUID().toString(); - resetPassword = false; } /** Constructor for hibernate reflective stuff things whatever */ @@ -75,12 +71,4 @@ public class ConfirmationToken { public void setUser(User user) { this.user = user; } - - public Boolean getResetPassword() { - return resetPassword; - } - - public void setResetPassword(Boolean resetPassword) { - this.resetPassword = resetPassword; - } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 26d5e8e..9bfe2a7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,12 +22,4 @@ spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 -spring.mail.properties.mail.smtp.writetimeout=5000 - -email.registrationsubject=Complete your SmartHut.sm registration -email.registration=To confirm your registration, please click here: - -email.resetpasswordsubject=SmartHut.sm password reset -email.resetpassword=To reset your password, please click here: - -email.serverhost=http://localhost:8080/ \ No newline at end of file +spring.mail.properties.mail.smtp.writetimeout=5000 \ No newline at end of file From 8838beb0e05584986288009fd7750333d2e5dc10 Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 8 Mar 2020 18:58:45 +0100 Subject: [PATCH 5/5] Removed setId methods from DTO classes --- .../sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java | 4 ---- .../sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java | 4 ---- .../sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java | 4 ---- .../sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java | 4 ---- .../sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java | 4 ---- .../inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java | 4 ---- .../sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java | 4 ---- .../inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java | 4 ---- 8 files changed, 32 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java index ce49970..31a22d8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java @@ -33,10 +33,6 @@ public class ButtonDimmerSaveRequest { this.lights = newLights; } - public void setId(long id) { - this.id = id; - } - public void setRoom(Room room) { this.room = room; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java index 43b5a97..8edff94 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java @@ -28,10 +28,6 @@ public class DimmableLightSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoom(Room room) { this.room = room; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java index bfc3ba8..ce053e3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java @@ -21,10 +21,6 @@ public class KnobDimmerSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoomId(Long roomId) { this.roomId = roomId; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java index 50976b6..ba73495 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java @@ -17,10 +17,6 @@ public class MotionSensorSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoomId(Long roomId) { this.roomId = roomId; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java index 34695e2..ac1324d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java @@ -18,10 +18,6 @@ public class RegularLightSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoomId(Long roomId) { this.roomId = roomId; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java index 47ca739..421523c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java @@ -43,10 +43,6 @@ public class SensorSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoomId(Long roomId) { this.roomId = roomId; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java index 7511341..3318505 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java @@ -18,10 +18,6 @@ public class SmartPlugSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoomId(Long roomId) { this.roomId = roomId; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java index 84142ec..c7516f2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java @@ -18,10 +18,6 @@ public class SwitchSaveRequest { /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ @NotNull private String name; - public void setId(long id) { - this.id = id; - } - public void setRoomId(Long roomId) { this.roomId = roomId; }