diff --git a/starsky/starsky.foundation.accountmanagement/Middleware/BasicAuthenticationMiddleware.cs b/starsky/starsky.foundation.accountmanagement/Middleware/BasicAuthenticationMiddleware.cs index d6ea755c6f..c62e8b3eb2 100644 --- a/starsky/starsky.foundation.accountmanagement/Middleware/BasicAuthenticationMiddleware.cs +++ b/starsky/starsky.foundation.accountmanagement/Middleware/BasicAuthenticationMiddleware.cs @@ -22,22 +22,33 @@ public BasicAuthenticationMiddleware(RequestDelegate next) public async Task Invoke(HttpContext context) { - if (!context.User.Identity.IsAuthenticated) - { - var basicAuthenticationHeader = GetBasicAuthenticationHeaderValue(context); - if (basicAuthenticationHeader.IsValidBasicAuthenticationHeaderValue) - { - - var userManager = (IUserManager) context.RequestServices.GetService(typeof(IUserManager)); - - var authenticationManager = new BasicAuthenticationSignInManager( - context, basicAuthenticationHeader, userManager); - await authenticationManager.TrySignInUser(); - } - } + await Authenticate(context); await _next.Invoke(context); } + public static async Task Authenticate(HttpContext context) + { + if ( context.User.Identity?.IsAuthenticated != false ) + { + return false; + } + var basicAuthenticationHeader = GetBasicAuthenticationHeaderValue(context); + + if ( !basicAuthenticationHeader + .IsValidBasicAuthenticationHeaderValue ) + { + return false; + } + + var userManager = (IUserManager) context.RequestServices.GetService(typeof(IUserManager)); + + var authenticationManager = new BasicAuthenticationSignInManager( + context, basicAuthenticationHeader, userManager); + await authenticationManager.TrySignInUser(); + + return context.User.Identity?.IsAuthenticated == true; + } + private static BasicAuthenticationHeaderValue GetBasicAuthenticationHeaderValue(HttpContext context) { var basicAuthenticationHeader = context.Request.Headers["Authorization"] diff --git a/starsky/starsky/Middleware/TestMiddleware.cs b/starsky/starsky/Middleware/TestMiddleware.cs new file mode 100644 index 0000000000..8111163b66 --- /dev/null +++ b/starsky/starsky/Middleware/TestMiddleware.cs @@ -0,0 +1,67 @@ +using System; +using System.Security.Cryptography; +using System.Threading.Tasks; +using System.Xml; +using Microsoft.AspNetCore.Http; +using starsky.foundation.accountmanagement.Interfaces; +using starsky.foundation.accountmanagement.Middleware; +using starsky.foundation.database.Interfaces; +using starsky.foundation.storage.Services; + +namespace starsky.Middleware; + +public class TestMiddleware +{ + + public TestMiddleware(RequestDelegate next) + { + _next = next; + } + + private readonly RequestDelegate _next; + private readonly IQuery _query; + + public async Task Invoke(HttpContext context) + { + if ( context.Request.Path.Value != "/" ) + { + await _next.Invoke(context); + return; + } + + if ( context.Request.Method.ToLowerInvariant() == "get" || + context.Request.Method.ToLowerInvariant() == "options" || + context.Request.Method.ToLowerInvariant() == "head" ) + { + context.Response.Headers.Add("DAV", "1,2, access-control"); + context.Response.Headers.Add("MS-Author-Via", "DAV"); + + var login = await BasicAuthenticationMiddleware.Authenticate(context); + if ( login ) + { + context.Response.Headers.Add("WWW-Authenticate",$"Basic realm=\"WebDAV\""); + } + context.Response.StatusCode = login == false ? 401 : 200; + + await context.Response.BodyWriter.WriteAsync(Array.Empty()); + return; + } + + if ( context.Request.Method.ToLowerInvariant() == "propfind" && context.Request.ContentLength != 0 && context.Request.ContentType?.Contains("xml") == true ) + { + context.Request.EnableBuffering(); + var bodyAsText = await new System.IO.StreamReader(context.Request.Body).ReadToEndAsync(); + context.Request.Body.Position = 0; + + XmlDocument gpxDoc = new XmlDocument(); + gpxDoc.LoadXml(bodyAsText); + + + Console.WriteLine(); + // + + //await _query.GetAllObjectsAsync("/"); + } + + } +} diff --git a/starsky/starsky/Startup.cs b/starsky/starsky/Startup.cs index 44060de322..8cbdcaf7ae 100644 --- a/starsky/starsky/Startup.cs +++ b/starsky/starsky/Startup.cs @@ -36,6 +36,7 @@ using starsky.foundation.webtelemetry.Helpers; using starsky.foundation.webtelemetry.Processor; using starsky.Helpers; +using starsky.Middleware; namespace starsky { @@ -241,6 +242,8 @@ public void Configure(IApplicationBuilder app, IHostEnvironment env, IHostApplic app.UseHttpsRedirection(); } + app.UseMiddleware(); + // Use the name of the application to use behind a reverse proxy app.UsePathBase( PathHelper.PrefixDbSlash("starsky") );