2020-03-24 08:47:08 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using IdentityModel;
|
|
|
|
|
|
|
|
namespace FarmmapsApi.HttpMessageHandlers
|
|
|
|
{
|
|
|
|
public class FarmmapsAuthenticationHandler : DelegatingHandler
|
|
|
|
{
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-03-25 20:32:28 +00:00
|
|
|
if (!OidcConstants.AuthenticationSchemes.AuthorizationHeaderBearer.Equals(request.Headers?.Authorization.Scheme) ||
|
|
|
|
string.IsNullOrEmpty(request.Headers?.Authorization.Parameter))
|
2020-03-24 08:47:08 +00:00
|
|
|
{
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.Unauthorized)
|
|
|
|
{
|
2020-03-25 20:32:28 +00:00
|
|
|
Content = new StringContent("You must authenticated before using the API")
|
2020-03-24 08:47:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return await base.SendAsync(request, cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|