2020-06-06 00:14:42 -04:00
|
|
|
|
using System;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-28 21:56:10 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using BirdsiteLive.ActivityPub;
|
2021-01-09 22:26:17 -05:00
|
|
|
|
using BirdsiteLive.ActivityPub.Converters;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using BirdsiteLive.Cryptography;
|
2020-07-07 21:03:20 -04:00
|
|
|
|
using BirdsiteLive.Domain.BusinessUseCases;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
using BirdsiteLive.Twitter.Models;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
using Tweetinvi.Core.Exceptions;
|
2020-07-01 22:45:43 -04:00
|
|
|
|
using Tweetinvi.Models;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Domain
|
|
|
|
|
{
|
|
|
|
|
public interface IUserService
|
|
|
|
|
{
|
|
|
|
|
Actor GetUser(TwitterUser twitterUser);
|
2020-12-28 00:43:02 -05:00
|
|
|
|
Task<bool> FollowRequestedAsync(string signature, string method, string path, string queryString, Dictionary<string, string> requestHeaders, ActivityFollow activity, string body);
|
|
|
|
|
Task<bool> UndoFollowRequestedAsync(string signature, string method, string path, string queryString, Dictionary<string, string> requestHeaders, ActivityUndoFollow activity, string body);
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UserService : IUserService
|
|
|
|
|
{
|
2020-07-07 21:03:20 -04:00
|
|
|
|
private readonly IProcessFollowUser _processFollowUser;
|
2020-07-08 19:50:58 -04:00
|
|
|
|
private readonly IProcessUndoFollowUser _processUndoFollowUser;
|
2020-07-07 21:03:20 -04:00
|
|
|
|
|
2020-07-22 19:49:08 -04:00
|
|
|
|
private readonly InstanceSettings _instanceSettings;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
private readonly ICryptoService _cryptoService;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
private readonly IActivityPubService _activityPubService;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
|
|
|
|
|
#region Ctor
|
2020-07-08 19:50:58 -04:00
|
|
|
|
public UserService(InstanceSettings instanceSettings, ICryptoService cryptoService, IActivityPubService activityPubService, IProcessFollowUser processFollowUser, IProcessUndoFollowUser processUndoFollowUser)
|
2020-06-06 00:14:42 -04:00
|
|
|
|
{
|
2020-07-22 19:49:08 -04:00
|
|
|
|
_instanceSettings = instanceSettings;
|
2020-06-06 00:14:42 -04:00
|
|
|
|
_cryptoService = cryptoService;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
_activityPubService = activityPubService;
|
2020-07-07 21:03:20 -04:00
|
|
|
|
_processFollowUser = processFollowUser;
|
2020-07-08 19:50:58 -04:00
|
|
|
|
_processUndoFollowUser = processUndoFollowUser;
|
2020-07-22 19:49:08 -04:00
|
|
|
|
//_host = $"https://{instanceSettings.Domain.Replace("https://",string.Empty).Replace("http://", string.Empty).TrimEnd('/')}";
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public Actor GetUser(TwitterUser twitterUser)
|
|
|
|
|
{
|
2021-01-09 22:26:17 -05:00
|
|
|
|
var actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, twitterUser.Acct);
|
|
|
|
|
var acct = twitterUser.Acct.ToLowerInvariant();
|
|
|
|
|
|
2020-06-06 00:14:42 -04:00
|
|
|
|
var user = new Actor
|
|
|
|
|
{
|
2021-01-09 22:26:17 -05:00
|
|
|
|
id = actorUrl,
|
|
|
|
|
type = "Service",
|
|
|
|
|
followers = $"{actorUrl}/followers",
|
|
|
|
|
preferredUsername = acct,
|
2020-06-06 00:14:42 -04:00
|
|
|
|
name = twitterUser.Name,
|
2021-01-09 22:26:17 -05:00
|
|
|
|
inbox = $"{actorUrl}/inbox",
|
2020-06-06 00:14:42 -04:00
|
|
|
|
summary = twitterUser.Description,
|
2021-01-09 22:26:17 -05:00
|
|
|
|
url = actorUrl,
|
2020-06-06 00:14:42 -04:00
|
|
|
|
publicKey = new PublicKey()
|
|
|
|
|
{
|
2021-01-09 22:26:17 -05:00
|
|
|
|
id = $"{actorUrl}#main-key",
|
|
|
|
|
owner = actorUrl,
|
|
|
|
|
publicKeyPem = _cryptoService.GetUserPem(acct)
|
2020-06-06 00:14:42 -04:00
|
|
|
|
},
|
|
|
|
|
icon = new Image
|
|
|
|
|
{
|
|
|
|
|
mediaType = "image/jpeg",
|
|
|
|
|
url = twitterUser.ProfileImageUrl
|
|
|
|
|
},
|
|
|
|
|
image = new Image
|
|
|
|
|
{
|
|
|
|
|
mediaType = "image/jpeg",
|
|
|
|
|
url = twitterUser.ProfileBannerURL
|
2020-07-22 19:49:08 -04:00
|
|
|
|
},
|
|
|
|
|
endpoints = new EndPoints
|
|
|
|
|
{
|
2020-07-22 23:49:57 -04:00
|
|
|
|
sharedInbox = $"https://{_instanceSettings.Domain}/inbox"
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return user;
|
|
|
|
|
}
|
2020-06-06 01:29:13 -04:00
|
|
|
|
|
2020-12-28 00:43:02 -05:00
|
|
|
|
public async Task<bool> FollowRequestedAsync(string signature, string method, string path, string queryString, Dictionary<string, string> requestHeaders, ActivityFollow activity, string body)
|
2020-06-06 01:29:13 -04:00
|
|
|
|
{
|
|
|
|
|
// Validate
|
2020-12-28 00:43:02 -05:00
|
|
|
|
var sigValidation = await ValidateSignature(activity.actor, signature, method, path, queryString, requestHeaders, body);
|
2020-07-07 21:03:20 -04:00
|
|
|
|
if (!sigValidation.SignatureIsValidated) return false;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
|
|
|
|
|
// Save Follow in DB
|
2020-11-20 18:59:57 -05:00
|
|
|
|
var followerUserName = sigValidation.User.preferredUsername.ToLowerInvariant();
|
2020-07-07 21:03:20 -04:00
|
|
|
|
var followerHost = sigValidation.User.url.Replace("https://", string.Empty).Split('/').First();
|
|
|
|
|
var followerInbox = sigValidation.User.inbox;
|
2020-08-10 20:04:12 -04:00
|
|
|
|
var followerSharedInbox = sigValidation.User?.endpoints?.sharedInbox;
|
2020-07-07 21:03:20 -04:00
|
|
|
|
var twitterUser = activity.apObject.Split('/').Last().Replace("@", string.Empty);
|
2020-08-10 20:04:12 -04:00
|
|
|
|
|
|
|
|
|
// Make sure to only keep routes
|
|
|
|
|
followerInbox = OnlyKeepRoute(followerInbox, followerHost);
|
|
|
|
|
followerSharedInbox = OnlyKeepRoute(followerSharedInbox, followerHost);
|
|
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
|
await _processFollowUser.ExecuteAsync(followerUserName, followerHost, twitterUser, followerInbox, followerSharedInbox);
|
2020-07-07 21:03:20 -04:00
|
|
|
|
|
|
|
|
|
// Send Accept Activity
|
2020-06-28 21:56:10 -04:00
|
|
|
|
var acceptFollow = new ActivityAcceptFollow()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-07-07 21:03:20 -04:00
|
|
|
|
var result = await _activityPubService.PostDataAsync(acceptFollow, followerHost, activity.apObject);
|
2021-01-07 19:37:09 -05:00
|
|
|
|
return result == HttpStatusCode.Accepted || result == HttpStatusCode.OK;
|
2020-06-06 01:29:13 -04:00
|
|
|
|
}
|
2020-07-08 19:50:58 -04:00
|
|
|
|
|
2020-08-10 20:04:12 -04:00
|
|
|
|
private string OnlyKeepRoute(string inbox, string host)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(inbox))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (inbox.Contains(host))
|
|
|
|
|
inbox = inbox.Split(new[] { host }, StringSplitOptions.RemoveEmptyEntries).Last();
|
|
|
|
|
|
|
|
|
|
return inbox;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 19:50:58 -04:00
|
|
|
|
public async Task<bool> UndoFollowRequestedAsync(string signature, string method, string path, string queryString,
|
2020-12-28 00:43:02 -05:00
|
|
|
|
Dictionary<string, string> requestHeaders, ActivityUndoFollow activity, string body)
|
2020-07-08 19:50:58 -04:00
|
|
|
|
{
|
|
|
|
|
// Validate
|
2020-12-28 00:43:02 -05:00
|
|
|
|
var sigValidation = await ValidateSignature(activity.actor, signature, method, path, queryString, requestHeaders, body);
|
2020-07-08 19:50:58 -04:00
|
|
|
|
if (!sigValidation.SignatureIsValidated) return false;
|
|
|
|
|
|
|
|
|
|
// Save Follow in DB
|
2021-01-04 13:49:07 -05:00
|
|
|
|
var followerUserName = sigValidation.User.preferredUsername.ToLowerInvariant();
|
2020-07-08 19:50:58 -04:00
|
|
|
|
var followerHost = sigValidation.User.url.Replace("https://", string.Empty).Split('/').First();
|
|
|
|
|
//var followerInbox = sigValidation.User.inbox;
|
|
|
|
|
var twitterUser = activity.apObject.apObject.Split('/').Last().Replace("@", string.Empty);
|
|
|
|
|
await _processUndoFollowUser.ExecuteAsync(followerUserName, followerHost, twitterUser);
|
|
|
|
|
|
|
|
|
|
// Send Accept Activity
|
|
|
|
|
var acceptFollow = new ActivityAcceptUndoFollow()
|
|
|
|
|
{
|
|
|
|
|
context = "https://www.w3.org/ns/activitystreams",
|
|
|
|
|
id = $"{activity.apObject.apObject}#accepts/undofollows/{Guid.NewGuid()}",
|
|
|
|
|
type = "Accept",
|
|
|
|
|
actor = activity.apObject.apObject,
|
|
|
|
|
apObject = new ActivityUndoFollow()
|
|
|
|
|
{
|
|
|
|
|
id = activity.id,
|
|
|
|
|
type = activity.type,
|
|
|
|
|
actor = activity.actor,
|
|
|
|
|
apObject = activity.apObject
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var result = await _activityPubService.PostDataAsync(acceptFollow, followerHost, activity.apObject.apObject);
|
2021-01-07 19:37:09 -05:00
|
|
|
|
return result == HttpStatusCode.Accepted || result == HttpStatusCode.OK;
|
2020-07-08 19:50:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 00:43:02 -05:00
|
|
|
|
private async Task<SignatureValidationResult> ValidateSignature(string actor, string rawSig, string method, string path, string queryString, Dictionary<string, string> requestHeaders, string body)
|
2020-06-06 01:29:13 -04:00
|
|
|
|
{
|
2020-12-03 02:37:03 -05:00
|
|
|
|
//Check Date Validity
|
|
|
|
|
var date = requestHeaders["date"];
|
|
|
|
|
var d = DateTime.Parse(date).ToUniversalTime();
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var delta = Math.Abs((d - now).TotalSeconds);
|
|
|
|
|
if (delta > 30) return new SignatureValidationResult { SignatureIsValidated = false };
|
|
|
|
|
|
2020-12-28 00:43:02 -05:00
|
|
|
|
//Check Digest
|
|
|
|
|
var digest = requestHeaders["digest"];
|
|
|
|
|
var digestHash = digest.Split(new [] {"SHA-256="},StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
|
|
|
|
|
var calculatedDigestHash = _cryptoService.ComputeSha256Hash(body);
|
|
|
|
|
if (digestHash != calculatedDigestHash) return new SignatureValidationResult { SignatureIsValidated = false };
|
|
|
|
|
|
2020-12-03 02:37:03 -05:00
|
|
|
|
//Check Signature
|
2020-06-06 01:29:13 -04:00
|
|
|
|
var signatures = rawSig.Split(',');
|
|
|
|
|
var signature_header = new Dictionary<string, string>();
|
|
|
|
|
foreach (var signature in signatures)
|
|
|
|
|
{
|
|
|
|
|
var splitSig = signature.Replace("\"", string.Empty).Split('=');
|
|
|
|
|
signature_header.Add(splitSig[0], splitSig[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signature_header["signature"] = signature_header["signature"] + "==";
|
|
|
|
|
|
|
|
|
|
var key_id = signature_header["keyId"];
|
|
|
|
|
var headers = signature_header["headers"];
|
|
|
|
|
var algorithm = signature_header["algorithm"];
|
|
|
|
|
var sig = Convert.FromBase64String(signature_header["signature"]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var remoteUser = await _activityPubService.GetUser(actor);
|
|
|
|
|
|
|
|
|
|
var toDecode = remoteUser.publicKey.publicKeyPem.Trim().Remove(0, remoteUser.publicKey.publicKeyPem.IndexOf('\n'));
|
|
|
|
|
toDecode = toDecode.Remove(toDecode.LastIndexOf('\n')).Replace("\n", "");
|
|
|
|
|
var signKey = ASN1.ToRSA(Convert.FromBase64String(toDecode));
|
|
|
|
|
|
|
|
|
|
var toSign = new StringBuilder();
|
|
|
|
|
//var comparisonString = headers.Split(' ').Select(signed_header_name =>
|
|
|
|
|
//{
|
|
|
|
|
// if (signed_header_name == "(request-target)")
|
|
|
|
|
// return "(request-target): post /inbox";
|
|
|
|
|
// else
|
|
|
|
|
// return $"{signed_header_name}: {r.Headers[signed_header_name.ToUpperInvariant()]}";
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
foreach (var headerKey in headers.Split(' '))
|
|
|
|
|
{
|
|
|
|
|
if (headerKey == "(request-target)") toSign.Append($"(request-target): {method.ToLower()} {path}{queryString}\n");
|
|
|
|
|
else toSign.Append($"{headerKey}: {string.Join(", ", requestHeaders[headerKey])}\n");
|
|
|
|
|
}
|
|
|
|
|
toSign.Remove(toSign.Length - 1, 1);
|
|
|
|
|
|
|
|
|
|
//var signKey = ASN1.ToRSA(Convert.FromBase64String(toDecode));
|
|
|
|
|
|
|
|
|
|
//new RSACryptoServiceProvider(keyId.publicKey.publicKeyPem);
|
|
|
|
|
|
|
|
|
|
//Create a new instance of RSACryptoServiceProvider.
|
|
|
|
|
RSACryptoServiceProvider key = new RSACryptoServiceProvider();
|
|
|
|
|
|
|
|
|
|
//Get an instance of RSAParameters from ExportParameters function.
|
|
|
|
|
RSAParameters RSAKeyInfo = key.ExportParameters(false);
|
|
|
|
|
|
|
|
|
|
//Set RSAKeyInfo to the public key values.
|
|
|
|
|
RSAKeyInfo.Modulus = Convert.FromBase64String(toDecode);
|
|
|
|
|
|
|
|
|
|
key.ImportParameters(RSAKeyInfo);
|
|
|
|
|
|
|
|
|
|
var result = signKey.VerifyData(Encoding.UTF8.GetBytes(toSign.ToString()), sig, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
|
|
|
|
|
2020-07-07 21:03:20 -04:00
|
|
|
|
return new SignatureValidationResult()
|
|
|
|
|
{
|
|
|
|
|
SignatureIsValidated = result,
|
|
|
|
|
User = remoteUser
|
|
|
|
|
};
|
2020-06-06 01:29:13 -04:00
|
|
|
|
}
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|
2020-07-07 21:03:20 -04:00
|
|
|
|
|
|
|
|
|
public class SignatureValidationResult
|
|
|
|
|
{
|
|
|
|
|
public bool SignatureIsValidated { get; set; }
|
|
|
|
|
public Actor User { get; set; }
|
|
|
|
|
}
|
2020-06-06 00:14:42 -04:00
|
|
|
|
}
|