From 6d3fc46e3450cb95ea81aa2ccb649b26b660715b Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 2 Sep 2018 23:59:41 +0200 Subject: [PATCH] Add Callback Endpoint to API --- Geekbot.net/Program.cs | 2 +- .../Callback/CallbackController.cs | 55 +++++++++++++++++++ .../Callback/CallbackTokenResponseDto.cs | 11 ++++ Geekbot.net/WebApi/WebApiStartup.cs | 6 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs create mode 100644 Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index e2f81b2..f2a6291 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -203,7 +203,7 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize()); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings); return Task.CompletedTask; } } diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs b/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs new file mode 100644 index 0000000..58bdb83 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord.WebSocket; +using Geekbot.net.Lib.GlobalSettings; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; + +namespace Geekbot.net.WebApi.Controllers.Callback +{ + public class CallbackController : Controller + { + private readonly DiscordSocketClient _client; + private readonly IGlobalSettings _globalSettings; + + public CallbackController(DiscordSocketClient client, IGlobalSettings globalSettings) + { + _client = client; + _globalSettings = globalSettings; + } + + [Route("/callback")] + public async Task DoCallback([FromQuery] string code) + { + var token = ""; + using (var client = new HttpClient()) + { + client.BaseAddress = new Uri("https://discordapp.com"); + var appInfo = await _client.GetApplicationInfoAsync(); + var accessToken = _globalSettings.GetKey("OAuthToken"); + var callbackUrl = _globalSettings.GetKey("OAuthCallbackUrl"); + + var form = new Dictionary(); + form.Add("client_id", appInfo.Id.ToString()); + form.Add("client_secret", accessToken); + form.Add("grant_type", "authorization_code"); + form.Add("code", code); + form.Add("scope", "identify email guilds"); + form.Add("redirect_uri", callbackUrl); + + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); + var result = await client.PostAsync("/api/oauth2/token", new FormUrlEncodedContent(form)); + result.EnsureSuccessStatusCode(); + + var stringResponse = await result.Content.ReadAsStringAsync(); + var responseData = JsonConvert.DeserializeObject(stringResponse); + token = responseData.access_token; + } + + return new RedirectResult($"https://geekbot.pizzaandcoffee.rocks/login?token={token}", false); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs b/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs new file mode 100644 index 0000000..3c81592 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs @@ -0,0 +1,11 @@ +namespace Geekbot.net.WebApi.Controllers.Callback +{ + public class CallbackTokenResponseDto + { + public string access_token { get; set; } + public string token_type { get; set; } + public int expires_in { get; set; } + public string refresh_token { get; set; } + public string scope { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/Geekbot.net/WebApi/WebApiStartup.cs index e91ff9f..e93a8d5 100644 --- a/Geekbot.net/WebApi/WebApiStartup.cs +++ b/Geekbot.net/WebApi/WebApiStartup.cs @@ -1,8 +1,10 @@ using System.Net; using System.Reflection; using Discord.Commands; +using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; +using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; using Geekbot.net.WebApi.Logging; using Microsoft.AspNetCore; @@ -16,7 +18,7 @@ namespace Geekbot.net.WebApi public static class WebApiStartup { public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, - DatabaseContext databaseContext) + DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings) { WebHost.CreateDefaultBuilder() .UseKestrel(options => @@ -28,6 +30,8 @@ namespace Geekbot.net.WebApi services.AddMvc(); services.AddSingleton(commandService); services.AddSingleton(databaseContext); + services.AddSingleton(client); + services.AddSingleton(globalSettings); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin",