Not a final solution,dealing with 403 status codes ...

This commit is contained in:
Paul Schneider 2020-10-17 23:40:37 +01:00
commit a68f7ed6a0
10 changed files with 253 additions and 81 deletions

View file

@ -1,10 +1,13 @@
using System;
using System.IO;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Extensions.Logging;
using Yavsc.Helpers;
@ -13,18 +16,60 @@ using Yavsc.ViewModels.Auth;
namespace Yavsc
{
public static class YaFileSServerExtenstions
{
public static IApplicationBuilder UseYaFileServer(this IApplicationBuilder builder, FileServerOptions options)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
if (options == null)
{
throw new ArgumentNullException("options");
}
if (options == null)
{
throw new ArgumentNullException("options");
}
if (options.EnableDefaultFiles)
{
builder = builder.UseDefaultFiles(options.DefaultFilesOptions);
}
if (options.EnableDirectoryBrowsing)
{
builder = builder.UseDirectoryBrowser(options.DirectoryBrowserOptions);
}
return builder.UseYaSendFileFallback().UseStaticFiles(options.StaticFileOptions);
}
public static IApplicationBuilder UseYaSendFileFallback(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
return builder.UseMiddleware<YaSendFileMiddleware>(new object[0]);
}
}
public partial class Startup
{
public static FileServerOptions UserFilesOptions { get; private set; }
public static FileServerOptions GitOptions { get; private set; }
public static FileServerOptions UserFilesOptions { get; private set; }
public static FileServerOptions GitOptions { get; private set; }
public static FileServerOptions AvatarsOptions { get; set; }
static IAuthorizationService AuthorizationService { get; set; }
public static FileServerOptions AvatarsOptions { get; set; }
public void ConfigureFileServerApp(IApplicationBuilder app,
SiteSettings siteSettings, IHostingEnvironment env,
IAuthorizationService authorizationService)
{
var userFilesDirInfo = new DirectoryInfo( siteSettings.Blog );
AbstractFileSystemHelpers.UserFilesDirName = userFilesDirInfo.FullName;
AuthorizationService = authorizationService;
var userFilesDirInfo = new DirectoryInfo(siteSettings.Blog);
AbstractFileSystemHelpers.UserFilesDirName = userFilesDirInfo.FullName;
if (!userFilesDirInfo.Exists) userFilesDirInfo.Create();
@ -34,21 +79,11 @@ namespace Yavsc
RequestPath = PathString.FromUriComponent(Constants.UserFilesPath),
EnableDirectoryBrowsing = env.IsDevelopment(),
};
UserFilesOptions.EnableDefaultFiles=true;
UserFilesOptions.StaticFileOptions.ServeUnknownFileTypes=true;
UserFilesOptions.EnableDefaultFiles = true;
UserFilesOptions.StaticFileOptions.ServeUnknownFileTypes = true;
UserFilesOptions.StaticFileOptions.OnPrepareResponse = OnPrepareUserFileResponse;
/* TODO needs a better design, at implementation time (don't use database, but in memory data) */
UserFilesOptions.StaticFileOptions.OnPrepareResponse += async context =>
{
var uname = context.Context.User?.GetUserName();
var path = context.Context.Request.Path;
var result = await authorizationService.AuthorizeAsync(context.Context.User, new ViewFileContext
{ UserName = uname, File = context.File, Path = path } , new ViewRequirement());
if (!result) {
context.Context.Response.StatusCode = 403;
context.Context.Abort();
}
};
var avatarsDirInfo = new DirectoryInfo(Startup.SiteSetup.Avatars);
if (!avatarsDirInfo.Exists) avatarsDirInfo.Create();
AvatarsDirName = avatarsDirInfo.FullName;
@ -72,8 +107,8 @@ namespace Yavsc
};
GitOptions.DefaultFilesOptions.DefaultFileNames.Add("index.md");
GitOptions.StaticFileOptions.ServeUnknownFileTypes = true;
_logger.LogInformation( $"{GitDirName}");
GitOptions.StaticFileOptions.OnPrepareResponse+= OnPrepareGitRepoResponse;
_logger.LogInformation($"{GitDirName}");
GitOptions.StaticFileOptions.OnPrepareResponse += OnPrepareGitRepoResponse;
app.UseFileServer(UserFilesOptions);
@ -82,12 +117,28 @@ namespace Yavsc
app.UseFileServer(GitOptions);
app.UseStaticFiles();
}
private async void OnPrepareUserFileResponse(StaticFileResponseContext context)
{
var uname = context.Context.User?.GetUserName();
var path = context.Context.Request.Path;
var result = await AuthorizationService.AuthorizeAsync(context.Context.User,
new ViewFileContext{ UserName = uname, File = context.File, Path = path }, new ViewRequirement());
if (!result)
{
_logger.LogInformation("403");
// TODO prettier
context.Context.Response.StatusCode = 403;
context.Context.Response.Redirect("/Home/Status/403");
}
}
static void OnPrepareGitRepoResponse(StaticFileResponseContext context)
{
if (context.File.Name.EndsWith(".ansi.log"))
{
context.Context.Response.Redirect("/Git"+context.Context.Request.Path);
context.Context.Response.Redirect("/Git" + context.Context.Request.Path);
}
}
}