Update
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace BackOffice.BFF.WebApi.Common.Services;
|
||||
|
||||
public class AppTokenProvider : ITokenProvider
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private string? _token;
|
||||
|
||||
public AppTokenProvider(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public async Task<string> GetTokenAsync()
|
||||
{
|
||||
if (_token != null)
|
||||
{
|
||||
return _token;
|
||||
}
|
||||
|
||||
// get token string
|
||||
var authorizationToken = _httpContextAccessor.HttpContext?.Request.Headers[HeaderNames.Authorization];
|
||||
|
||||
if (!string.IsNullOrEmpty(authorizationToken))
|
||||
_token = authorizationToken.ToString().Replace("Bearer ", "");
|
||||
|
||||
return _token;
|
||||
|
||||
// return await Task.FromResult(GetToken()) ?? string.Empty;
|
||||
}
|
||||
|
||||
private string? GetToken()
|
||||
{
|
||||
if (_token != null) return _token;
|
||||
|
||||
// get token string
|
||||
var authorizationToken = _httpContextAccessor.HttpContext?.Request.Headers[HeaderNames.Authorization];
|
||||
if (authorizationToken is null) return _token;
|
||||
|
||||
var token = authorizationToken.Value.ToString();
|
||||
if (string.IsNullOrEmpty(token)) return _token;
|
||||
|
||||
_token = token;
|
||||
|
||||
return _token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Security.Claims;
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace BackOffice.BFF.WebApi.Common.Services;
|
||||
|
||||
public class CurrentUserService : ICurrentUserService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public CurrentUserService(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public string? UserId => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
namespace BackOffice.BFF.WebApi.Common.Services;
|
||||
public interface IDispatchRequestToCQRS
|
||||
{
|
||||
Task<TResponse> Handle<TRequest, TCommand, TResponse>(TRequest request,
|
||||
ServerCallContext context);
|
||||
Task<Empty> Handle<TRequest, TCommand>(TRequest request,
|
||||
ServerCallContext context);
|
||||
Task<TResponse> Handle<TCommand, TResponse>(ServerCallContext context);
|
||||
}
|
||||
public class DispatchRequestToCQRS : IDispatchRequestToCQRS
|
||||
{
|
||||
private readonly ISender _sender;
|
||||
|
||||
public DispatchRequestToCQRS(ISender sender)
|
||||
{
|
||||
_sender = sender;
|
||||
}
|
||||
|
||||
public async Task<TResponse> Handle<TRequest, TCommand, TResponse>(TRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (request is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
var cqrsInput = request.Adapt<TCommand>();
|
||||
|
||||
if (cqrsInput is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cqrsInput));
|
||||
}
|
||||
|
||||
var output = await _sender.Send(cqrsInput, context.CancellationToken);
|
||||
return (output ?? throw new InvalidOperationException()).Adapt<TResponse>();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public async Task<TResponse> Handle<TCommand, TResponse>(ServerCallContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cqrsInput = Activator.CreateInstance<TCommand>();
|
||||
if (cqrsInput is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cqrsInput));
|
||||
}
|
||||
|
||||
var output = await _sender.Send(cqrsInput, context.CancellationToken);
|
||||
return (output ?? throw new InvalidOperationException()).Adapt<TResponse>();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public async Task<Empty> Handle<TRequest, TCommand>(TRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (request is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
var cqrsInput = request.Adapt<TCommand>();
|
||||
|
||||
if (cqrsInput is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cqrsInput));
|
||||
}
|
||||
|
||||
await _sender.Send(cqrsInput, context.CancellationToken);
|
||||
return new Empty();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user