Finish connection api
This commit is contained in:
parent
650da78fe6
commit
4b6b6680fe
@ -4,10 +4,22 @@ public class UserConnectionData {
|
|||||||
public boolean accepted;
|
public boolean accepted;
|
||||||
public String token;
|
public String token;
|
||||||
public String message;
|
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.accepted = accepted;
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.message = message;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import sun.applet.Main;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/userManagement")
|
@RequestMapping("/api/userManagement")
|
||||||
@ -69,13 +68,26 @@ public class UserManagerAPIController {
|
|||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
pendingUserRepository.delete(pUser);
|
pendingUserRepository.delete(pUser);
|
||||||
|
|
||||||
return new ResponseEntity<>(new UserConnectionData(true,"",""),HttpStatus.OK);
|
return new ResponseEntity<>(new UserConnectionData(true,user.getApiToken(),""),HttpStatus.OK);
|
||||||
} catch (TokenNotMatch tokenNotMatch) {
|
} catch (TokenNotMatch tokenNotMatch) {
|
||||||
logger.warn("Pre token not match for "+data.id+"!");
|
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) {
|
} catch (UserNotFoundException e) {
|
||||||
logger.warn("Id not found in DB ("+data.id+")");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.Broken.Tools.UserManager;
|
package net.Broken.Tools.UserManager;
|
||||||
|
|
||||||
import net.Broken.DB.Entity.PendingUserEntity;
|
import net.Broken.DB.Entity.PendingUserEntity;
|
||||||
|
import net.Broken.DB.Entity.UserEntity;
|
||||||
import net.Broken.DB.Repository.PendingUserRepository;
|
import net.Broken.DB.Repository.PendingUserRepository;
|
||||||
import net.Broken.DB.Repository.UserRepository;
|
import net.Broken.DB.Repository.UserRepository;
|
||||||
import net.Broken.MainBot;
|
import net.Broken.MainBot;
|
||||||
@ -105,6 +106,25 @@ public class UserRegister {
|
|||||||
return pendingUser;
|
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(){
|
public String generateApiToken(){
|
||||||
return UUID.randomUUID().toString();
|
return UUID.randomUUID().toString();
|
||||||
|
Loading…
Reference in New Issue
Block a user