Add Callback Endpoint to API
This commit is contained in:
parent
d4c0899ba9
commit
6d3fc46e34
4 changed files with 72 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<IActionResult> 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<string, string>();
|
||||
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<CallbackTokenResponseDto>(stringResponse);
|
||||
token = responseData.access_token;
|
||||
}
|
||||
|
||||
return new RedirectResult($"https://geekbot.pizzaandcoffee.rocks/login?token={token}", false);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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>(commandService);
|
||||
services.AddSingleton<DatabaseContext>(databaseContext);
|
||||
services.AddSingleton<DiscordSocketClient>(client);
|
||||
services.AddSingleton<IGlobalSettings>(globalSettings);
|
||||
services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowSpecificOrigin",
|
||||
|
|
Loading…
Reference in a new issue