Merge branch 'develop' into master #patch
This commit is contained in:
commit
602e859a42
129
ChaosBot/WebServer/App/DiscordController.cs
Normal file
129
ChaosBot/WebServer/App/DiscordController.cs
Normal file
@ -0,0 +1,129 @@
|
||||
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;
|
||||
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<IActionResult> Index(string code = null)
|
||||
{
|
||||
string redirectUri = $"{Request.Scheme}://{Request.Host}/discord";
|
||||
string clientId = Program.AppSettingsHandler.GetValue<string>("Discord:ClientId");
|
||||
string clientSecret = Program.AppSettingsHandler.GetValue<string>("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<string, string> values = new Dictionary<string, string>
|
||||
{
|
||||
{ "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<DiscordOauthResponse>(responseString);
|
||||
|
||||
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<DiscordUserResponse>(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<DiscordGuildResponse> userResponse = JsonConvert.DeserializeObject<List<DiscordGuildResponse>>(responseString);
|
||||
|
||||
return Json(userResponse);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user