diff --git a/.gitignore b/.gitignore index dfcfd56..dd88ba3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +appsettings.*.json + # User-specific files *.rsuser *.suo diff --git a/src/BirdsiteLive/Controllers/WellKnownController.cs b/src/BirdsiteLive/Controllers/WellKnownController.cs index 455d3da..1dc58ce 100644 --- a/src/BirdsiteLive/Controllers/WellKnownController.cs +++ b/src/BirdsiteLive/Controllers/WellKnownController.cs @@ -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 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 { + 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 links { get; set; } = new List(); } diff --git a/src/BirdsiteLive/Models/InstanceSettings.cs b/src/BirdsiteLive/Models/InstanceSettings.cs new file mode 100644 index 0000000..816d4f3 --- /dev/null +++ b/src/BirdsiteLive/Models/InstanceSettings.cs @@ -0,0 +1,7 @@ +namespace BirdsiteLive.Models +{ + public class InstanceSettings + { + public string Domain { get; set; } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive/Startup.cs b/src/BirdsiteLive/Startup.cs index b55082f..b0841a6 100644 --- a/src/BirdsiteLive/Startup.cs +++ b/src/BirdsiteLive/Startup.cs @@ -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(); + 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(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(); diff --git a/src/BirdsiteLive/appsettings.Development.json b/src/BirdsiteLive/appsettings.Development.json index 8983e0f..bbe791a 100644 --- a/src/BirdsiteLive/appsettings.Development.json +++ b/src/BirdsiteLive/appsettings.Development.json @@ -5,5 +5,8 @@ "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } + }, + "Instance": { + "Domain": "domain.name" } } diff --git a/src/BirdsiteLive/appsettings.json b/src/BirdsiteLive/appsettings.json index d9d9a9b..c75a72b 100644 --- a/src/BirdsiteLive/appsettings.json +++ b/src/BirdsiteLive/appsettings.json @@ -6,5 +6,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Instance": { + "Domain": "domain.name" + } }