From 15149d9d186094da78d880ac095da144f577122c Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 7 Mar 2023 20:38:12 +0200 Subject: [PATCH] Tuitio refactoring --- ...mash.Security.Authentication.Tuitio.csproj | 4 +- .../ReleaseNotes.txt | 6 ++- .../TuitioAuthenticationHandler.cs | 46 +++++++++---------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/security/authentication/Netmash.Security.Authentication.Tuitio/Netmash.Security.Authentication.Tuitio.csproj b/src/security/authentication/Netmash.Security.Authentication.Tuitio/Netmash.Security.Authentication.Tuitio.csproj index 329d746..69e5051 100644 --- a/src/security/authentication/Netmash.Security.Authentication.Tuitio/Netmash.Security.Authentication.Tuitio.csproj +++ b/src/security/authentication/Netmash.Security.Authentication.Tuitio/Netmash.Security.Authentication.Tuitio.csproj @@ -13,11 +13,11 @@ README.md Toodle HomeLab Toodle Netmash - 2.0.0 + 2.1.0 - + diff --git a/src/security/authentication/Netmash.Security.Authentication.Tuitio/ReleaseNotes.txt b/src/security/authentication/Netmash.Security.Authentication.Tuitio/ReleaseNotes.txt index f2cef51..ea4d814 100644 --- a/src/security/authentication/Netmash.Security.Authentication.Tuitio/ReleaseNotes.txt +++ b/src/security/authentication/Netmash.Security.Authentication.Tuitio/ReleaseNotes.txt @@ -1,3 +1,7 @@ -2.0.0 release [2023-02-01 19:29] +2.1.0 release [2023-03-07 23:35] +◾ Tuitio nuget packages upgrade +◾ Tuitio refactoring + +2.0.0 release [2023-02-01 19:29] ◾ Tuitio rebranding ◾ Initial release of Netmash.Security.Authentication.Tuitio \ No newline at end of file diff --git a/src/security/authentication/Netmash.Security.Authentication.Tuitio/TuitioAuthenticationHandler.cs b/src/security/authentication/Netmash.Security.Authentication.Tuitio/TuitioAuthenticationHandler.cs index 89301c4..c613b23 100644 --- a/src/security/authentication/Netmash.Security.Authentication.Tuitio/TuitioAuthenticationHandler.cs +++ b/src/security/authentication/Netmash.Security.Authentication.Tuitio/TuitioAuthenticationHandler.cs @@ -17,14 +17,14 @@ namespace Netmash.Security.Authentication.Tuitio { public class TuitioAuthenticationHandler : AuthenticationHandler { - private readonly IIdentityService _identityService; + private readonly ITuitioService _tuitioService; private readonly IAuthenticationOptions _authenticationOptions; private readonly ILogger _logger; - public TuitioAuthenticationHandler(IOptionsMonitor options, ILoggerFactory loggerFactory, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService, IAuthenticationOptions authenticationOptions, ILogger logger) + public TuitioAuthenticationHandler(IOptionsMonitor options, ILoggerFactory loggerFactory, UrlEncoder encoder, ISystemClock clock, ITuitioService tuitioService, IAuthenticationOptions authenticationOptions, ILogger logger) : base(options, loggerFactory, encoder, clock) { - _identityService = identityService; + _tuitioService = tuitioService; _authenticationOptions = authenticationOptions; _logger = logger; } @@ -34,21 +34,21 @@ namespace Netmash.Security.Authentication.Tuitio var token = GetAuthorizationToken(); if (token != null) { - TokenCore tokenCore; + Envelope authorizationEnvelope; try { - tokenCore = await _identityService.Authorize(token); + authorizationEnvelope = await _tuitioService.Authorize(token); } catch (Exception e) { - _logger.LogError(e, "Identity service authorization failed"); + _logger.LogError(e, "Tuitio authorization failed"); return AuthenticateResult.Fail("Invalid authorization"); } - if (tokenCore == null) - return AuthenticateResult.Fail("Invalid token"); + if (!string.IsNullOrEmpty(authorizationEnvelope.Error)) + return AuthenticateResult.Fail(authorizationEnvelope.Error); - var ticket = GetAuthenticationTicket(tokenCore); + var ticket = GetAuthenticationTicket(authorizationEnvelope.Result); return AuthenticateResult.Success(ticket); } @@ -94,27 +94,27 @@ namespace Netmash.Security.Authentication.Tuitio return ticket; } - private AuthenticationTicket GetAuthenticationTicket(TokenCore tokenCore) + private AuthenticationTicket GetAuthenticationTicket(AuthorizationResult authorization) { var claimCollection = new Dictionary() { - { ClaimTypes.NameIdentifier, tokenCore.UserId.ToString() }, - { ClaimTypes.Name, tokenCore.UserName }, - { Constants.ClaimTypes.UserName, tokenCore.UserName } + { ClaimTypes.NameIdentifier, authorization.UserId.ToString() }, + { ClaimTypes.Name, authorization.UserName }, + { Constants.ClaimTypes.UserName, authorization.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 (authorization.FirstName != null) + claimCollection.Add(Constants.ClaimTypes.FirstName, authorization.FirstName); + if (authorization.LastName != null) + claimCollection.Add(Constants.ClaimTypes.LastName, authorization.LastName); + if (authorization.ProfilePictureUrl != null) + claimCollection.Add(Constants.ClaimTypes.ProfilePictureUrl, authorization.ProfilePictureUrl); + if (authorization.Email != null) + claimCollection.Add(ClaimTypes.Email, authorization.Email); - if (tokenCore.Claims != null && tokenCore.Claims.Any()) + if (authorization.Claims != null && authorization.Claims.Any()) { - foreach (var claim in tokenCore.Claims) + foreach (var claim in authorization.Claims) claimCollection.Add(claim.Key, claim.Value); }