2020-06-28 21:56:10 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BirdsiteLive.ActivityPub;
|
|
|
|
|
using Newtonsoft.Json;
|
2020-06-28 21:56:10 -04:00
|
|
|
|
using Org.BouncyCastle.Bcpg;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Domain
|
|
|
|
|
{
|
|
|
|
|
public interface IActivityPubService
|
|
|
|
|
{
|
|
|
|
|
Task<Actor> GetUser(string objectId);
|
2020-06-28 21:56:10 -04:00
|
|
|
|
Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl);
|
2020-06-06 01:29:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ActivityPubService : IActivityPubService
|
|
|
|
|
{
|
2020-06-28 21:56:10 -04:00
|
|
|
|
private readonly ICryptoService _cryptoService;
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
|
public ActivityPubService(ICryptoService cryptoService)
|
|
|
|
|
{
|
|
|
|
|
_cryptoService = cryptoService;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-06-06 01:29:13 -04:00
|
|
|
|
public async Task<Actor> GetUser(string objectId)
|
|
|
|
|
{
|
|
|
|
|
using (var httpClient = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
|
|
|
var result = await httpClient.GetAsync(objectId);
|
|
|
|
|
var content = await result.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<Actor>(content);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-28 21:56:10 -04:00
|
|
|
|
|
|
|
|
|
public async Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonConvert.SerializeObject(data);
|
|
|
|
|
|
|
|
|
|
var date = DateTime.UtcNow.ToUniversalTime();
|
|
|
|
|
var httpDate = date.ToString("r");
|
|
|
|
|
var signature = _cryptoService.SignAndGetSignatureHeader(date, actorUrl, targetHost);
|
|
|
|
|
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var httpRequestMessage = new HttpRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Post,
|
|
|
|
|
RequestUri = new Uri($"https://{targetHost}/inbox"),
|
|
|
|
|
Headers =
|
|
|
|
|
{
|
|
|
|
|
{"Host", targetHost},
|
|
|
|
|
{"Date", httpDate},
|
|
|
|
|
{"Signature", signature}
|
|
|
|
|
},
|
|
|
|
|
Content = new StringContent(json, Encoding.UTF8, "application/ld+json")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await client.SendAsync(httpRequestMessage);
|
|
|
|
|
return response.StatusCode;
|
|
|
|
|
}
|
2020-06-06 01:29:13 -04:00
|
|
|
|
}
|
|
|
|
|
}
|