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>
<Company>Toodle HomeLab</Company>
<Copyright>Toodle Netmash</Copyright>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
</PropertyGroup>
<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" />
</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
◾ Initial release of Netmash.Security.Authentication.Tuitio

View File

@ -17,14 +17,14 @@ namespace Netmash.Security.Authentication.Tuitio
{
public class TuitioAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IIdentityService _identityService;
private readonly ITuitioService _tuitioService;
private readonly IAuthenticationOptions _authenticationOptions;
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)
{
_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<AuthorizationResult> 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<string, string>()
{
{ 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);
}