Update
Refactor, clean, add some user api rest method
This commit is contained in:
parent
18f679ff6e
commit
2c5ef6ce29
|
@ -57,6 +57,8 @@ public class User {
|
|||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {this.userId = userId;}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package restImplementation;
|
|||
|
||||
import blockchain.query.QueryWrapper;
|
||||
|
||||
public class QueryImplementation {
|
||||
public class BlockchainQueryImplementation {
|
||||
|
||||
public String getUserBalance(String userHash){
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
|
@ -1,66 +0,0 @@
|
|||
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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package restImplementation;
|
||||
|
||||
import database.user.User;
|
||||
import database.user.UserDao;
|
||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class DatabaseUserImplementation {
|
||||
|
||||
public boolean saveUser(User user) throws Exception {
|
||||
UserDao userDao = new UserDao();
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public User getUser(String email, String password) throws Exception{
|
||||
UserDao userDao = new UserDao();
|
||||
User user1 = userDao.getUser(email);
|
||||
if(user1 != null){
|
||||
String hash = user1.getPassword();
|
||||
if(BCrypt.checkpw(password, hash)){
|
||||
return user1;
|
||||
}else{
|
||||
System.out.println("Bcrypt nope");
|
||||
return null;
|
||||
}
|
||||
}else {
|
||||
System.out.println("nope");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String userLogger(User user) throws Exception {
|
||||
UserDao userDao = new UserDao();
|
||||
String response;
|
||||
|
||||
User user1 = userDao.getUser(user.getEmail());
|
||||
if(user1 != null){
|
||||
String hash = user1.getPassword();
|
||||
|
||||
if(!BCrypt.checkpw(user.getPassword(), hash)){
|
||||
response = "Not Allowed";
|
||||
}else{
|
||||
response = "Ok";
|
||||
}
|
||||
} else {
|
||||
response = "Not Exist";
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
private String hashPassword(String plainTextPassword){
|
||||
return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt());
|
||||
}
|
||||
}
|
|
@ -4,12 +4,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import restImplementation.QueryImplementation;
|
||||
import restImplementation.BlockchainQueryImplementation;
|
||||
|
||||
@RestController
|
||||
public class QueryController {
|
||||
@RequestMapping(value = "/api/rest/query")
|
||||
public class BlockchainQueryResource {
|
||||
|
||||
@RequestMapping(value = "/user",params = {"name"})
|
||||
@RequestMapping(value = "/hello",params = {"name"})
|
||||
public @ResponseBody
|
||||
String getUser(
|
||||
@RequestParam(value = "name") String hash){
|
||||
|
@ -20,8 +21,8 @@ public class QueryController {
|
|||
public @ResponseBody
|
||||
String getUserBalance(
|
||||
@RequestParam(value = "name") String hash){
|
||||
QueryImplementation queryImplementation = new QueryImplementation();
|
||||
return queryImplementation.getUserBalance(hash);
|
||||
BlockchainQueryImplementation blockchainQueryImplementation = new BlockchainQueryImplementation();
|
||||
return blockchainQueryImplementation.getUserBalance(hash);
|
||||
}
|
||||
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package restService;
|
||||
|
||||
public class DatabaseTransactionResource {
|
||||
}
|
74
src/main/java/restService/DatabaseUserResource.java
Normal file
74
src/main/java/restService/DatabaseUserResource.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
package restService;
|
||||
|
||||
import database.user.User;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import restImplementation.DatabaseUserImplementation;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/rest/user")
|
||||
public class DatabaseUserResource {
|
||||
|
||||
@PostMapping("/save")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public ResponseEntity<String> saveUser(@Valid @RequestBody User user){
|
||||
try{
|
||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||
boolean result = databaseUserImplementation.saveUser(user);
|
||||
if(result){
|
||||
return ResponseEntity.status(HttpStatus.OK).body("Ok");
|
||||
}else {
|
||||
return ResponseEntity.status(HttpStatus.FOUND).body("User already exist");
|
||||
}
|
||||
}catch (Exception e){
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<String> login(@Valid @RequestBody User user){
|
||||
try{
|
||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||
String response = databaseUserImplementation.userLogger(user);
|
||||
switch (response){
|
||||
case "Not Exist" : return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
|
||||
case "Not Allowed" : return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Wrong Password!");
|
||||
case "" : return ResponseEntity.status(HttpStatus.CONFLICT).body("Error");
|
||||
case "Ok": return ResponseEntity.status(HttpStatus.OK).body("Ok");
|
||||
default: return ResponseEntity.status(HttpStatus.CONFLICT).body("Error");
|
||||
}
|
||||
}catch (Exception e){
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/get")
|
||||
@ResponseBody
|
||||
public ResponseEntity<User> getUser(@RequestBody User user){
|
||||
try{
|
||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||
User response = databaseUserImplementation.getUser(user.getEmail(), user.getPassword());
|
||||
if(response != null){
|
||||
User userResponse = new User();
|
||||
userResponse.setUser_hash(response.getUser_hash());
|
||||
userResponse.setUserId(response.getUserId());
|
||||
return ResponseEntity.status(HttpStatus.OK).body(userResponse);
|
||||
}else{
|
||||
return new ResponseEntity("Error", HttpStatus.CONFLICT);
|
||||
}
|
||||
}catch (Exception e){
|
||||
return new ResponseEntity(e.getMessage(), HttpStatus.CONFLICT);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity updateUser(@RequestBody User user){
|
||||
return new ResponseEntity(null, HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class WelcomeController {
|
||||
public class WelcomeResource {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index(){return "Welcome from Monnethic !";}
|
23
src/test/java/database/DatabaseUserImplementationTest.java
Normal file
23
src/test/java/database/DatabaseUserImplementationTest.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
package database;
|
||||
|
||||
import database.user.User;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import restImplementation.DatabaseUserImplementation;
|
||||
|
||||
@Ignore
|
||||
public class DatabaseUserImplementationTest {
|
||||
@Test
|
||||
public void TestUser(){
|
||||
try {
|
||||
//String email = "null";
|
||||
String email = "thomas.marshal@gmail.com";
|
||||
String password = "null";
|
||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||
User response = databaseUserImplementation.getUser(email, password);
|
||||
System.out.println(response);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,13 +8,18 @@ import org.junit.Test;
|
|||
public class UserLoggerTest {
|
||||
@Test
|
||||
public void testLoggerUser(){
|
||||
try{
|
||||
User user = new User();
|
||||
user.setPassword("newPassword");
|
||||
user.setEmail("thomas.marshal@gmail.com");
|
||||
|
||||
DatabaseImplementation databaseImplementation = new DatabaseImplementation();
|
||||
String res = databaseImplementation.userLogger(user);
|
||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||
String res = databaseUserImplementation.userLogger(user);
|
||||
System.out.println(res);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue