From 1040959a73a02b3c634dd9542b15a7d506fb21ef Mon Sep 17 00:00:00 2001 From: Dion Date: Fri, 25 Nov 2022 18:10:19 +0100 Subject: [PATCH 1/2] WIP --- .../BasicAuthenticationMiddleware.cs | 37 ++++++---- starsky/starsky/Middleware/TestMiddleware.cs | 68 +++++++++++++++++++ starsky/starsky/Startup.cs | 3 + 3 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 starsky/starsky/Middleware/TestMiddleware.cs 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..41184c1b77 --- /dev/null +++ b/starsky/starsky/Middleware/TestMiddleware.cs @@ -0,0 +1,68 @@ +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.Response.Headers.Add("DAV", "1,2, access-control"); + context.Response.Headers.Add("MS-Author-Via", "DAV"); + context.Response.Headers.Add("WWW-Authenticate",$"WWW-Authenticate: Basic realm=\"server\""); + context.Response.StatusCode = 401; + await context.Response.BodyWriter.WriteAsync(Array.Empty()); + return; + } + + if ( context.Request.Method.ToLowerInvariant() == "head" ) + { + var login = await BasicAuthenticationMiddleware.Authenticate(context); + 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 b402a90093..57b7ce271d 100644 --- a/starsky/starsky/Startup.cs +++ b/starsky/starsky/Startup.cs @@ -37,6 +37,7 @@ using starsky.foundation.webtelemetry.Helpers; using starsky.foundation.webtelemetry.Processor; using starsky.Helpers; +using starsky.Middleware; namespace starsky { @@ -236,6 +237,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") ); From 52231ff26f30d67e1b4f2167bfa778ba68764e32 Mon Sep 17 00:00:00 2001 From: Dion Date: Sat, 26 Nov 2022 17:08:41 +0100 Subject: [PATCH 2/2] WIP --- starsky/starsky/Middleware/TestMiddleware.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/starsky/starsky/Middleware/TestMiddleware.cs b/starsky/starsky/Middleware/TestMiddleware.cs index 41184c1b77..8111163b66 100644 --- a/starsky/starsky/Middleware/TestMiddleware.cs +++ b/starsky/starsky/Middleware/TestMiddleware.cs @@ -29,19 +29,18 @@ public async Task Invoke(HttpContext context) return; } - if ( context.Request.Method.ToLowerInvariant() == "get" || context.Request.Method.ToLowerInvariant() == "options") + 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"); - context.Response.Headers.Add("WWW-Authenticate",$"WWW-Authenticate: Basic realm=\"server\""); - context.Response.StatusCode = 401; - await context.Response.BodyWriter.WriteAsync(Array.Empty()); - return; - } - - if ( context.Request.Method.ToLowerInvariant() == "head" ) - { + 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());