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
|
|
|
|
|
{
|
2021-11-12 01:37:10 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 01:37:10 +02:00
|
|
|
|
public Task<TokenCore> Handle(AuthorizeToken command, CancellationToken cancellationToken)
|
2020-12-20 03:06:43 +02:00
|
|
|
|
{
|
2021-11-12 01:37:10 +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}'.");
|
2021-11-12 01:37:10 +02:00
|
|
|
|
var tokenCoreResult = _mapper.Map<TokenCore>(tokenCore);
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(tokenCoreResult);
|
2020-12-20 03:06:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|