Add random password for oauth login

This commit is contained in:
Sebastien 2018-11-22 16:05:23 +02:00
parent 7cb32de495
commit 0d03c72a1e
3 changed files with 7 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package net.Broken.DB.Entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import net.Broken.Tools.UserManager.UserUtils;
import net.dv8tion.jda.core.entities.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.persistence.*;
import java.util.ArrayList;
@ -40,10 +41,11 @@ public class UserEntity {
this.apiToken = apiToken;
}
public UserEntity(User user){
public UserEntity(User user, PasswordEncoder passwordEncoder){
this.name = user.getName();
this.jdaId = user.getId();
this.apiToken = UserUtils.getInstance().generateApiToken();
this.password = passwordEncoder.encode(UserUtils.getInstance().generateCheckToken());
}
public String getPassword() {

View File

@ -128,7 +128,7 @@ public class UserManagerAPIController {
@RequestMapping(value = "/oauthLogin", method = RequestMethod.POST)
public ResponseEntity<UserConnectionData> oauthLogin(@RequestParam(value = "token") String discordToken){
logger.debug(discordToken);
UserEntity user = Oauth.getInstance().getUserEntity(discordToken, userRepository);
UserEntity user = Oauth.getInstance().getUserEntity(discordToken, userRepository, passwordEncoder);
logger.info("OAuth login for " + user.getName());
return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK);

View File

@ -6,6 +6,7 @@ import net.Broken.MainBot;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.io.BufferedReader;
import java.io.IOException;
@ -53,13 +54,13 @@ public class Oauth {
}
public UserEntity getUserEntity(String token, UserRepository userRepository){
public UserEntity getUserEntity(String token, UserRepository userRepository, PasswordEncoder passwordEncoder){
String discorId = getUserId(token);
List<UserEntity> userEntitys = userRepository.findByJdaId(discorId);
if(userEntitys.size() != 0){
return userEntitys.get(0);
}else{
UserEntity user = new UserEntity(MainBot.jda.getUserById(discorId));
UserEntity user = new UserEntity(MainBot.jda.getUserById(discorId), passwordEncoder);
user = userRepository.save(user);
return user;
}