From 43c316391fe2e0229d27cbdd1bf1fba4c78db9dd Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 10 Sep 2018 15:53:56 +0300 Subject: [PATCH] Add OAuth login, ready for test --- .../java/net/Broken/DB/Entity/UserEntity.java | 8 +++ src/main/java/net/Broken/Init.java | 1 + .../RestApi/UserManagerAPIController.java | 15 +++++ .../net/Broken/Tools/UserManager/Oauth.java | 63 +++++++++++++++++++ .../net/Broken/webView/GeneralWebView.java | 7 +++ .../java/net/Broken/webView/MusicWebView.java | 2 + src/main/resources/static/js/oauthCallback.js | 33 ++++++++++ src/main/resources/templates/header.html | 13 +++- src/main/resources/templates/index.html | 6 +- src/main/resources/templates/music.html | 2 +- .../resources/templates/oauthCallback.html | 57 +++++++++++++++++ 11 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/static/js/oauthCallback.js create mode 100644 src/main/resources/templates/oauthCallback.html diff --git a/src/main/java/net/Broken/DB/Entity/UserEntity.java b/src/main/java/net/Broken/DB/Entity/UserEntity.java index c3132c3..e61a64d 100644 --- a/src/main/java/net/Broken/DB/Entity/UserEntity.java +++ b/src/main/java/net/Broken/DB/Entity/UserEntity.java @@ -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; } diff --git a/src/main/java/net/Broken/Init.java b/src/main/java/net/Broken/Init.java index b0014b4..fc74f26 100644 --- a/src/main/java/net/Broken/Init.java +++ b/src/main/java/net/Broken/Init.java @@ -54,6 +54,7 @@ public class Init { MainBot.jda = jda; jda.setAutoReconnect(true); + /************************************* * Definition des commande * *************************************/ diff --git a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java index dae025c..e98dd66 100644 --- a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java +++ b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java @@ -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 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); + + + + } + + + + } diff --git a/src/main/java/net/Broken/Tools/UserManager/Oauth.java b/src/main/java/net/Broken/Tools/UserManager/Oauth.java index 1509b3f..b4ad19a 100644 --- a/src/main/java/net/Broken/Tools/UserManager/Oauth.java +++ b/src/main/java/net/Broken/Tools/UserManager/Oauth.java @@ -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 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; + } + } } diff --git a/src/main/java/net/Broken/webView/GeneralWebView.java b/src/main/java/net/Broken/webView/GeneralWebView.java index 6a4df3a..5160304 100644 --- a/src/main/java/net/Broken/webView/GeneralWebView.java +++ b/src/main/java/net/Broken/webView/GeneralWebView.java @@ -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"; + } + diff --git a/src/main/java/net/Broken/webView/MusicWebView.java b/src/main/java/net/Broken/webView/MusicWebView.java index 6dddf53..8c24428 100644 --- a/src/main/java/net/Broken/webView/MusicWebView.java +++ b/src/main/java/net/Broken/webView/MusicWebView.java @@ -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"); } diff --git a/src/main/resources/static/js/oauthCallback.js b/src/main/resources/static/js/oauthCallback.js new file mode 100644 index 0000000..2e955fe --- /dev/null +++ b/src/main/resources/static/js/oauthCallback.js @@ -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; +} diff --git a/src/main/resources/templates/header.html b/src/main/resources/templates/header.html index eb5181c..e73f31d 100644 --- a/src/main/resources/templates/header.html +++ b/src/main/resources/templates/header.html @@ -12,7 +12,7 @@ -
+