tuitio/IdentityServer.Application/CommandHandlers/AuthorizeTokenHandler.cs

41 lines
1.4 KiB
C#
Raw Normal View History

2020-12-20 03:06:43 +02:00
using AutoMapper;
using IdentityServer.Application.Commands;
using IdentityServer.Application.Services;
using IdentityServer.PublishedLanguage.Dto;
using MediatR;
2020-12-21 01:28:22 +02:00
using Microsoft.Extensions.Logging;
2020-12-20 03:06:43 +02:00
using System.Threading;
using System.Threading.Tasks;
namespace IdentityServer.Application.CommandHandlers
{
public class AuthorizeTokenHandler : IRequestHandler<AuthorizeToken, TokenCore>
2020-12-20 03:06:43 +02:00
{
private readonly IUserService _userService;
private readonly IMapper _mapper;
2020-12-21 01:28:22 +02:00
private readonly ILogger<AuthorizeTokenHandler> _logger;
2020-12-20 03:06:43 +02:00
2020-12-21 01:28:22 +02:00
public AuthorizeTokenHandler(IUserService userService, IMapper mapper, ILogger<AuthorizeTokenHandler> logger)
2020-12-20 03:06:43 +02:00
{
_userService = userService;
_mapper = mapper;
2020-12-21 01:28:22 +02:00
_logger = logger;
2020-12-20 03:06:43 +02:00
}
public Task<TokenCore> Handle(AuthorizeToken command, CancellationToken cancellationToken)
2020-12-20 03:06:43 +02:00
{
var tokenCore = _userService.Authorize(command.Token);
if (tokenCore == null)
2020-12-21 01:28:22 +02:00
{
_logger.LogDebug($"Authorization failed for token '{command.Token}'.");
2020-12-20 03:06:43 +02:00
return null;
2020-12-21 01:28:22 +02:00
}
2020-12-20 03:06:43 +02:00
2020-12-21 01:28:22 +02:00
_logger.LogDebug($"Authorization succeeded for token '{command.Token}'.");
var tokenCoreResult = _mapper.Map<TokenCore>(tokenCore);
return Task.FromResult(tokenCoreResult);
2020-12-20 03:06:43 +02:00
}
}
}