Setup basic login

Setup login, register, basic hash
This commit is contained in:
GME 2018-11-20 19:45:06 +01:00
parent f8308dff09
commit 18f679ff6e
10 changed files with 206 additions and 33 deletions

View file

@ -34,6 +34,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
<!-- ***** --> <!-- ***** -->
<!-- LOGGER AND TEST DEPENDENCIES --> <!-- LOGGER AND TEST DEPENDENCIES -->

View file

@ -5,9 +5,10 @@ import com.j256.ormlite.support.ConnectionSource;
public class DatabaseHelper { public class DatabaseHelper {
private static final String DATABASE_NAME = "monnethic"; private static final String DATABASE_NAME = "monnethic";
private static final String DATABASE_USER = ""; private static final String DATABASE_USER = "postgres";
private static final String DATABASE_PWD = ""; private static final String DATABASE_PWD = "L-*q~Ytaha{;u+7yJ8";
private final static String DATABASE_URL = "jdbc:postgresql://host:port/"+DATABASE_NAME; //private final static String DATABASE_URL = "jdbc:postgresql://host:port/"+DATABASE_NAME;
private final static String DATABASE_URL = "jdbc:postgresql://37.187.101.44:5432/"+DATABASE_NAME;
public ConnectionSource setupDatabaseConnection(){ public ConnectionSource setupDatabaseConnection(){
try{ try{

View file

@ -4,38 +4,36 @@ import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; import com.j256.ormlite.table.DatabaseTable;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Date;
@DatabaseTable(tableName = "T_TRANSACTION") @DatabaseTable(tableName = "T_TRANSACTION")
public class Transaction { public class Transaction {
@DatabaseField(columnName = "transactionId", generatedId = true, unique = true) @DatabaseField
private int id; private int transactionId;
@DatabaseField(columnName = "transactionDate") @DatabaseField(canBeNull = false)
private Date transactionDate; private int userId;
@DatabaseField(columnName = "transactionDSrcAddress") @DatabaseField(canBeNull = false)
private long transactionDate;
@DatabaseField(columnName = "transactionFrom", canBeNull = false)
private String sourceAddress; private String sourceAddress;
@DatabaseField(columnName = "transactionDestAddress") @DatabaseField(columnName = "transactionTo", canBeNull = false)
private String destAddress; private String destAddress;
@DatabaseField(columnName = "transactionHash") @DatabaseField(canBeNull = false)
private String transactionHash; private String transactionHash;
@DatabaseField(columnName = "transactionBlockNumber") @DatabaseField(canBeNull = false)
private String blockNumber;
@DatabaseField(columnName = "transactionAmout")
private BigInteger amount; private BigInteger amount;
@DatabaseField(columnName = "transactionUnit") @DatabaseField(canBeNull = false)
private String unit; private String unit;
public Transaction() { public Transaction() {
super(); super();
} }
public Transaction(Date transactionDate, String sourceAddress, String destAddress, String transactionHash, String blockNumber, BigInteger amount, String unit) { public Transaction(long transactionDate, String sourceAddress, String destAddress, String transactionHash, BigInteger amount, String unit) {
super(); super();
this.transactionDate = transactionDate; this.transactionDate = transactionDate;
this.sourceAddress = sourceAddress; this.sourceAddress = sourceAddress;
this.destAddress = destAddress; this.destAddress = destAddress;
this.transactionHash = transactionHash; this.transactionHash = transactionHash;
this.blockNumber = blockNumber;
this.amount = amount; this.amount = amount;
this.unit = unit; this.unit = unit;
} }
@ -72,19 +70,27 @@ public class Transaction {
this.amount = amount; this.amount = amount;
} }
public int getId() { public int getTransactionId() {
return id; return transactionId;
} }
public void setId(int id) { public void setTransactionId(int id) {
this.id = id; this.transactionId = transactionId;
} }
public Date getTransactionDate() { public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public long getTransactionDate() {
return transactionDate; return transactionDate;
} }
public void setTransactionDate(Date transactionDate) { public void setTransactionDate(long transactionDate) {
this.transactionDate = transactionDate; this.transactionDate = transactionDate;
} }
@ -96,11 +102,4 @@ public class Transaction {
this.unit = unit; this.unit = unit;
} }
public String getBlockNumber() {
return blockNumber;
}
public void setBlockNumber(String blockNumber) {
this.blockNumber = blockNumber;
}
} }

View file

@ -6,11 +6,13 @@ import com.j256.ormlite.table.DatabaseTable;
//Class User which represent an User in the postgresDB //Class User which represent an User in the postgresDB
@DatabaseTable(tableName = "T_USER") @DatabaseTable(tableName = "T_USER")
public class User { public class User {
@DatabaseField
private int userId;
@DatabaseField(canBeNull = false) @DatabaseField(canBeNull = false)
private String name; private String name;
@DatabaseField(canBeNull = false) @DatabaseField(canBeNull = false)
private String firstname; private String firstname;
@DatabaseField(id = true) @DatabaseField(canBeNull = false)
private String email; private String email;
@DatabaseField(canBeNull = false) @DatabaseField(canBeNull = false)
private String password; private String password;
@ -22,6 +24,8 @@ public class User {
private boolean verified; private boolean verified;
@DatabaseField(canBeNull = false) @DatabaseField(canBeNull = false)
private boolean approved; private boolean approved;
@DatabaseField(canBeNull = false)
private String user_hash;
//Constructors //Constructors
//Default constructor for ORMLite //Default constructor for ORMLite
@ -35,9 +39,10 @@ public class User {
this.password = password; this.password = password;
} }
public User(String name, String firstname, String email, String password, long creation_date, long modification_date, boolean verified, boolean approved) { public User(String name, String firstname,String user_hash, String email, String password, long creation_date, long modification_date, boolean verified, boolean approved) {
this.name = name; this.name = name;
this.firstname = firstname; this.firstname = firstname;
this.user_hash = user_hash;
this.email = email; this.email = email;
this.password = password; this.password = password;
this.creation_date = creation_date; this.creation_date = creation_date;
@ -48,10 +53,18 @@ public class User {
//Getters and Setters //Getters and Setters
public int getUserId() {
return userId;
}
public String getName() { public String getName() {
return name; return name;
} }
public void setUser_hash(String user_hash){this.user_hash = user_hash;}
public String getUser_hash(){return user_hash;}
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }

View file

@ -14,7 +14,7 @@ public class UserDao {
private DatabaseHelper dbh = new DatabaseHelper(); private DatabaseHelper dbh = new DatabaseHelper();
private Dao<User, String> userDao; private Dao<User, String> userDao;
public Dao createUserDaoConnection(){ private Dao createUserDaoConnection(){
try { try {
return DaoManager.createDao(dbh.setupDatabaseConnection(),User.class); return DaoManager.createDao(dbh.setupDatabaseConnection(),User.class);
}catch (Exception e){ }catch (Exception e){

View file

@ -0,0 +1,66 @@
package restImplementation;
import database.user.User;
import database.user.UserDao;
import org.springframework.security.crypto.bcrypt.BCrypt;
import java.time.Instant;
public class DatabaseImplementation {
public boolean saveUser(User user){
UserDao userDao = new UserDao();
try{
User dbUser = userDao.getUser(user.getEmail());
if(dbUser != null){
return false;
}else {
user.setPassword(hashPassword(user.getPassword()));
long now = Instant.now().toEpochMilli();
user.setCreation_date(now);
user.setModification_date(now);
user.setVerified(true);
user.setApproved(true);
//TEMPORARY USER HASH IS
// name+email+password
user.setUser_hash(hashPassword(user.getName()+user.getEmail()+user.getPassword()));
userDao.addUser(user);
}
} catch (Exception e){
e.printStackTrace();
}
return true;
}
public String userLogger(User user){
UserDao userDao = new UserDao();
String response = "";
try{
User user1 = userDao.getUser(user.getEmail());
if(user1 != null){
String hash = user1.getPassword();
if(!BCrypt.checkpw(user.getPassword(), hash)){
response = "NotAllowed";
}else{
response = "Ok";
}
} else {
response = "NotExist";
}
} catch (Exception e){
System.out.println(e);
}
return response;
}
private String hashPassword(String plainTextPassword){
return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt());
}
}

View file

@ -0,0 +1,44 @@
package restService;
import database.user.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import restImplementation.DatabaseImplementation;
import javax.validation.Valid;
@RestController
public class DatabaseController {
@PostMapping("/save")
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<String> saveUser(@Valid @RequestBody User user){
DatabaseImplementation databaseImplementation = new DatabaseImplementation();
boolean result = databaseImplementation.saveUser(user);
if(result){
return ResponseEntity.status(HttpStatus.OK).body("");
}else {
return ResponseEntity.status(HttpStatus.FOUND).body("User exist");
}
}
@PostMapping("/login")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> login(@Valid @RequestBody User user){
DatabaseImplementation databaseImplementation = new DatabaseImplementation();
String response = databaseImplementation.userLogger(user);
if(response.equals("NotExist")){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
}else if(response.equals("NotAllowed")){
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Wrong Password!");
} else if(response.equals("")){
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Error");
} else {
return ResponseEntity.ok("Welcome");
}
}
}

View file

@ -0,0 +1,23 @@
package crypto;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.security.crypto.bcrypt.BCrypt;
import java.time.Instant;
@Ignore
public class CryptoTest {
@Test
public void testHash(){
String password = "newPassword";
System.out.println(hashPassword(password));
long now = Instant.now().toEpochMilli();
System.out.println(now);
}
private String hashPassword(String plainTextPassword) {
return BCrypt.hashpw(plainTextPassword, BCrypt.gensalt());
}
}

View file

@ -2,9 +2,11 @@ package database;
import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.dao.DaoManager;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
//Testing class for postgresql connection //Testing class for postgresql connection
@Ignore
public class DatabaseHelperTest { public class DatabaseHelperTest {
private DatabaseHelper dbh = new DatabaseHelper(); private DatabaseHelper dbh = new DatabaseHelper();

View file

@ -0,0 +1,21 @@
package restImplementation;
import database.user.User;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class UserLoggerTest {
@Test
public void testLoggerUser(){
User user = new User();
user.setPassword("newPassword");
user.setEmail("thomas.marshal@gmail.com");
DatabaseImplementation databaseImplementation = new DatabaseImplementation();
String res = databaseImplementation.userLogger(user);
System.out.println(res);
}
}