IdentityAuthenticationHandler error handling

messaging
Tudor Stanciu 2022-01-21 23:52:11 +02:00
parent 08cb3b4c42
commit 9cf46796fd
1 changed files with 9 additions and 5 deletions

View File

@ -4,13 +4,14 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NDB.Security.Authentication.Identity.Abstractions; using NDB.Security.Authentication.Identity.Abstractions;
using c = NDB.Security.Authentication.Identity.Constants; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Security.Claims; using System.Security.Claims;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; using System.Threading.Tasks;
using c = NDB.Security.Authentication.Identity.Constants;
namespace NDB.Security.Authentication.Identity namespace NDB.Security.Authentication.Identity
{ {
@ -18,12 +19,14 @@ namespace NDB.Security.Authentication.Identity
{ {
private readonly IIdentityService _identityService; private readonly IIdentityService _identityService;
private readonly IAuthenticationOptions _authenticationOptions; private readonly IAuthenticationOptions _authenticationOptions;
private readonly ILogger<IdentityAuthenticationHandler> _logger;
public IdentityAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService, IAuthenticationOptions authenticationOptions) public IdentityAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService, IAuthenticationOptions authenticationOptions, ILogger<IdentityAuthenticationHandler> logger)
: base(options, logger, encoder, clock) : base(options, loggerFactory, encoder, clock)
{ {
_identityService = identityService; _identityService = identityService;
_authenticationOptions = authenticationOptions; _authenticationOptions = authenticationOptions;
_logger = logger;
} }
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
@ -36,8 +39,9 @@ namespace NDB.Security.Authentication.Identity
{ {
tokenCore = await _identityService.Authorize(token); tokenCore = await _identityService.Authorize(token);
} }
catch catch (Exception e)
{ {
_logger.LogError(e, "Identity service authorization failed");
return AuthenticateResult.Fail("Invalid authorization"); return AuthenticateResult.Fail("Invalid authorization");
} }
@ -67,7 +71,7 @@ namespace NDB.Security.Authentication.Identity
return token; return token;
} }
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))
{ {