validation of the fediverse user

This commit is contained in:
Nicolas Constant
2022-11-02 01:15:05 -04:00
parent ec3234324c
commit 15f0ad55ae
2 changed files with 41 additions and 2 deletions

View File

@@ -16,12 +16,19 @@ namespace BirdsiteLive.Domain
{ {
public interface IActivityPubService public interface IActivityPubService
{ {
Task<string> GetUserIdAsync(string acct);
Task<Actor> GetUser(string objectId); Task<Actor> GetUser(string objectId);
Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl, string inbox = null); Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl, string inbox = null);
Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost,
string targetInbox); string targetInbox);
} }
public class WebFinger
{
public string subject { get; set; }
public string[] aliases { get; set; }
}
public class ActivityPubService : IActivityPubService public class ActivityPubService : IActivityPubService
{ {
private readonly InstanceSettings _instanceSettings; private readonly InstanceSettings _instanceSettings;
@@ -39,6 +46,24 @@ namespace BirdsiteLive.Domain
} }
#endregion #endregion
public async Task<string> GetUserIdAsync(string acct)
{
var splittedAcct = acct.Trim('@').Split('@');
var url = $"https://{splittedAcct[1]}/.well-known/webfinger?resource=acct:{splittedAcct[0]}@{splittedAcct[1]}";
var httpClient = _httpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
var result = await httpClient.GetAsync(url);
result.EnsureSuccessStatusCode();
var content = await result.Content.ReadAsStringAsync();
var actor = JsonConvert.DeserializeObject<WebFinger>(content);
return actor.aliases.FirstOrDefault();
}
public async Task<Actor> GetUser(string objectId) public async Task<Actor> GetUser(string objectId)
{ {
var httpClient = _httpClientFactory.CreateClient(); var httpClient = _httpClientFactory.CreateClient();

View File

@@ -10,11 +10,13 @@ namespace BirdsiteLive.Domain
public class MigrationService public class MigrationService
{ {
private readonly ITwitterTweetsService _twitterTweetsService; private readonly ITwitterTweetsService _twitterTweetsService;
private readonly IActivityPubService _activityPubService;
#region Ctor #region Ctor
public MigrationService(ITwitterTweetsService twitterTweetsService) public MigrationService(ITwitterTweetsService twitterTweetsService, IActivityPubService activityPubService)
{ {
_twitterTweetsService = twitterTweetsService; _twitterTweetsService = twitterTweetsService;
_activityPubService = activityPubService;
} }
#endregion #endregion
@@ -52,11 +54,23 @@ namespace BirdsiteLive.Domain
public async Task<bool> ValidateFediverseAcctAsync(string fediverseAcct) public async Task<bool> ValidateFediverseAcctAsync(string fediverseAcct)
{ {
return true; if (string.IsNullOrWhiteSpace(fediverseAcct))
throw new ArgumentException("Please provide Fediverse account");
if( !fediverseAcct.Contains('@') || fediverseAcct.Trim('@').Split('@').Length != 2)
throw new ArgumentException("Please provide valid Fediverse handle");
var objectId = await _activityPubService.GetUserIdAsync(fediverseAcct);
var user = await _activityPubService.GetUser(objectId);
if(user != null) return true;
return false;
} }
public async Task MigrateAccountAsync(string acct, string tweetId, string fediverseAcct, bool triggerRemoteMigration) public async Task MigrateAccountAsync(string acct, string tweetId, string fediverseAcct, bool triggerRemoteMigration)
{ {
throw new NotImplementedException("Migration not implemented");
} }
private byte[] GetHash(string inputString) private byte[] GetHash(string inputString)