40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using AutoMapper;
|
|
using IdentityServer.Application.Commands;
|
|
using IdentityServer.Application.Services;
|
|
using IdentityServer.PublishedLanguage.Dto;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
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;
|
|
private readonly ILogger<AuthorizeTokenHandler> _logger;
|
|
|
|
public AuthorizeTokenHandler(IUserService userService, IMapper mapper, ILogger<AuthorizeTokenHandler> logger)
|
|
{
|
|
_userService = userService;
|
|
_mapper = mapper;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<User> Handle(AuthorizeToken command, CancellationToken cancellationToken)
|
|
{
|
|
var appUser = await _userService.Authorize(command.Token);
|
|
if (appUser == null)
|
|
{
|
|
_logger.LogDebug($"Authorization failed for token '{command.Token}'.");
|
|
return null;
|
|
}
|
|
|
|
_logger.LogDebug($"Authorization succeeded for token '{command.Token}'.");
|
|
var user = _mapper.Map<User>(appUser);
|
|
return user;
|
|
}
|
|
}
|
|
}
|