Added Authorization

This commit is contained in:
t.ruspekhofer 2022-02-22 01:06:41 +01:00
parent 04049f31f6
commit e47d6488c2
15 changed files with 134 additions and 21 deletions

View file

@ -1,5 +1,8 @@
using Microsoft.AspNetCore.Authentication;
using Lieb.Data;
using Lieb.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Security.Claims;
@ -10,9 +13,12 @@ namespace Discord.OAuth2
{
internal class DiscordHandler : OAuthHandler<DiscordOptions>
{
public DiscordHandler(IOptionsMonitor<DiscordOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
private readonly Lieb.Data.LiebContext _LiebDbcontext;
public DiscordHandler(IOptionsMonitor<DiscordOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, Lieb.Data.LiebContext context)
: base(options, logger, encoder, clock)
{
_LiebDbcontext = context;
}
protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
@ -30,6 +36,16 @@ namespace Discord.OAuth2
context.RunClaimActions();
await Events.CreatingTicket(context);
LiebUser? user = await _LiebDbcontext.LiebUsers.Include(u => u.Roles).FirstOrDefaultAsync(m => m.DiscordUserId == 1);
if (user != null)
{
foreach (UserRole role in user.Roles)
{
context.Identity.AddClaim(new Claim(Constants.ClaimType, role.RoleName));
}
}
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}

View file

@ -16,6 +16,7 @@ namespace Discord.OAuth2
TokenEndpoint = DiscordDefaults.TokenEndpoint;
UserInformationEndpoint = DiscordDefaults.UserInformationEndpoint;
Scope.Add("identify");
//Scope.Add("guilds.members.read");
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id", ClaimValueTypes.UInteger64);
ClaimActions.MapJsonKey(ClaimTypes.Name, "username", ClaimValueTypes.String);