Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebDAV alpha unplanned featured #973

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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"]
Expand Down
67 changes: 67 additions & 0 deletions starsky/starsky/Middleware/TestMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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<byte>());
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();
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?><propfind xmlns="DAV:"><prop><creationdate/><displayname/><getcontentlength/><getcontenttype/><getetag/><getlastmodified/><lockdiscovery/><resourcetype/><s:lastmodified_server xmlns:s="SAR:"/><s:lastmodified xmlns:s="SAR:"/></prop></propfind>

//await _query.GetAllObjectsAsync("/");
}

}
}
3 changes: 3 additions & 0 deletions starsky/starsky/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using starsky.foundation.webtelemetry.Helpers;
using starsky.foundation.webtelemetry.Processor;
using starsky.Helpers;
using starsky.Middleware;

namespace starsky
{
Expand Down Expand Up @@ -241,6 +242,8 @@ public void Configure(IApplicationBuilder app, IHostEnvironment env, IHostApplic
app.UseHttpsRedirection();
}

app.UseMiddleware<TestMiddleware>();

// Use the name of the application to use behind a reverse proxy
app.UsePathBase( PathHelper.PrefixDbSlash("starsky") );

Expand Down