created parametrable webfinger

This commit is contained in:
Nicolas Constant 2020-03-21 17:11:35 -04:00
parent 6be59fc867
commit 2bbcb3b36b
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
6 changed files with 80 additions and 12 deletions

2
.gitignore vendored
View file

@ -3,6 +3,8 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
appsettings.*.json
# User-specific files
*.rsuser
*.suo

View file

@ -2,44 +2,86 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace BirdsiteLive.Controllers
{
[ApiController]
public class WellKnownController : ControllerBase
{
#region Ctor
public WellKnownController()
{
private readonly InstanceSettings _settings;
#region Ctor
public WellKnownController(IOptions<InstanceSettings> settings)
{
_settings = settings.Value;
}
#endregion
[Route("/.well-known/webfinger")]
public WebFingerResult Webfinger(string resource = null)
public IActionResult Webfinger(string resource = null)
{
var acct = resource.Split("acct:")[1];
var acct = resource.Split("acct:")[1].Trim();
string name = null;
string domain = null;
return new WebFingerResult()
var splitAcct = acct.Split('@', StringSplitOptions.RemoveEmptyEntries);
var atCount = acct.Count(x => x == '@');
if (atCount == 1 && acct.StartsWith('@'))
{
subject = $"acct:{acct}",
name = splitAcct[1];
}
else if (atCount == 1 || atCount == 2)
{
name = splitAcct[0];
domain = splitAcct[1];
}
else
{
return BadRequest();
}
if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain)
return NotFound();
//TODO: check if twitter user exists
var result = new WebFingerResult()
{
subject = $"acct:{name}@{_settings.Domain}",
aliases = new []
{
$"https://{_settings.Domain}/@{name}",
$"https://{_settings.Domain}/users/{name}"
},
links = new List<WebFingerLink>
{
new WebFingerLink()
{
rel = "http://webfinger.net/rel/profile-page",
type = "text/html",
href = $"https://{_settings.Domain}/@{name}"
},
new WebFingerLink()
{
rel = "self",
type = "application/activity+json",
href = "https://d150a079.ngrok.io/actor"
href = $"https://{_settings.Domain}/users/{name}"
}
}
};
return new JsonResult(result);
}
public class WebFingerResult
{
public string subject { get; set; }
public string[] aliases { get; set; }
public List<WebFingerLink> links { get; set; } = new List<WebFingerLink>();
}

View file

@ -0,0 +1,7 @@
namespace BirdsiteLive.Models
{
public class InstanceSettings
{
public string Domain { get; set; }
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
@ -13,9 +14,17 @@ namespace BirdsiteLive
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup(IWebHostEnvironment env)
{
Configuration = configuration;
Console.WriteLine($"EnvironmentName {env.EnvironmentName}");
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName.ToLowerInvariant()}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment()) builder.AddUserSecrets<Startup>();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
@ -23,6 +32,8 @@ namespace BirdsiteLive
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<InstanceSettings>(Configuration.GetSection("Instance"));
services.AddControllersWithViews();
}
@ -39,7 +50,7 @@ namespace BirdsiteLive
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

View file

@ -5,5 +5,8 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Instance": {
"Domain": "domain.name"
}
}

View file

@ -6,5 +6,8 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Instance": {
"Domain": "domain.name"
}
}