2020-12-21 22:58:40 +02:00
|
|
|
|
using IdentityServer.PublishedLanguage.Dto;
|
|
|
|
|
using IdentityServer.Wrapper.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2023-01-12 01:01:36 +02:00
|
|
|
|
using Netmash.Security.Authentication.Identity.Abstractions;
|
2022-01-21 23:52:11 +02:00
|
|
|
|
using System;
|
2021-11-12 02:40:46 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-01-12 01:01:36 +02:00
|
|
|
|
using c = Netmash.Security.Authentication.Identity.Constants;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
|
2023-01-12 01:01:36 +02:00
|
|
|
|
namespace Netmash.Security.Authentication.Identity
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
2021-06-26 21:31:31 +03:00
|
|
|
|
public class IdentityAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly IIdentityService _identityService;
|
2021-06-26 21:20:49 +03:00
|
|
|
|
private readonly IAuthenticationOptions _authenticationOptions;
|
2022-01-21 23:52:11 +02:00
|
|
|
|
private readonly ILogger<IdentityAuthenticationHandler> _logger;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
|
2022-01-21 23:52:11 +02:00
|
|
|
|
public IdentityAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService, IAuthenticationOptions authenticationOptions, ILogger<IdentityAuthenticationHandler> logger)
|
|
|
|
|
: base(options, loggerFactory, encoder, clock)
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
|
|
|
|
_identityService = identityService;
|
2021-06-26 21:20:49 +03:00
|
|
|
|
_authenticationOptions = authenticationOptions;
|
2022-01-21 23:52:11 +02:00
|
|
|
|
_logger = logger;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
|
{
|
2021-11-24 03:40:30 +02:00
|
|
|
|
var token = GetAuthorizationToken();
|
|
|
|
|
if (token != null)
|
2021-06-26 21:20:49 +03:00
|
|
|
|
{
|
2021-11-24 03:40:30 +02:00
|
|
|
|
TokenCore tokenCore;
|
|
|
|
|
try
|
2021-06-26 21:20:49 +03:00
|
|
|
|
{
|
2021-11-24 03:40:30 +02:00
|
|
|
|
tokenCore = await _identityService.Authorize(token);
|
2021-06-26 21:20:49 +03:00
|
|
|
|
}
|
2022-01-21 23:52:11 +02:00
|
|
|
|
catch (Exception e)
|
2021-11-24 03:40:30 +02:00
|
|
|
|
{
|
2022-01-21 23:52:11 +02:00
|
|
|
|
_logger.LogError(e, "Identity service authorization failed");
|
2021-11-24 03:40:30 +02:00
|
|
|
|
return AuthenticateResult.Fail("Invalid authorization");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tokenCore == null)
|
|
|
|
|
return AuthenticateResult.Fail("Invalid token");
|
2021-06-26 21:20:49 +03:00
|
|
|
|
|
2021-11-24 03:40:30 +02:00
|
|
|
|
var ticket = GetAuthenticationTicket(tokenCore);
|
|
|
|
|
return AuthenticateResult.Success(ticket);
|
2021-06-26 21:20:49 +03:00
|
|
|
|
}
|
2020-12-21 22:58:40 +02:00
|
|
|
|
|
2021-11-24 03:40:30 +02:00
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetAuthorizationToken()
|
|
|
|
|
{
|
|
|
|
|
if (Request.Headers.ContainsKey("Authorization"))
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
|
|
|
|
var authorizationHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
|
|
|
|
|
var token = authorizationHeader.Parameter;
|
2021-11-24 03:40:30 +02:00
|
|
|
|
return token;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
}
|
2021-11-24 03:40:30 +02:00
|
|
|
|
|
2022-01-21 23:52:11 +02:00
|
|
|
|
if (_authenticationOptions.AcceptTokenFromQuery
|
2021-11-24 03:40:30 +02:00
|
|
|
|
&& Request.Query.Count > 0
|
|
|
|
|
&& Request.Query.ContainsKey(c.QueryParams.Token))
|
2020-12-21 22:58:40 +02:00
|
|
|
|
{
|
2021-11-24 03:40:30 +02:00
|
|
|
|
var token = Request.Query[c.QueryParams.Token];
|
|
|
|
|
return token.ToString();
|
2020-12-21 22:58:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 03:40:30 +02:00
|
|
|
|
return null;
|
2021-06-26 21:20:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 02:40:46 +02:00
|
|
|
|
private AuthenticationTicket GetGuestAuthenticationTicket(int guestId, string guestName)
|
2021-06-26 21:20:49 +03:00
|
|
|
|
{
|
2020-12-21 22:58:40 +02:00
|
|
|
|
var claims = new[] {
|
2021-11-12 02:40:46 +02:00
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, guestId.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Name, guestName),
|
|
|
|
|
new Claim(Constants.ClaimTypes.IsGuestUser, bool.TrueString)
|
2020-12-21 22:58:40 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-12 02:40:46 +02:00
|
|
|
|
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 },
|
2021-11-14 00:21:13 +02:00
|
|
|
|
{ Constants.ClaimTypes.UserName, tokenCore.UserName }
|
2021-11-12 02:40:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-14 00:21:13 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2021-11-12 02:40:46 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2020-12-21 22:58:40 +02:00
|
|
|
|
var identity = new ClaimsIdentity(claims, Scheme.Name);
|
|
|
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
|
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
|
|
|
|
|
2021-06-26 21:20:49 +03:00
|
|
|
|
return ticket;
|
2020-12-21 22:58:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|