Add random password for oauth login
This commit is contained in:
parent
7cb32de495
commit
0d03c72a1e
@ -3,6 +3,7 @@ package net.Broken.DB.Entity;
|
|||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import net.Broken.Tools.UserManager.UserUtils;
|
import net.Broken.Tools.UserManager.UserUtils;
|
||||||
import net.dv8tion.jda.core.entities.User;
|
import net.dv8tion.jda.core.entities.User;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -40,10 +41,11 @@ public class UserEntity {
|
|||||||
this.apiToken = apiToken;
|
this.apiToken = apiToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity(User user){
|
public UserEntity(User user, PasswordEncoder passwordEncoder){
|
||||||
this.name = user.getName();
|
this.name = user.getName();
|
||||||
this.jdaId = user.getId();
|
this.jdaId = user.getId();
|
||||||
this.apiToken = UserUtils.getInstance().generateApiToken();
|
this.apiToken = UserUtils.getInstance().generateApiToken();
|
||||||
|
this.password = passwordEncoder.encode(UserUtils.getInstance().generateCheckToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
|
@ -128,7 +128,7 @@ public class UserManagerAPIController {
|
|||||||
@RequestMapping(value = "/oauthLogin", method = RequestMethod.POST)
|
@RequestMapping(value = "/oauthLogin", method = RequestMethod.POST)
|
||||||
public ResponseEntity<UserConnectionData> oauthLogin(@RequestParam(value = "token") String discordToken){
|
public ResponseEntity<UserConnectionData> oauthLogin(@RequestParam(value = "token") String discordToken){
|
||||||
logger.debug(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());
|
logger.info("OAuth login for " + user.getName());
|
||||||
return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK);
|
return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK);
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import net.Broken.MainBot;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
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);
|
String discorId = getUserId(token);
|
||||||
List<UserEntity> userEntitys = userRepository.findByJdaId(discorId);
|
List<UserEntity> userEntitys = userRepository.findByJdaId(discorId);
|
||||||
if(userEntitys.size() != 0){
|
if(userEntitys.size() != 0){
|
||||||
return userEntitys.get(0);
|
return userEntitys.get(0);
|
||||||
}else{
|
}else{
|
||||||
UserEntity user = new UserEntity(MainBot.jda.getUserById(discorId));
|
UserEntity user = new UserEntity(MainBot.jda.getUserById(discorId), passwordEncoder);
|
||||||
user = userRepository.save(user);
|
user = userRepository.save(user);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user