Merge branch 'hashPassword' into 'master'

Add password hashing & deploy heroku

See merge request cse1105/2018-2019/oopp-group-43/template!89
This commit is contained in:
Ceren Ugurlu
2019-04-14 16:37:56 +00:00
10 changed files with 85 additions and 35 deletions

View File

@@ -39,8 +39,8 @@ test {
}
bootJar {
baseName = 'gs-consuming-rest'
version = '0.1.0'
baseName = 'greenify'
version = '1.0.0'
}
repositories {
@@ -48,6 +48,7 @@ repositories {
}
dependencies {
compile "org.bouncycastle:bcprov-jdk16:1.46", "org.bouncycastle:bcpg-jdk16:1.46", "org.bouncycastle:bcmail-jdk16:1.46", "org.bouncycastle:bctsp-jdk16:1.46"
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")

View File

@@ -36,6 +36,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.client.HttpClientErrorException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
@@ -197,7 +198,7 @@ public class RegisterWindowController {
* @param event the click of the sign up button
*/
@FXML
public void handleSignUpButton(ActionEvent event) throws IOException {
public void handleSignUpButton(ActionEvent event) throws IOException, NoSuchAlgorithmException {
//set the window to the current window (for displaying the alerts)
Window owner = signUpButton.getScene().getWindow();
//check if the username field is empty

View File

@@ -20,6 +20,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.client.HttpClientErrorException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
/**
@@ -52,7 +53,8 @@ public class UserController {
* @throws IOException an exception for logging in the user
*/
@FXML
protected void handleLoginButtonAction(ActionEvent event) throws IOException {
protected void handleLoginButtonAction(ActionEvent event)
throws IOException, NoSuchAlgorithmException {
Window owner = loginButton.getScene().getWindow(); //get the current window
if (usernameField.getText().isEmpty()) {
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Log-in Error!",

View File

@@ -2,6 +2,7 @@ package greenify.client.rest;
import greenify.common.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
@@ -12,6 +13,9 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
@@ -19,6 +23,9 @@ import java.util.Map;
public class UserService {
public UserDto currentUser;
@Value( "${server.address:http://localhost:8080}" )
String serverAddress = "http://localhost:8080";
@Autowired
RestTemplate restTemplate;
@@ -36,16 +43,17 @@ public class UserService {
@SuppressWarnings("Duplicates")
//this suppressWarnings is to get rid of the errors of duplicate code
//because the methods are very similar
public UserDto registerUser(String name, String password) {
public UserDto registerUser(String name, String password) throws NoSuchAlgorithmException {
//headers for http
HttpHeaders headers = new HttpHeaders();
//set the accept header in JSON value
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
//connect to the server with the needed url
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/registerUser")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/registerUser")
.queryParam("name", name)
//getting the name from the database
.queryParam("password", password);
.queryParam("password", hashPassword(password));
//getting the password from the database
//create a http entity to be sent
@@ -67,13 +75,14 @@ public class UserService {
* @return a userDTO
*/
@SuppressWarnings("Duplicates")
public UserDto loginUser(String name, String password) {
public UserDto loginUser(String name, String password) throws NoSuchAlgorithmException {
//this method is almost the same as the registerUser one, but with a different link
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/loginUser")
.queryParam("name", name)
.queryParam("password", password);
.queryParam("password", hashPassword(password));
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
UserDto result = this.restTemplate.getForObject(builder.build()
@@ -92,7 +101,7 @@ public class UserService {
public void updateInput(String name, String inputName, String value) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/setInput")
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(serverAddress + "/setInput")
.queryParam("name", name)
.queryParam("inputName", inputName)
.queryParam("value",value);
@@ -112,7 +121,8 @@ public class UserService {
public void updateExtraInput(String name, String inputName, String value) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/setExtraInput")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/setExtraInput")
.queryParam("name", name)
.queryParam("inputName", inputName)
.queryParam("value", value);
@@ -131,7 +141,8 @@ public class UserService {
public double getFootprint(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFootprint")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getFootprint")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -149,7 +160,7 @@ public class UserService {
public double getFirstFootprint(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFirst")
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(serverAddress + "/getFirst")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -168,7 +179,8 @@ public class UserService {
public Float saveFootprint(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/saveFootprint")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/saveFootprint")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -187,7 +199,8 @@ public class UserService {
public Float saveFirstFootprint(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/saveFirstFootprint")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/saveFirstFootprint")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -205,7 +218,8 @@ public class UserService {
public List<String> getFriendNames(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFriends")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getFriends")
.queryParam("name", name);
new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -223,7 +237,8 @@ public class UserService {
public void addFriend(String name, String friend) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addFriend")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/addFriend")
.queryParam("name", name)
.queryParam("friend",friend);
HttpEntity<?> entity = new HttpEntity<>(headers);
@@ -241,7 +256,8 @@ public class UserService {
public void removeFriend(String name, String friend) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/removeFriend")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/removeFriend")
.queryParam("name", name)
.queryParam("friend",friend);
HttpEntity<?> entity = new HttpEntity<>(headers);
@@ -258,7 +274,8 @@ public class UserService {
public Map<String, String> getInputs(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getInputs")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getInputs")
.queryParam("name", name);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -275,7 +292,8 @@ public class UserService {
public Map<String, String> getExtraInputs(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getExtraInputs")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getExtraInputs")
.queryParam("name", name);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -293,7 +311,8 @@ public class UserService {
public Map getAchievements(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getAchievements")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getAchievements")
.queryParam("name", name);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -310,7 +329,8 @@ public class UserService {
public Map<String, String> getResults(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getResults")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getResults")
.queryParam("name", name);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
@@ -325,7 +345,8 @@ public class UserService {
public List<String> getAllUsers() {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getAllUsers");
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/getAllUsers");
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
List<String> result = this.restTemplate.getForObject(builder
@@ -341,11 +362,27 @@ public class UserService {
public void deleteAccount(String name) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/deleteAccount")
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(serverAddress + "/deleteAccount")
.queryParam("name", name);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
ResponseEntity<String> authenticateResponse = this.restTemplate.getForEntity(builder.build()
.encode().toUri(), String.class);
}
public String hashPassword(String password)
throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedHash = digest.digest(password.getBytes(StandardCharsets.UTF_8));
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < encodedHash.length; i++) {
String hex = Integer.toHexString(0xff & encodedHash[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}

View File

@@ -614,12 +614,12 @@
</ImageView>
</graphic>
</Button>
<Button fx:id="removeFriend" contentDisplay="RIGHT" layoutX="575.0" layoutY="150.0" mnemonicParsing="false" onAction="#openRemoveFriend" prefHeight="74.0" prefWidth="200.0" style="-fx-border-radius: 20px; -fx-padding: 0px 0px 0px 0px; -fx-background-color: transparent;" text="Remove friend" textFill="#29721a">
<Button fx:id="removeFriend" contentDisplay="RIGHT" layoutX="560.0" layoutY="150.0" mnemonicParsing="false" onAction="#openRemoveFriend" prefHeight="74.0" prefWidth="200.0" style="-fx-border-radius: 20px; -fx-padding: 0px 0px 0px 0px; -fx-background-color: transparent;" text="Remove friend" textFill="#29721a">
<font>
<Font name="Corbel Bold" size="14.0" />
</font>
<graphic>
<ImageView fitHeight="74.0" fitWidth="64.0" pickOnBounds="true" preserveRatio="true">
<ImageView fitHeight="70.0" fitWidth="60.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icons/remove_friend.png" />
</image>

View File

@@ -30,7 +30,7 @@ public class UserServiceTest {
@Test
public void userRegisterTest() throws Exception {
UserDto testUser = new UserDto(1L, "Eric");
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"),
UserDto.class))
.thenReturn(testUser);
@@ -41,7 +41,7 @@ public class UserServiceTest {
@Test
public void userLoginTest() throws Exception {
UserDto testUser = new UserDto(1L, "Eric");
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"),
UserDto.class))
.thenReturn(testUser);
UserDto user = userService.loginUser("Eric", "password");

View File

@@ -7,6 +7,19 @@ buildscript {
}
}
plugins {
id "com.heroku.sdk.heroku-gradle" version "1.0.4"
}
heroku {
appName = "greenify43"
includes = ["${buildDir}/libs/greenify-server-1.0.0.jar"]
includeBuildDir = false
processTypes(
web: "java -Dserver.port=\$PORT -jar src/Server/build/libs/greenify-server-1.0.0.jar"
)
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
@@ -16,8 +29,8 @@ apply plugin: 'jacoco'
apply plugin: 'checkstyle'
bootJar {
baseName = 'gs-rest-service'
version = '0.1.0'
baseName = 'greenify-server'
version = '1.0.0'
}
test {

View File

@@ -209,7 +209,6 @@ public class User {
throw new ApplicationException("Cannot add yourself as a friend");
} else {
friends.add(user);
System.out.print("Friend added!");
}
}
@@ -222,7 +221,6 @@ public class User {
throw new ApplicationException("This user is not your friend!");
} else {
friends.remove(user);
System.out.print("Friend removed");
}
}

View File

@@ -114,7 +114,6 @@ public class UserController {
*/
@RequestMapping("/getFirst")
public Float getFirstFootprint(@RequestParam(value = "name") String name) {
System.out.println("hello");
Float footprint = userService.getFirstFootprint(name);
return footprint;
}

View File

@@ -63,7 +63,6 @@ public class CalculatorService {
.indexOf("<result_grand_total>")
+ 20, response.getBody().indexOf("</result_grand_total>"));
// to do: in not HTTP 200 or exception case throws exception
System.out.println(Float.parseFloat(result));
return Float.parseFloat(result);
}