Netmash.Security.Authentication.Tuitio .net 8 upgrade

master
Tudor Stanciu 2025-03-24 02:15:20 +02:00
parent 7cce362dca
commit a5d5b3c8aa
6 changed files with 33 additions and 31 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Description>Netmash.Security.Authentication.Tuitio configures authentication with a Tuitio instance in a .NET environment for a .NET API.</Description>
<PackageProjectUrl>https://lab.code-rove.com/gitea/bricks/netmash/src/branch/master/src/security/authentication/Netmash.Security.Authentication.Tuitio</PackageProjectUrl>
<RepositoryUrl>https://lab.code-rove.com/gitea/bricks/netmash</RepositoryUrl>
@ -13,14 +13,12 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<Company>Toodle HomeLab</Company>
<Copyright>Toodle Netmash</Copyright>
<Version>2.2.1</Version>
<Version>2.3.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Metadata" Version="6.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.2.0" />
<PackageReference Include="Tuitio.Wrapper" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Tuitio.Wrapper" Version="2.3.0" />
</ItemGroup>
<ItemGroup>

View File

@ -1,4 +1,8 @@
2.2.1 release [2023-04-12 21:54]
2.3.0 release [2025-03-24 02:09]
◾ Tuitio nuget packages upgrade
◾ .NET 8 upgrade
2.2.1 release [2023-04-12 21:54]
◾ The authentication handler has been updated to skip the token validation if the method from controller is marked with [AllowAnonymous] attribute.
◾ Tuitio nuget packages upgrade
◾ Added authenticated user groups and roles in claims.

View File

@ -5,7 +5,7 @@ using System;
using System.Linq;
using System.Security.Claims;
using Tuitio.PublishedLanguage.Dto;
using c = Netmash.Security.Authentication.Tuitio.Constants;
using C = Netmash.Security.Authentication.Tuitio.Constants;
namespace Netmash.Security.Authentication.Tuitio.Services
{
@ -25,7 +25,7 @@ namespace Netmash.Security.Authentication.Tuitio.Services
var claims = new[] {
new Claim(ClaimTypes.NameIdentifier, guestId.ToString()),
new Claim(ClaimTypes.Name, guestName),
new Claim(c.ClaimTypes.IsAnonymousGuest, bool.TrueString)
new Claim(C.ClaimTypes.IsAnonymousGuest, bool.TrueString)
};
var ticket = GetAuthenticationTicket(claims);
@ -41,21 +41,21 @@ namespace Netmash.Security.Authentication.Tuitio.Services
{
{ ClaimTypes.NameIdentifier, authorization.UserId },
{ ClaimTypes.Name, authorization.UserName },
{ c.ClaimTypes.UserName, authorization.UserName }
{ C.ClaimTypes.UserName, authorization.UserName }
};
claimCollection.TryAddClaim(authorization.FirstName, ClaimTypes.GivenName, c.ClaimTypes.FirstName);
claimCollection.TryAddClaim(authorization.LastName, ClaimTypes.Surname, c.ClaimTypes.LastName);
claimCollection.TryAddClaim(authorization.FirstName, ClaimTypes.GivenName, C.ClaimTypes.FirstName);
claimCollection.TryAddClaim(authorization.LastName, ClaimTypes.Surname, C.ClaimTypes.LastName);
claimCollection.TryAddClaim(authorization.Email, ClaimTypes.Email);
claimCollection.TryAddClaim(authorization.UserGroups, c.ClaimTypes.UserGroups);
claimCollection.TryAddClaim(authorization.UserRoles, c.ClaimTypes.UserRoles);
claimCollection.TryAddClaim(authorization.UserGroups, C.ClaimTypes.UserGroups);
claimCollection.TryAddClaim(authorization.UserRoles, C.ClaimTypes.UserRoles);
claimCollection.TryAddRange(authorization.Claims, (key, value) =>
{
_logger.LogWarning($"There is already a claim with key {key} in the collection. The combination {key}:{value} will be ignored.");
});
var claims = claimCollection.Select(z => new Claim(z.Key, z.Value.ToString(), z.Value.GetType().Name, c.ClaimIssuer.Tuitio)).ToArray();
var claims = claimCollection.Select(z => new Claim(z.Key, z.Value.ToString(), z.Value.GetType().Name, C.ClaimIssuer.Tuitio)).ToArray();
var ticket = GetAuthenticationTicket(claims);
return ticket;

View File

@ -7,7 +7,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Tuitio.PublishedLanguage.Dto;
using c = Netmash.Security.Authentication.Tuitio.Constants;
using C = Netmash.Security.Authentication.Tuitio.Constants;
namespace Netmash.Security.Authentication.Tuitio.Services
{
@ -27,7 +27,7 @@ namespace Netmash.Security.Authentication.Tuitio.Services
{
get
{
var groups = GetUserClaim<IEnumerable<RecordIdentifier>>(c.ClaimTypes.UserGroups, false, false);
var groups = GetUserClaim<IEnumerable<RecordIdentifier>>(C.ClaimTypes.UserGroups, false, false);
return groups.ToTuples();
}
}
@ -36,12 +36,12 @@ namespace Netmash.Security.Authentication.Tuitio.Services
{
get
{
var roles = GetUserClaim<IEnumerable<RecordIdentifier>>(c.ClaimTypes.UserRoles, false, false);
var roles = GetUserClaim<IEnumerable<RecordIdentifier>>(C.ClaimTypes.UserRoles, false, false);
return roles.ToTuples();
}
}
public bool IsAnonymousGuest => GetUserClaim<bool>(c.ClaimTypes.IsAnonymousGuest);
public bool IsAnonymousGuest => GetUserClaim<bool>(C.ClaimTypes.IsAnonymousGuest);
private T GetUserClaim<T>(string claimType, bool isMandatory = false, bool isPrimitiveType = true, string claimLabel = null)
{

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Netmash.Security.Authentication.Tuitio.Abstractions;
@ -9,9 +9,9 @@ using System;
using System.Net.Http.Headers;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Tuitio.PublishedLanguage.Dto;
using Tuitio.Wrapper.Services;
using c = Netmash.Security.Authentication.Tuitio.Constants;
using C = Netmash.Security.Authentication.Tuitio.Constants;
using Tdto = Tuitio.PublishedLanguage.Dto;
namespace Netmash.Security.Authentication.Tuitio
{
@ -21,8 +21,8 @@ namespace Netmash.Security.Authentication.Tuitio
private readonly IAuthenticationOptions _authenticationOptions;
private readonly ILogger<TuitioAuthenticationHandler> _logger;
public TuitioAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder, ISystemClock clock, ITuitioService tuitioService, IAuthenticationOptions authenticationOptions, ILogger<TuitioAuthenticationHandler> logger)
: base(options, loggerFactory, encoder, clock)
public TuitioAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder, ITuitioService tuitioService, IAuthenticationOptions authenticationOptions, ILogger<TuitioAuthenticationHandler> logger)
: base(options, loggerFactory, encoder)
{
_tuitioService = tuitioService;
_authenticationOptions = authenticationOptions;
@ -34,9 +34,8 @@ namespace Netmash.Security.Authentication.Tuitio
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
// if the method is marked with [AllowAnonymous], the handler will skip token validation.
var endpointFeature = Context.Features.Get<IEndpointFeature>();
var endpoint = endpointFeature?.Endpoint;
if (endpoint?.Metadata.GetMetadata<IAllowAnonymous>() != null)
var endpoint = Context.GetEndpoint();
if (endpoint?.Metadata?.GetMetadata<IAllowAnonymous>() != null)
{
return AuthenticateResult.NoResult();
}
@ -44,7 +43,8 @@ namespace Netmash.Security.Authentication.Tuitio
var token = GetAuthorizationToken();
if (token != null)
{
Envelope<AuthorizationResult> authorizationEnvelope;
Tdto.Envelope<Tdto.AuthorizationResult> authorizationEnvelope;
try
{
authorizationEnvelope = await _tuitioService.Authorize(token);
@ -83,9 +83,9 @@ namespace Netmash.Security.Authentication.Tuitio
if (_authenticationOptions.AcceptTokenFromQuery
&& Request.Query.Count > 0
&& Request.Query.ContainsKey(c.QueryParams.Token))
&& Request.Query.ContainsKey(C.QueryParams.Token))
{
var token = Request.Query[c.QueryParams.Token];
var token = Request.Query[C.QueryParams.Token];
return token.ToString();
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>