following working

This commit is contained in:
Nicolas Constant 2020-06-28 21:56:10 -04:00
parent f5fe4f53f9
commit 3b08f75204
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
3 changed files with 62 additions and 6 deletions

View file

@ -1,17 +1,31 @@
using System.Net.Http; using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BirdsiteLive.ActivityPub; using BirdsiteLive.ActivityPub;
using Newtonsoft.Json; using Newtonsoft.Json;
using Org.BouncyCastle.Bcpg;
namespace BirdsiteLive.Domain namespace BirdsiteLive.Domain
{ {
public interface IActivityPubService public interface IActivityPubService
{ {
Task<Actor> GetUser(string objectId); Task<Actor> GetUser(string objectId);
Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl);
} }
public class ActivityPubService : IActivityPubService public class ActivityPubService : IActivityPubService
{ {
private readonly ICryptoService _cryptoService;
#region Ctor
public ActivityPubService(ICryptoService cryptoService)
{
_cryptoService = cryptoService;
}
#endregion
public async Task<Actor> GetUser(string objectId) public async Task<Actor> GetUser(string objectId)
{ {
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
@ -22,5 +36,31 @@ namespace BirdsiteLive.Domain
return JsonConvert.DeserializeObject<Actor>(content); return JsonConvert.DeserializeObject<Actor>(content);
} }
} }
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;
}
} }
} }

View file

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -71,9 +73,23 @@ namespace BirdsiteLive.Domain
// Save Follow in DB // Save Follow in DB
// Send Accept Activity // Send Accept Activity
var targetHost = activity.actor.Replace("https://", string.Empty).Split('/').First();
var acceptFollow = new ActivityAcceptFollow()
throw new NotImplementedException(); {
context = "https://www.w3.org/ns/activitystreams",
id = $"{activity.apObject}#accepts/follows/{Guid.NewGuid()}",
type = "Accept",
actor = activity.apObject,
apObject = new ActivityFollow()
{
id = activity.id,
type = activity.type,
actor = activity.actor,
apObject = activity.apObject
}
};
var result = await _activityPubService.PostDataAsync(acceptFollow, targetHost, activity.apObject);
return result == HttpStatusCode.Accepted;
} }
private async Task<bool> ValidateSignature(string actor, string rawSig, string method, string path, string queryString, Dictionary<string, string> requestHeaders) private async Task<bool> ValidateSignature(string actor, string rawSig, string method, string path, string queryString, Dictionary<string, string> requestHeaders)

View file

@ -60,7 +60,7 @@ namespace BirdsiteLive.Controllers
{ {
case "Follow": case "Follow":
var succeeded = await _userService.FollowRequestedAsync(r.Headers["Signature"].First(), r.Method, r.Path, r.QueryString.ToString(), RequestHeaders(r.Headers), activity as ActivityFollow); var succeeded = await _userService.FollowRequestedAsync(r.Headers["Signature"].First(), r.Method, r.Path, r.QueryString.ToString(), RequestHeaders(r.Headers), activity as ActivityFollow);
if (succeeded) return Ok(); if (succeeded) return Accepted();
else return Unauthorized(); else return Unauthorized();
break; break;
default: default: