Initial commit.
This commit is contained in:
29
Wino.Core/Http/GmailClientMessageHandler.cs
Normal file
29
Wino.Core/Http/GmailClientMessageHandler.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Google.Apis.Http;
|
||||
using Wino.Core.Domain.Entities;
|
||||
|
||||
namespace Wino.Core.Http
|
||||
{
|
||||
internal class GmailClientMessageHandler : ConfigurableMessageHandler
|
||||
{
|
||||
public Func<Task<TokenInformation>> TokenRetrieveDelegate { get; }
|
||||
|
||||
public GmailClientMessageHandler(Func<Task<TokenInformation>> tokenRetrieveDelegate) : base(new HttpClientHandler())
|
||||
{
|
||||
TokenRetrieveDelegate = tokenRetrieveDelegate;
|
||||
}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tokenizationTask = TokenRetrieveDelegate.Invoke();
|
||||
var tokenInformation = await tokenizationTask;
|
||||
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenInformation.AccessToken);
|
||||
|
||||
return await base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Wino.Core/Http/MicrosoftImmutableIdHandler.cs
Normal file
19
Wino.Core/Http/MicrosoftImmutableIdHandler.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Wino.Core.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds additional Prefer header for immutable id support in the Graph service client.
|
||||
/// </summary>
|
||||
public class MicrosoftImmutableIdHandler : DelegatingHandler
|
||||
{
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("Prefer", "IdType=\"ImmutableId\"");
|
||||
|
||||
return base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Wino.Core/Http/MicrosoftJsonContractResolver.cs
Normal file
36
Wino.Core/Http/MicrosoftJsonContractResolver.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Wino.Core.Http
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// We need to generate HttpRequestMessage for batch requests, and sometimes we need to
|
||||
/// serialize content as json. However, some of the fields like 'ODataType' must be ignored
|
||||
/// in order PATCH requests to succeed. Therefore Microsoft account synchronizer uses
|
||||
/// special JsonSerializerSettings for ignoring some of the properties.
|
||||
/// </summary>
|
||||
public class MicrosoftJsonContractResolver : DefaultContractResolver
|
||||
{
|
||||
private readonly HashSet<string> ignoreProps = new HashSet<string>()
|
||||
{
|
||||
"ODataType"
|
||||
};
|
||||
|
||||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||
{
|
||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
if (ignoreProps.Contains(property.PropertyName))
|
||||
{
|
||||
property.ShouldSerialize = _ => false;
|
||||
}
|
||||
|
||||
return property;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Wino.Core/Http/MicrosoftTokenProvider.cs
Normal file
33
Wino.Core/Http/MicrosoftTokenProvider.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Kiota.Abstractions.Authentication;
|
||||
using Wino.Core.Domain.Entities;
|
||||
using Wino.Core.Domain.Interfaces;
|
||||
|
||||
namespace Wino.Core.Http
|
||||
{
|
||||
public class MicrosoftTokenProvider : IAccessTokenProvider
|
||||
{
|
||||
private readonly MailAccount _account;
|
||||
private readonly IAuthenticator _authenticator;
|
||||
|
||||
public MicrosoftTokenProvider(MailAccount account, IAuthenticator authenticator)
|
||||
{
|
||||
_account = account;
|
||||
_authenticator = authenticator;
|
||||
}
|
||||
|
||||
public AllowedHostsValidator AllowedHostsValidator { get; }
|
||||
|
||||
public async Task<string> GetAuthorizationTokenAsync(Uri uri,
|
||||
Dictionary<string, object> additionalAuthenticationContext = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var token = await _authenticator.GetTokenAsync(_account).ConfigureAwait(false);
|
||||
|
||||
return token?.AccessToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user