forked from Sarah/Lieb-Website
Add project files.
This commit is contained in:
parent
d867fa03f3
commit
00c33a335c
41 changed files with 1543 additions and 0 deletions
12
Discord.OAuth2/Discord.OAuth2.csproj
Normal file
12
Discord.OAuth2/Discord.OAuth2.csproj
Normal file
|
@ -0,0 +1,12 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="../../Discord.OAuth2.targets" />
|
||||
<PropertyGroup>
|
||||
<Description>ASP.Net Core middleware that enables an application to support Discord's OAuth 2.0 authentication workflow.</Description>
|
||||
<RootNamespace>Discord.OAuth2</RootNamespace>
|
||||
<AssemblyName>Discord.OAuth2</AssemblyName>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OAuth" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
12
Discord.OAuth2/DiscordDefaults.cs
Normal file
12
Discord.OAuth2/DiscordDefaults.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
namespace Discord.OAuth2
|
||||
{
|
||||
public static class DiscordDefaults
|
||||
{
|
||||
public const string AuthenticationScheme = "Discord";
|
||||
public const string DisplayName = "Discord";
|
||||
|
||||
public static readonly string AuthorizationEndpoint = "https://discordapp.com/api/oauth2/authorize";
|
||||
public static readonly string TokenEndpoint = "https://discordapp.com/api/oauth2/token";
|
||||
public static readonly string UserInformationEndpoint = "https://discordapp.com/api/users/@me";
|
||||
}
|
||||
}
|
22
Discord.OAuth2/DiscordExtensions.cs
Normal file
22
Discord.OAuth2/DiscordExtensions.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Discord.OAuth2;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public static class DiscordAuthenticationOptionsExtensions
|
||||
{
|
||||
public static AuthenticationBuilder AddDiscord(this AuthenticationBuilder builder)
|
||||
=> builder.AddDiscord(DiscordDefaults.AuthenticationScheme, _ => { });
|
||||
|
||||
public static AuthenticationBuilder AddDiscord(this AuthenticationBuilder builder, Action<DiscordOptions> configureOptions)
|
||||
=> builder.AddDiscord(DiscordDefaults.AuthenticationScheme, configureOptions);
|
||||
|
||||
public static AuthenticationBuilder AddDiscord(this AuthenticationBuilder builder, string authenticationScheme, Action<DiscordOptions> configureOptions)
|
||||
=> builder.AddDiscord(authenticationScheme, DiscordDefaults.DisplayName, configureOptions);
|
||||
|
||||
public static AuthenticationBuilder AddDiscord(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<DiscordOptions> configureOptions)
|
||||
=> builder.AddOAuth<DiscordOptions, DiscordHandler>(authenticationScheme, displayName, configureOptions);
|
||||
}
|
||||
}
|
40
Discord.OAuth2/DiscordHandler.cs
Normal file
40
Discord.OAuth2/DiscordHandler.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.OAuth;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.OAuth2
|
||||
{
|
||||
internal class DiscordHandler : OAuthHandler<DiscordOptions>
|
||||
{
|
||||
public DiscordHandler(IOptionsMonitor<DiscordOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
|
||||
: base(options, logger, encoder, clock)
|
||||
{
|
||||
}
|
||||
|
||||
protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
var response = await Backchannel.SendAsync(request, Context.RequestAborted);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new HttpRequestException($"Failed to retrieve Discord user information ({response.StatusCode}).");
|
||||
|
||||
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
|
||||
|
||||
var context = new OAuthCreatingTicketContext(new ClaimsPrincipal(identity), properties, Context, Scheme, Options, Backchannel, tokens, payload);
|
||||
context.RunClaimActions();
|
||||
|
||||
await Events.CreatingTicket(context);
|
||||
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
|
||||
}
|
||||
}
|
||||
}
|
33
Discord.OAuth2/DiscordOptions.cs
Normal file
33
Discord.OAuth2/DiscordOptions.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.OAuth;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Discord.OAuth2
|
||||
{
|
||||
/// <summary> Configuration options for <see cref="DiscordHandler"/>. </summary>
|
||||
public class DiscordOptions : OAuthOptions
|
||||
{
|
||||
/// <summary> Initializes a new <see cref="DiscordOptions"/>. </summary>
|
||||
public DiscordOptions()
|
||||
{
|
||||
CallbackPath = new PathString("/signin-discord");
|
||||
AuthorizationEndpoint = DiscordDefaults.AuthorizationEndpoint;
|
||||
TokenEndpoint = DiscordDefaults.TokenEndpoint;
|
||||
UserInformationEndpoint = DiscordDefaults.UserInformationEndpoint;
|
||||
Scope.Add("identify");
|
||||
|
||||
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id", ClaimValueTypes.UInteger64);
|
||||
ClaimActions.MapJsonKey(ClaimTypes.Name, "username", ClaimValueTypes.String);
|
||||
ClaimActions.MapJsonKey(ClaimTypes.Email, "email", ClaimValueTypes.Email);
|
||||
ClaimActions.MapJsonKey("urn:discord:discriminator", "discriminator", ClaimValueTypes.UInteger32);
|
||||
ClaimActions.MapJsonKey("urn:discord:avatar", "avatar", ClaimValueTypes.String);
|
||||
ClaimActions.MapJsonKey("urn:discord:verified", "verified", ClaimValueTypes.Boolean);
|
||||
}
|
||||
|
||||
/// <summary> Gets or sets the Discord-assigned appId. </summary>
|
||||
public string AppId { get => ClientId; set => ClientId = value; }
|
||||
/// <summary> Gets or sets the Discord-assigned app secret. </summary>
|
||||
public string AppSecret { get => ClientSecret; set => ClientSecret = value; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue