Created
July 12, 2020 14:19
-
-
Save HarryET/5fb8719997e95dc413c486395bb47680 to your computer and use it in GitHub Desktop.
ASP.Net Core Auth Discord API Extention [AspNet.Security.OAuth.Discord]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Security.Claims; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Authentication.OAuth; | |
using Newtonsoft.Json.Linq; | |
/* | |
* Credits To HarryET#2954 & https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/issues/209 | |
*/ | |
namespace Website.Extensions | |
{ | |
public static class DiscordApiExtention | |
{ | |
public static async Task<Claim> GetGuildClaims(OAuthCreatingTicketContext context) | |
{ | |
var request = new HttpRequestMessage(HttpMethod.Get, "https://discord.com/api/users/@me/guilds"); | |
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken); | |
var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None); | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception("failed to get guilds"); | |
} | |
var payload = JArray.Parse(await response.Content.ReadAsStringAsync()); | |
Claim claim = new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/guilds", payload.ToString(),ClaimValueTypes.String); | |
return claim; | |
} | |
public static async Task<Claim> GetGuildMemberData(OAuthCreatingTicketContext context) | |
{ | |
string BotToken = "BOT_TOKEN"; | |
string GuildId = "GUILD_ID"; | |
string MemberId = ""; | |
foreach (var claim in context.Identity.Claims) | |
{ | |
if (claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier") | |
{ | |
MemberId = claim.Value; | |
} | |
} | |
var request = new HttpRequestMessage(HttpMethod.Get, $"https://discord.com/api/guilds/{GuildId}/members/{MemberId}"); | |
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
request.Headers.Authorization = new AuthenticationHeaderValue("Bot", BotToken); | |
var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None); | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception("failed to get member data"); | |
} | |
var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); | |
return new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/guildsMemberData", payload.ToString(), ClaimValueTypes.String); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* CODE HERE | |
*/ | |
.AddDiscord(options => | |
{ | |
options.ClientId = "Client Id"; | |
options.ClientSecret = "Client Secret"; | |
options.Scope.Add("guilds"); | |
options.SaveTokens = true; | |
options.Events = new OAuthEvents | |
{ | |
OnCreatingTicket = async context => | |
{ | |
Claim guildClaim = await DiscordApiExtention.GetGuildClaims(context); | |
context.Identity.AddClaim(guildClaim); | |
Claim guildMemberData = await DiscordApiExtention.GetGuildMemberData(context); | |
context.Identity.AddClaim(guildMemberData); | |
} | |
}; | |
}); | |
/* | |
* CODE HERE | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment