113 lines
4.5 KiB
C#
113 lines
4.5 KiB
C#
using IdentityServer.PublishedLanguage.Dto;
|
|
using IdentityServer.Wrapper.Services;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NDB.Security.Authentication.Identity.Abstractions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Claims;
|
|
using System.Text.Encodings.Web;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace NDB.Security.Authentication.Identity
|
|
{
|
|
public class IdentityAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
|
{
|
|
private readonly IIdentityService _identityService;
|
|
private readonly IAuthenticationOptions _authenticationOptions;
|
|
|
|
public IdentityAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService, IAuthenticationOptions authenticationOptions)
|
|
: base(options, logger, encoder, clock)
|
|
{
|
|
_identityService = identityService;
|
|
_authenticationOptions = authenticationOptions;
|
|
}
|
|
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
{
|
|
if (!Request.Headers.ContainsKey("Authorization"))
|
|
{
|
|
var authenticateAsGuest = _authenticationOptions.AuthenticateAsGuest?.Invoke(Request) ?? false;
|
|
if (authenticateAsGuest)
|
|
{
|
|
var guestTicket = GetGuestAuthenticationTicket(_authenticationOptions.GuestUserId, _authenticationOptions.GuestUserName);
|
|
return AuthenticateResult.Success(guestTicket);
|
|
}
|
|
|
|
return AuthenticateResult.Fail("Missing Authorization Header");
|
|
}
|
|
|
|
TokenCore tokenCore;
|
|
try
|
|
{
|
|
var authorizationHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
|
|
var token = authorizationHeader.Parameter;
|
|
tokenCore = await _identityService.Authorize(token);
|
|
}
|
|
catch
|
|
{
|
|
return AuthenticateResult.Fail("Invalid authorization header");
|
|
}
|
|
|
|
if (tokenCore == null)
|
|
return AuthenticateResult.Fail("Invalid token");
|
|
|
|
var ticket = GetAuthenticationTicket(tokenCore);
|
|
return AuthenticateResult.Success(ticket);
|
|
}
|
|
|
|
private AuthenticationTicket GetGuestAuthenticationTicket(int guestId, string guestName)
|
|
{
|
|
var claims = new[] {
|
|
new Claim(ClaimTypes.NameIdentifier, guestId.ToString()),
|
|
new Claim(ClaimTypes.Name, guestName),
|
|
new Claim(Constants.ClaimTypes.IsGuestUser, bool.TrueString)
|
|
};
|
|
|
|
var ticket = GetAuthenticationTicket(claims);
|
|
return ticket;
|
|
}
|
|
|
|
private AuthenticationTicket GetAuthenticationTicket(TokenCore tokenCore)
|
|
{
|
|
var claimCollection = new Dictionary<string, string>()
|
|
{
|
|
{ ClaimTypes.NameIdentifier, tokenCore.UserId.ToString() },
|
|
{ ClaimTypes.Name, tokenCore.UserName },
|
|
{ Constants.ClaimTypes.UserName, tokenCore.UserName }
|
|
};
|
|
|
|
if (tokenCore.FirstName != null)
|
|
claimCollection.Add(Constants.ClaimTypes.FirstName, tokenCore.FirstName);
|
|
if (tokenCore.LastName != null)
|
|
claimCollection.Add(Constants.ClaimTypes.LastName, tokenCore.LastName);
|
|
if (tokenCore.ProfilePictureUrl != null)
|
|
claimCollection.Add(Constants.ClaimTypes.ProfilePictureUrl, tokenCore.ProfilePictureUrl);
|
|
if (tokenCore.Email != null)
|
|
claimCollection.Add(ClaimTypes.Email, tokenCore.Email);
|
|
|
|
if (tokenCore.Claims != null && tokenCore.Claims.Any())
|
|
{
|
|
foreach (var claim in tokenCore.Claims)
|
|
claimCollection.Add(claim.Key, claim.Value);
|
|
}
|
|
|
|
var claims = claimCollection.Select(z => new Claim(z.Key, z.Value)).ToArray();
|
|
var ticket = GetAuthenticationTicket(claims);
|
|
|
|
return ticket;
|
|
}
|
|
|
|
private AuthenticationTicket GetAuthenticationTicket(Claim[] claims)
|
|
{
|
|
var identity = new ClaimsIdentity(claims, Scheme.Name);
|
|
var principal = new ClaimsPrincipal(identity);
|
|
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
|
|
|
return ticket;
|
|
}
|
|
}
|
|
}
|