Finish connection api

This commit is contained in:
BrokenFire 2018-02-13 16:29:36 +01:00
parent 650da78fe6
commit 4b6b6680fe
3 changed files with 49 additions and 5 deletions

View File

@ -4,10 +4,22 @@ public class UserConnectionData {
public boolean accepted;
public String token;
public String message;
public String error;
public String name;
public UserConnectionData(boolean accepted, String token, String message) {
public UserConnectionData(boolean accepted, String name, String token, String message) {
this.accepted = accepted;
this.token = token;
this.message = message;
this.name = name;
this.error = null;
}
public UserConnectionData(boolean accepted, String message, String error) {
this.accepted = accepted;
this.token = null;
this.message = message;
this.error = error;
this.name = null;
}
}

View File

@ -23,7 +23,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.applet.Main;
@RestController
@RequestMapping("/api/userManagement")
@ -69,13 +68,26 @@ public class UserManagerAPIController {
userRepository.save(user);
pendingUserRepository.delete(pUser);
return new ResponseEntity<>(new UserConnectionData(true,"",""),HttpStatus.OK);
return new ResponseEntity<>(new UserConnectionData(true,user.getApiToken(),""),HttpStatus.OK);
} catch (TokenNotMatch tokenNotMatch) {
logger.warn("Pre token not match for "+data.id+"!");
return new ResponseEntity<>(new UserConnectionData(false,null,"Token not match!"),HttpStatus.NOT_ACCEPTABLE);
return new ResponseEntity<>(new UserConnectionData(false,"Token not match!","token"),HttpStatus.NOT_ACCEPTABLE);
} catch (UserNotFoundException e) {
logger.warn("Id not found in DB ("+data.id+")");
return new ResponseEntity<>(new UserConnectionData(false,null,"User not found on DB!"),HttpStatus.NOT_ACCEPTABLE);
return new ResponseEntity<>(new UserConnectionData(false,"User not found on DB!", "user"),HttpStatus.NOT_ACCEPTABLE);
}
}
@RequestMapping(value = "/requestToken", method = RequestMethod.POST)
public ResponseEntity<UserConnectionData> requestToken(@RequestBody UserInfoData data){
try {
UserEntity user = MainBot.userRegister.getUser(userRepository, passwordEncoder, data);
return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK);
} catch (UserNotFoundException e) {
return new ResponseEntity<>(new UserConnectionData(false,"User not registered!", "user"),HttpStatus.NOT_ACCEPTABLE);
} catch (PasswordNotMatchException e) {
return new ResponseEntity<>(new UserConnectionData(false,"Wrong user name or password!", "password"),HttpStatus.NOT_ACCEPTABLE);
}
}

View File

@ -1,6 +1,7 @@
package net.Broken.Tools.UserManager;
import net.Broken.DB.Entity.PendingUserEntity;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.PendingUserRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot;
@ -105,6 +106,25 @@ public class UserRegister {
return pendingUser;
}
public UserEntity getUser(UserRepository userRepository, PasswordEncoder passwordEncoder, UserInfoData userInfoData) throws UserNotFoundException, PasswordNotMatchException {
List<UserEntity> users = userRepository.findByName(userInfoData.name);
if(users.size()<1){
logger.warn("Login with unknown username: " + userInfoData.name);
throw new UserNotFoundException();
}
else{
UserEntity user = users.get(0);
if(passwordEncoder.matches(userInfoData.password,user.getPassword())){
logger.info("Login successful for " + user.getName());
return user;
}
else
{
logger.warn("Login fail for " + user.getName() + ", wrong password!");
throw new PasswordNotMatchException();
}
}
}
public String generateApiToken(){
return UUID.randomUUID().toString();