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"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <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> <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> <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> <RepositoryUrl>https://lab.code-rove.com/gitea/bricks/netmash</RepositoryUrl>
@ -13,14 +13,12 @@
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<Company>Toodle HomeLab</Company> <Company>Toodle HomeLab</Company>
<Copyright>Toodle Netmash</Copyright> <Copyright>Toodle Netmash</Copyright>
<Version>2.2.1</Version> <Version>2.3.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Metadata" Version="6.0.15" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.2.0" /> <PackageReference Include="Tuitio.Wrapper" Version="2.3.0" />
<PackageReference Include="Tuitio.Wrapper" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
</ItemGroup> </ItemGroup>
<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. ◾ 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 ◾ Tuitio nuget packages upgrade
◾ Added authenticated user groups and roles in claims. ◾ Added authenticated user groups and roles in claims.

View File

@ -5,7 +5,7 @@ using System;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using Tuitio.PublishedLanguage.Dto; using Tuitio.PublishedLanguage.Dto;
using c = Netmash.Security.Authentication.Tuitio.Constants; using C = Netmash.Security.Authentication.Tuitio.Constants;
namespace Netmash.Security.Authentication.Tuitio.Services namespace Netmash.Security.Authentication.Tuitio.Services
{ {
@ -25,7 +25,7 @@ namespace Netmash.Security.Authentication.Tuitio.Services
var claims = new[] { var claims = new[] {
new Claim(ClaimTypes.NameIdentifier, guestId.ToString()), new Claim(ClaimTypes.NameIdentifier, guestId.ToString()),
new Claim(ClaimTypes.Name, guestName), new Claim(ClaimTypes.Name, guestName),
new Claim(c.ClaimTypes.IsAnonymousGuest, bool.TrueString) new Claim(C.ClaimTypes.IsAnonymousGuest, bool.TrueString)
}; };
var ticket = GetAuthenticationTicket(claims); var ticket = GetAuthenticationTicket(claims);
@ -41,21 +41,21 @@ namespace Netmash.Security.Authentication.Tuitio.Services
{ {
{ ClaimTypes.NameIdentifier, authorization.UserId }, { ClaimTypes.NameIdentifier, authorization.UserId },
{ ClaimTypes.Name, authorization.UserName }, { 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.FirstName, ClaimTypes.GivenName, C.ClaimTypes.FirstName);
claimCollection.TryAddClaim(authorization.LastName, ClaimTypes.Surname, c.ClaimTypes.LastName); claimCollection.TryAddClaim(authorization.LastName, ClaimTypes.Surname, C.ClaimTypes.LastName);
claimCollection.TryAddClaim(authorization.Email, ClaimTypes.Email); claimCollection.TryAddClaim(authorization.Email, ClaimTypes.Email);
claimCollection.TryAddClaim(authorization.UserGroups, c.ClaimTypes.UserGroups); claimCollection.TryAddClaim(authorization.UserGroups, C.ClaimTypes.UserGroups);
claimCollection.TryAddClaim(authorization.UserRoles, c.ClaimTypes.UserRoles); claimCollection.TryAddClaim(authorization.UserRoles, C.ClaimTypes.UserRoles);
claimCollection.TryAddRange(authorization.Claims, (key, value) => 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."); _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); var ticket = GetAuthenticationTicket(claims);
return ticket; return ticket;

View File

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

View File

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