Add OAuth login, ready for test
This commit is contained in:
parent
dd07abb135
commit
43c316391f
@ -1,6 +1,8 @@
|
||||
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 javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
@ -38,6 +40,12 @@ public class UserEntity {
|
||||
this.apiToken = apiToken;
|
||||
}
|
||||
|
||||
public UserEntity(User user){
|
||||
this.name = user.getName();
|
||||
this.jdaId = user.getId();
|
||||
this.apiToken = UserUtils.getInstance().generateApiToken();
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
@ -54,6 +54,7 @@ public class Init {
|
||||
MainBot.jda = jda;
|
||||
jda.setAutoReconnect(true);
|
||||
|
||||
|
||||
/*************************************
|
||||
* Definition des commande *
|
||||
*************************************/
|
||||
|
@ -7,6 +7,7 @@ import net.Broken.DB.Repository.UserRepository;
|
||||
import net.Broken.MainBot;
|
||||
import net.Broken.RestApi.Data.UserManager.*;
|
||||
import net.Broken.Tools.UserManager.Exceptions.*;
|
||||
import net.Broken.Tools.UserManager.Oauth;
|
||||
import net.Broken.Tools.UserManager.UserUtils;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
@ -122,4 +123,18 @@ 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);
|
||||
logger.debug(user.getName());
|
||||
return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,67 @@
|
||||
package net.Broken.Tools.UserManager;
|
||||
|
||||
import net.Broken.DB.Entity.UserEntity;
|
||||
import net.Broken.DB.Repository.UserRepository;
|
||||
import net.Broken.MainBot;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
public class Oauth {
|
||||
private static Oauth INSTANCE = new Oauth();
|
||||
public static Oauth getInstance(){ return INSTANCE; }
|
||||
|
||||
Logger logger = LogManager.getLogger();
|
||||
private String baseUrl = "https://discordapp.com/api";
|
||||
private String mePath = "/users/@me";
|
||||
|
||||
|
||||
|
||||
private String getUserId(String token){
|
||||
StringBuffer content = new StringBuffer();
|
||||
try {
|
||||
String httpsURL = baseUrl+mePath;
|
||||
URL myUrl = new URL(httpsURL);
|
||||
HttpURLConnection con = (HttpURLConnection)myUrl.openConnection();
|
||||
con.setRequestProperty("Authorization", "Bearer "+token);
|
||||
con.setRequestProperty("User-Agent", "DiscordBot (bot.seb6596.ovh, 0.1)");
|
||||
con.setRequestMethod("GET");
|
||||
logger.debug("Response code: " + con.getResponseCode());
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
JSONObject json = new JSONObject(content.toString());
|
||||
|
||||
|
||||
return json.getString("id");
|
||||
}
|
||||
|
||||
|
||||
public UserEntity getUserEntity(String token, UserRepository userRepository){
|
||||
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));
|
||||
user = userRepository.save(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ public class GeneralWebView {
|
||||
model.addAttribute("guild_name", guild.getName());
|
||||
else
|
||||
model.addAttribute("guild_name", "");
|
||||
model.addAttribute("redirect_url", System.getenv("OAUTH_URL"));
|
||||
|
||||
|
||||
return CheckPage.getPageIfReady("index");
|
||||
}
|
||||
@ -44,6 +46,11 @@ public class GeneralWebView {
|
||||
return CheckPage.getPageIfReady("forgetPass");
|
||||
}
|
||||
|
||||
@RequestMapping("/oauthCallback")
|
||||
public String oauthCallback(Model model){
|
||||
return "oauthCallback";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -19,6 +19,8 @@ public class MusicWebView {
|
||||
model.addAttribute("guild_name", guild.getName());
|
||||
else
|
||||
model.addAttribute("guild_name", "");
|
||||
model.addAttribute("redirect_url", System.getenv("OAUTH_URL"));
|
||||
|
||||
|
||||
return CheckPage.getPageIfReady("music");
|
||||
}
|
||||
|
33
src/main/resources/static/js/oauthCallback.js
Normal file
33
src/main/resources/static/js/oauthCallback.js
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
var hash = window.location.hash.replace("#","").split("&");
|
||||
var discordToken = "";
|
||||
|
||||
|
||||
hash.forEach(function (value) {
|
||||
if(value.indexOf("access_token") !== -1){
|
||||
discordToken = value.split("=")[1];
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
if(discordToken !== ""){
|
||||
console.log(discordToken);
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
url: "/api/userManagement/oauthLogin?token=" + discordToken,
|
||||
success: function (data) {
|
||||
console.log(data);
|
||||
Cookies.set('token',data.token, { expires: 31 });
|
||||
Cookies.set('name', data.name, { expires: 31 });
|
||||
debugger;
|
||||
window.location = "/";
|
||||
}
|
||||
|
||||
}).fail(function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
}else{
|
||||
window.location = "/";
|
||||
debugger;
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
<!-- LOGIN -->
|
||||
<!--__________________________________________________________-->
|
||||
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
|
||||
<div th:fragment="header (page, guild_name)">
|
||||
<div th:fragment="header (page, guild_name, redirect_url)">
|
||||
<nav class="blue-grey darken-4 z-depth-3" role="navigation" >
|
||||
<div class="nav-wrapper container">
|
||||
<a id="logo-container" href="/" class="brand-logo">Claptrap Bot</a>
|
||||
@ -90,11 +90,22 @@
|
||||
<!--________________________________________-->
|
||||
<div id="modal_connection" class="modal">
|
||||
<div class="modal-content">
|
||||
|
||||
<div class="row center">
|
||||
<div class="col s12">
|
||||
<h3 class="" style="font-weight: bold">Sign in</h3>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row center">
|
||||
<a class="btn waves-effect waves-light" style="background-color: #7289DA" th:href="${redirect_url}">
|
||||
Sign in with discord <i class="fab fa-discord fa-lg" style="margin-left: 2px"></i>
|
||||
</a>
|
||||
</div>
|
||||
<div class="row center">
|
||||
<div class="col s12">
|
||||
<h3 style="font-weight: bold">Or</h3>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row center" style="margin-bottom: 0px">
|
||||
<form name="login_form" id="login_form" action="javascript:void(0);" onsubmit="tryConnection()">
|
||||
<div class="row" style="margin-bottom: 0px">
|
||||
|
@ -12,11 +12,13 @@
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
|
||||
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
|
||||
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<body class="blue-grey lighten-5" >
|
||||
|
||||
<div th:replace="header :: header ('home',${guild_name})">...</div>
|
||||
<div th:replace="header :: header ('home',${guild_name},${redirect_url})">...</div>
|
||||
|
||||
<div class="section no-pad-bot main" id="index-banner">
|
||||
<div class="center center-align">
|
||||
@ -34,6 +36,8 @@
|
||||
<script th:src="@{/js/materialize.js}"></script>
|
||||
<script th:src="@{/js/navabar.js}"></script>
|
||||
<script th:src="@{/js/js.cookie.js}"></script>
|
||||
<script src="https://use.fontawesome.com/releases/v5.3.1/js/all.js" crossorigin="anonymous"></script>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
<body class="blue-grey lighten-5" >
|
||||
|
||||
<div th:replace="header :: header ('music',${guild_name})">...</div>
|
||||
<div th:replace="header :: header ('music',${guild_name},${redirect_url})">...</div>
|
||||
|
||||
<div class="section no-pad-bot main" id="index-banner">
|
||||
<div class="row">
|
||||
|
57
src/main/resources/templates/oauthCallback.html
Normal file
57
src/main/resources/templates/oauthCallback.html
Normal file
@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0"/>
|
||||
<title>Claptrap Bot</title>
|
||||
<link rel="icon"
|
||||
type="image/x-icon"
|
||||
href="/favicon.png"/>
|
||||
|
||||
<!-- CSS -->
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
|
||||
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
|
||||
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<body class="blue-grey lighten-5" >
|
||||
|
||||
<div th:replace="header :: header ('home',${guild_name},${redirect_url})">...</div>
|
||||
|
||||
<div class="section no-pad-bot main" id="index-banner">
|
||||
<div class="row center" >
|
||||
<div class="preloader-wrapper big active">
|
||||
<div class="spinner-layer spinner-blue-only">
|
||||
<div class="circle-clipper left">
|
||||
<div class="circle"></div>
|
||||
</div>
|
||||
<div class="gap-patch">
|
||||
<div class="circle"></div>
|
||||
</div>
|
||||
<div class="circle-clipper right">
|
||||
<div class="circle"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row center center-align">
|
||||
<h3>Please wait</h3>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Scripts-->
|
||||
<script th:src="@{/js/jquery-3.3.1.min.js}"></script>
|
||||
<script th:src="@{/js/materialize.js}"></script>
|
||||
<script th:src="@{/js/navabar.js}"></script>
|
||||
<script th:src="@{/js/js.cookie.js}"></script>
|
||||
<script th:src="@{/js/oauthCallback.js}"></script>
|
||||
|
||||
<script src="https://use.fontawesome.com/releases/v5.3.1/js/all.js" crossorigin="anonymous"></script>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user