From 4cf3f6c28870fa45621c896baa3b955e1e1c3979 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Mon, 24 Aug 2020 17:22:02 +0200 Subject: [PATCH 1/2] Add discord oauth endpoint --- ChaosBot/WebServer/App/DiscordController.cs | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ChaosBot/WebServer/App/DiscordController.cs diff --git a/ChaosBot/WebServer/App/DiscordController.cs b/ChaosBot/WebServer/App/DiscordController.cs new file mode 100644 index 0000000..3bff07d --- /dev/null +++ b/ChaosBot/WebServer/App/DiscordController.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; +using NLog; + +namespace ChaosBot.WebServer.App +{ + [ApiController] + [Route("/discord")] + public class DiscordController : Controller + { + private static readonly HttpClient client = new HttpClient(); + private static readonly ILogger Logger = Program.Logger; + + [HttpGet] + public async Task Index(string code = null) + { + string redirectUri = $"{Request.Scheme}://{Request.Host}/discord"; + string clientId = Program.AppSettingsHandler.GetValue("Discord:ClientId"); + string clientSecret = Program.AppSettingsHandler.GetValue("Discord:ClientSecret"); + + if (code == null) + return Redirect($"https://discord.com/api/oauth2/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=identify%20guilds"); + + Dictionary values = new Dictionary + { + { "client_id", clientId }, + { "client_secret", clientSecret }, + { "grant_type", "authorization_code" }, + { "code", code }, + { "redirect_uri", redirectUri }, + { "scope", "identify guild" } + }; + + FormUrlEncodedContent content = new FormUrlEncodedContent(values); + HttpResponseMessage response = await client.PostAsync("https://discord.com/api/oauth2/token", content); + string responseString = await response.Content.ReadAsStringAsync(); + DiscordOauthResponse responseObject = JsonConvert.DeserializeObject(responseString); + + return LocalRedirect($"/#/?access_token={responseObject.access_token}"); + } + } + + internal class DiscordOauthResponse + { + public string error = null; + public string error_description = null; + + public string access_token = null; + public int expires_in = 0; + public string refresh_token = null; + public string scope = null; + public string token_type = null; + } +} \ No newline at end of file From f56e9c7e42d818bb8aae528c9f89a2c3cfd57d2c Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Mon, 24 Aug 2020 18:22:10 +0200 Subject: [PATCH 2/2] Set up endpoints for user and guilds --- ChaosBot/WebServer/App/DiscordController.cs | 71 +++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/ChaosBot/WebServer/App/DiscordController.cs b/ChaosBot/WebServer/App/DiscordController.cs index 3bff07d..7d06c68 100644 --- a/ChaosBot/WebServer/App/DiscordController.cs +++ b/ChaosBot/WebServer/App/DiscordController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -42,6 +43,49 @@ namespace ChaosBot.WebServer.App return LocalRedirect($"/#/?access_token={responseObject.access_token}"); } + + [HttpGet("user")] + public IActionResult GetUser(string access_token) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token); + + HttpResponseMessage response; + using (HttpRequestMessage requestMessage = + new HttpRequestMessage(HttpMethod.Get, "https://discord.com/api/v7/users/@me")) + { + requestMessage.Headers.Authorization = + new AuthenticationHeaderValue("Bearer", access_token); + response = client.SendAsync(requestMessage).GetAwaiter().GetResult(); + } + string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + DiscordUserResponse userResponse = JsonConvert.DeserializeObject(responseString); + + return Json(new + { + id = userResponse.id, + username = $"{userResponse.username}#{userResponse.discriminator}", + avatar = userResponse.avatar + }); + } + + [HttpGet("guilds")] + public IActionResult GetGuilds(string access_token) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token); + + HttpResponseMessage response; + using (HttpRequestMessage requestMessage = + new HttpRequestMessage(HttpMethod.Get, "https://discord.com/api/v7/users/@me/guilds")) + { + requestMessage.Headers.Authorization = + new AuthenticationHeaderValue("Bearer", access_token); + response = client.SendAsync(requestMessage).GetAwaiter().GetResult(); + } + string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + List userResponse = JsonConvert.DeserializeObject>(responseString); + + return Json(userResponse); + } } internal class DiscordOauthResponse @@ -55,4 +99,31 @@ namespace ChaosBot.WebServer.App public string scope = null; public string token_type = null; } + + public class DiscordUserResponse + { + public string id; + public string username; + public string discriminator; + public string avatar = null; + public bool bot = false; + public bool system = false; + public bool mfa_enabled = false; + public string locale = null; + public bool verified = false; + public string email = null; + public int flags = 0; + public int premium_type = 0; + public int public_flags = 0; + } + + public class DiscordGuildResponse + { + public string id; + public string name; + public string icon; + public bool owner; + public int permissions; + public int permissions_new; + } } \ No newline at end of file