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, User>
|
|
|
|
|
{
|
|
|
|
|
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 async Task<User> Handle(AuthorizeToken command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var appUser = await _userService.Authorize(command.Token);
|
|
|
|
|
if (appUser == 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}'.");
|
2020-12-20 03:06:43 +02:00
|
|
|
|
var user = _mapper.Map<User>(appUser);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|