Skip to content

Commit

Permalink
Reference & Depenency Optimizations
Browse files Browse the repository at this point in the history
Converted to .NET Standard 2.0 class library.
Moved ILibrary & ILibraryPathProvider to an abstractions packcage to reduce dependency imports when used in seperate target data projects.
Updated middleware invoke methods to run async since authorize requirement handlers could involve calls to a DB backend.
  • Loading branch information
kyse committed Dec 9, 2017
1 parent a1b6b8d commit 05a6b43
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,4 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
/src/Kyse.AspNetCore.StaticLibrary/Properties/PublishProfiles/Local.pubxml
/src/*/Properties/PublishProfiles/Local.pubxml
15 changes: 15 additions & 0 deletions Kyse.AspNetCore.StaticLibrary.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kyse.AspNetCore.StaticLibra
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{68390E87-984C-4887-AD19-1DD41C49110B}"
ProjectSection(SolutionItems) = preProject
src\common.props = src\common.props
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kyse.AspNetCore.StaticLibrary.Abstractions", "src\Kyse.AspNetCore.StaticLibrary.Abstractions\Kyse.AspNetCore.StaticLibrary.Abstractions.csproj", "{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,6 +34,18 @@ Global
{309EE5C7-C0EC-41BB-87EA-82FA835D29E7}.Release|x64.Build.0 = Release|Any CPU
{309EE5C7-C0EC-41BB-87EA-82FA835D29E7}.Release|x86.ActiveCfg = Release|Any CPU
{309EE5C7-C0EC-41BB-87EA-82FA835D29E7}.Release|x86.Build.0 = Release|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Debug|x64.ActiveCfg = Debug|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Debug|x64.Build.0 = Debug|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Debug|x86.ActiveCfg = Debug|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Debug|x86.Build.0 = Debug|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Release|Any CPU.Build.0 = Release|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Release|x64.ActiveCfg = Release|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Release|x64.Build.0 = Release|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Release|x86.ActiveCfg = Release|Any CPU
{770F83D8-C6C1-47BC-BE6C-983BC9C67F28}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,3 @@ However, there are a few more added options avaialable to control authentication
- AllowAnonymous - Set to true to allow anonymous access to the libraries expoesd by this middleware.
- AuthenticationSchemes - Specify an Authentication Scheme to use for authenticating the request.
> If not specified, all defined authentication schemes will be checked until one authenticates with a ClaimsPrincipal.

## Todo & Thoughts
- Switch Invoke to async to allow authorizeasync calls since authorize requirements could be hitting a database backend.
- Might be worth adding MapWhen to UseStaticLibrary extension against the root library path to prevent unneeded calls to our middleware code.
- Move interfaces to an abstractions package to remove nuget package dependencies being pulled into solution setups where a data poco implementing ILibrary is in a data project and doesn't need to pull in the larger staticlibrary package dependencies.
- Review dependencies to see if we can reduce them (can we import abstraction packages instead of full core packages?).
- Expand support to provide a service for exposing files via a temp path or via a temp auth token (media streaming?). Might be too in depth for the purposee of this library though, as such a concept could just wrap and interface with this library via the path provider since each request will check with the path provider if a library path is valid. However, in the current state, temp paths are limited to exposing all files in the directory, thus you'd hav to combine authorization for now to limit file access if so desired.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\common.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>$(CommonVersion)</Version>
<Authors>Jared Fisher</Authors>
<Description>Abstractions for Kyse.AspNetCore.StaticLibrary</Description>
<PackageId>Kyse.AspNetCore.StaticLibrary.Abstractions</PackageId>
<PackageTags>staticlibrary abstractions</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes></PackageReleaseNotes>
<Copyright>© $(CopyrightYear) Jared Fisher</Copyright>
<RepositoryUrl>https://github.com/kyse/Kyse.AspNetCore.StaticLibrary.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/kyse/Kyse.AspNetCore.StaticLibrary</PackageProjectUrl>
<RepositoryType>github</RepositoryType>
<Product>Kyse.AspNetCore.StaticLibrary</Product>
<RootNamespace>Kyse.AspNetCore.StaticLibrary</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>Kyse.AspNetCore.StaticLibrary.Abstractions.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Remove="Kyse.AspNetCore.StaticLibrary.Abstractions.xml" />
</ItemGroup>



</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/Kyse.AspNetCore.StaticLibrary/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Changes:
// - Including internal classes required for modified files.

using System;
using System.Threading.Tasks;

namespace Kyse.AspNetCore.StaticLibrary
Expand Down
1 change: 0 additions & 1 deletion src/Kyse.AspNetCore.StaticLibrary/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<CopyrightInitialYear>2017</CopyrightInitialYear>
<CopyrightYear Condition="$([System.DateTime]::Now.ToString(`yyyy`)) == $(CopyrightInitialYear)">$([System.DateTime]::Now.ToString(`yyyy`))</CopyrightYear>
<CopyrightYear Condition="$(CopyrightYear) == ''">$(CopyrightInitialYear)-$([System.DateTime]::Now.ToString(`yyyy`))</CopyrightYear>
</PropertyGroup>
<Import Project="..\common.props" Condition="Exists('..\common.props')" />

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Version>1.0.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>$(CommonVersion)</Version>
<Authors>Jared Fisher</Authors>
<Description>Dynamic Library to filesystem mapping with Authorization implementation of Microsoft.AspNetCore.StaticFiles.</Description>
<PackageId>Kyse.AspNetCore.StaticLibrary</PackageId>
<PackageTags>staticlibrary staticfiles staticbrowser static files browser auhentication authorization</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes>AspNetCore 2.0 version.</PackageReleaseNotes>
<PackageReleaseNotes>Moved ILibrary and ILibraryPathProvider to an abstractions package.
Converted to a .NET Standard 2.0 class library.
Optimized references.</PackageReleaseNotes>
<Copyright>© $(CopyrightYear) Jared Fisher</Copyright>
<RepositoryUrl>https://github.com/kyse/Kyse.AspNetCore.StaticLibrary.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/kyse/Kyse.AspNetCore.StaticLibrary</PackageProjectUrl>
<RepositoryType>github</RepositoryType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>latest</LangVersion>
<DocumentationFile>Kyse.AspNetCore.StaticLibrary.xml</DocumentationFile>
</PropertyGroup>

Expand All @@ -33,12 +30,15 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kyse.AspNetCore.StaticLibrary.Abstractions\Kyse.AspNetCore.StaticLibrary.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions src/Kyse.AspNetCore.StaticLibrary/LibraryBrowserMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// - Updated to support class name changes.
// - Added authentication and authorization.
// - Added dynamic library path mapping.
// - Converted Invoke to async
//
// Original Copywrite/License Notice:
//
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -87,24 +87,28 @@ public LibraryBrowserMiddleware(RequestDelegate next, IHostingEnvironment hostin
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public Task Invoke(HttpContext context)
public async Task Invoke(HttpContext context)
{
// Check if the URL matches any expected paths
if (!Helpers.IsGetOrHeadMethod(context.Request.Method) ||
!Helpers.TryMatchPath(context, _prefixUrl, forDirectory: true, subpath: out PathString libraryPath) ||
!Helpers.TryMatchLibrary(libraryPath, _pathProvider.GetLibraries(), forDirectory: true, subpath: out PathString subpath, library: out ILibrary library))
return _next(context);
!Helpers.TryMatchLibrary(libraryPath, _pathProvider.GetLibraries(), forDirectory: true,
subpath: out PathString subpath, library: out ILibrary library))
{
await _next(context);
return;
}

if (!context.IsAuthenticated(_options))
{
context.Response.StatusCode = 401;
return Constants.CompletedTask;
return;
}

if (!context.IsAuthorized(_options, library, LibraryServerAuthorizationPolicy.Browser))
{
context.Response.StatusCode = 403;
return Constants.CompletedTask;
return;
}

// If the path matches a directory but does not end in a slash, redirect to add the slash.
Expand All @@ -114,14 +118,16 @@ public Task Invoke(HttpContext context)
context.Response.StatusCode = 301;
context.Response.Headers[HeaderNames.Location] =
context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
return Constants.CompletedTask;
return;
}

if (TryGetDirectoryInfo(library, subpath, out IDirectoryContents contents))
return _formatter.GenerateContentAsync(context, contents);
{
await _formatter.GenerateContentAsync(context, contents);
return;
}

context.Response.StatusCode = 500;
return Constants.CompletedTask;
}

private bool TryGetDirectoryInfo(ILibrary library, PathString subpath, out IDirectoryContents contents)
Expand Down
2 changes: 0 additions & 2 deletions src/Kyse.AspNetCore.StaticLibrary/LibraryBrowserOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.StaticFiles;

namespace Kyse.AspNetCore.StaticLibrary
Expand Down
3 changes: 0 additions & 3 deletions src/Kyse.AspNetCore.StaticLibrary/LibraryFileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
Expand Down
1 change: 0 additions & 1 deletion src/Kyse.AspNetCore.StaticLibrary/LibraryFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Options;

namespace Kyse.AspNetCore.StaticLibrary
Expand Down
Loading

0 comments on commit 05a6b43

Please sign in to comment.