Tuitio refactoring

master
Tudor Stanciu 2023-03-07 20:38:12 +02:00
parent 4a3963b697
commit 15149d9d18
3 changed files with 30 additions and 26 deletions

View File

@ -13,11 +13,11 @@
<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.0.0</Version> <Version>2.1.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Tuitio.Wrapper" Version="2.0.0" /> <PackageReference Include="Tuitio.Wrapper" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
</ItemGroup> </ItemGroup>

View File

@ -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 ◾ Tuitio rebranding
◾ Initial release of Netmash.Security.Authentication.Tuitio ◾ Initial release of Netmash.Security.Authentication.Tuitio

View File

@ -17,14 +17,14 @@ namespace Netmash.Security.Authentication.Tuitio
{ {
public class TuitioAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> public class TuitioAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{ {
private readonly IIdentityService _identityService; private readonly ITuitioService _tuitioService;
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, IIdentityService identityService, IAuthenticationOptions authenticationOptions, 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) : base(options, loggerFactory, encoder, clock)
{ {
_identityService = identityService; _tuitioService = tuitioService;
_authenticationOptions = authenticationOptions; _authenticationOptions = authenticationOptions;
_logger = logger; _logger = logger;
} }
@ -34,21 +34,21 @@ namespace Netmash.Security.Authentication.Tuitio
var token = GetAuthorizationToken(); var token = GetAuthorizationToken();
if (token != null) if (token != null)
{ {
TokenCore tokenCore; Envelope<AuthorizationResult> authorizationEnvelope;
try try
{ {
tokenCore = await _identityService.Authorize(token); authorizationEnvelope = await _tuitioService.Authorize(token);
} }
catch (Exception e) catch (Exception e)
{ {
_logger.LogError(e, "Identity service authorization failed"); _logger.LogError(e, "Tuitio authorization failed");
return AuthenticateResult.Fail("Invalid authorization"); return AuthenticateResult.Fail("Invalid authorization");
} }
if (tokenCore == null) if (!string.IsNullOrEmpty(authorizationEnvelope.Error))
return AuthenticateResult.Fail("Invalid token"); return AuthenticateResult.Fail(authorizationEnvelope.Error);
var ticket = GetAuthenticationTicket(tokenCore); var ticket = GetAuthenticationTicket(authorizationEnvelope.Result);
return AuthenticateResult.Success(ticket); return AuthenticateResult.Success(ticket);
} }
@ -94,27 +94,27 @@ namespace Netmash.Security.Authentication.Tuitio
return ticket; return ticket;
} }
private AuthenticationTicket GetAuthenticationTicket(TokenCore tokenCore) private AuthenticationTicket GetAuthenticationTicket(AuthorizationResult authorization)
{ {
var claimCollection = new Dictionary<string, string>() var claimCollection = new Dictionary<string, string>()
{ {
{ ClaimTypes.NameIdentifier, tokenCore.UserId.ToString() }, { ClaimTypes.NameIdentifier, authorization.UserId.ToString() },
{ ClaimTypes.Name, tokenCore.UserName }, { ClaimTypes.Name, authorization.UserName },
{ Constants.ClaimTypes.UserName, tokenCore.UserName } { Constants.ClaimTypes.UserName, authorization.UserName }
}; };
if (tokenCore.FirstName != null) if (authorization.FirstName != null)
claimCollection.Add(Constants.ClaimTypes.FirstName, tokenCore.FirstName); claimCollection.Add(Constants.ClaimTypes.FirstName, authorization.FirstName);
if (tokenCore.LastName != null) if (authorization.LastName != null)
claimCollection.Add(Constants.ClaimTypes.LastName, tokenCore.LastName); claimCollection.Add(Constants.ClaimTypes.LastName, authorization.LastName);
if (tokenCore.ProfilePictureUrl != null) if (authorization.ProfilePictureUrl != null)
claimCollection.Add(Constants.ClaimTypes.ProfilePictureUrl, tokenCore.ProfilePictureUrl); claimCollection.Add(Constants.ClaimTypes.ProfilePictureUrl, authorization.ProfilePictureUrl);
if (tokenCore.Email != null) if (authorization.Email != null)
claimCollection.Add(ClaimTypes.Email, tokenCore.Email); 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); claimCollection.Add(claim.Key, claim.Value);
} }