Skip to content

Commit

Permalink
Upgrade to ASP.NET Core 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Sep 5, 2017
1 parent f58a370 commit 32a74e4
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 194 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### [Unreleased]
* Basic Authentication
* Select fields to return from query
* Upgrade to ASP.NET Core 2.0

### [0.3.0] - 2017-08-17
* Offset and limit slice option
Expand Down
4 changes: 2 additions & 2 deletions FakeServer.Test/FakeServer.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RootNamespace>FakeServer.Test</RootNamespace>
<AssemblyName>FakeServer.Test</AssemblyName>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@

<ItemGroup>
<PackageReference Include="JsonFlatFileDataStore" Version="1.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="WebSocket4Net" Version="0.15.0-beta6" />
<PackageReference Include="xunit" Version="2.3.0-beta3-build3705" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta3-build3705" />
Expand Down
2 changes: 1 addition & 1 deletion FakeServer.Test/IntegrationTestCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private async Task<bool> WaitForServer()
{
var sw = Stopwatch.StartNew();

while (sw.ElapsedMilliseconds < 10000)
while (sw.ElapsedMilliseconds < 20000)
{
try
{
Expand Down
1 change: 1 addition & 0 deletions FakeServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Dockerfile = Dockerfile
README.md = README.md
release.bat = release.bat
release.sh = release.sh
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FakeServer.Test", "FakeServer.Test\FakeServer.Test.csproj", "{746E6041-664F-4DC0-A570-1B2CABEB1669}"
Expand Down
116 changes: 116 additions & 0 deletions FakeServer/Authentication/Basic/BasicAuthentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;

namespace FakeServer.Authentication.Basic
{
public static class BasicAuthenticationConfiguration
{
public static void Configure(IServiceCollection services)
{
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = BasicAuthenticationDefaults.AuthenticationScheme;
})
.AddBasicAuthentication();
}
}

public static class BasicAuthenticationDefaults
{
public static string AuthenticationScheme => "Basic";
}

public static class BasicAuthenticationExtensions
{
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder)
=> builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme, _ => { });

public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, Action<BasicTokenOptions> configureOptions)
=> builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme, configureOptions);

public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, string authenticationScheme, Action<BasicTokenOptions> configureOptions)
=> builder.AddBasicAuthentication(authenticationScheme, displayName: null, configureOptions: configureOptions);

public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<BasicTokenOptions> configureOptions)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<BasicTokenOptions>, BasicTokenPostConfigureOptions>());
return builder.AddScheme<BasicTokenOptions, BasicAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
}
}

public class BasicTokenPostConfigureOptions : IPostConfigureOptions<BasicTokenOptions>
{
public void PostConfigure(string name, BasicTokenOptions options)
{
}
}

public class BasicTokenOptions : AuthenticationSchemeOptions
{
public BasicTokenOptions() : base()
{
}

public override void Validate()
{
}
}

public class BasicAuthenticationHandler : AuthenticationHandler<BasicTokenOptions>
{
public BasicAuthenticationHandler(IOptionsMonitor<BasicTokenOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{ }

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var authHeader = Context.Request.Headers["Authorization"].ToString();

bool Authenticate(out string name)
{
var authenticationSettings = Context.RequestServices.GetService(typeof(IOptions<AuthenticationSettings>)) as IOptions<AuthenticationSettings>;

var token = authHeader.Substring("Basic ".Length).Trim();
var credentialString = Encoding.UTF8.GetString(Convert.FromBase64String(token));
var credentials = credentialString.Split(':');

if (authenticationSettings.Value.Users.Any(e => e.Username == credentials[0] && e.Password == credentials[1]))
{
name = credentials[0];
return true;
}

name = string.Empty;
return false;
};

if (!string.IsNullOrEmpty(authHeader) &&
authHeader.StartsWith("basic", StringComparison.OrdinalIgnoreCase) &&
Authenticate(out string loginName))
{
var claims = new[] { new Claim("name", loginName), new Claim(ClaimTypes.Role, "Admin") };
var identity = new ClaimsIdentity(claims, BasicAuthenticationDefaults.AuthenticationScheme);

return Task.FromResult(AuthenticateResult.Success(
new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
BasicAuthenticationDefaults.AuthenticationScheme)));
}
else
{
Context.Response.Headers["WWW-Authenticate"] = BasicAuthenticationDefaults.AuthenticationScheme;
return Task.FromResult(AuthenticateResult.Fail(""));
}
}
}
}
86 changes: 0 additions & 86 deletions FakeServer/Authentication/Basic/BasicAuthenticationMiddleware.cs

This file was deleted.

81 changes: 81 additions & 0 deletions FakeServer/Authentication/Custom/AllowAllAuthentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;

namespace FakeServer.Authentication.Custom
{
public static class AllowAllAuthenticationConfiguration
{
public static void Configure(IServiceCollection services)
{
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = AllowAllAuthenticationDefaults.AuthenticationScheme;
})
.AddAllowAllAuthentication();
}
}

public static class AllowAllAuthenticationDefaults
{
public static string AuthenticationScheme => string.Empty;
}

public static class AllowAllAuthenticationExtensions
{
public static AuthenticationBuilder AddAllowAllAuthentication(this AuthenticationBuilder builder)
=> builder.AddAllowAllAuthentication(AllowAllAuthenticationDefaults.AuthenticationScheme, _ => { });

public static AuthenticationBuilder AddAllowAllAuthentication(this AuthenticationBuilder builder, Action<AllowAllOptions> configureOptions)
=> builder.AddAllowAllAuthentication(AllowAllAuthenticationDefaults.AuthenticationScheme, configureOptions);

public static AuthenticationBuilder AddAllowAllAuthentication(this AuthenticationBuilder builder, string authenticationScheme, Action<AllowAllOptions> configureOptions)
=> builder.AddAllowAllAuthentication(authenticationScheme, displayName: null, configureOptions: configureOptions);

public static AuthenticationBuilder AddAllowAllAuthentication(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<AllowAllOptions> configureOptions)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<AllowAllOptions>, AllowAllPostConfigureOptions>());
return builder.AddScheme<AllowAllOptions, AllowAllAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
}
}

public class AllowAllPostConfigureOptions : IPostConfigureOptions<AllowAllOptions>
{
public void PostConfigure(string name, AllowAllOptions options)
{
}
}

public class AllowAllOptions : AuthenticationSchemeOptions
{
public AllowAllOptions() : base()
{
}

public override void Validate()
{
}
}

public class AllowAllAuthenticationHandler : AuthenticationHandler<AllowAllOptions>
{
public AllowAllAuthenticationHandler(IOptionsMonitor<AllowAllOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{ }

protected async override Task<AuthenticateResult> HandleAuthenticateAsync()
{
return await Task.FromResult(AuthenticateResult.Success(
new AuthenticationTicket(
new ClaimsPrincipal(new ClaimsIdentity("Custom")),
new Microsoft.AspNetCore.Authentication.AuthenticationProperties(),
AllowAllAuthenticationDefaults.AuthenticationScheme)));
}
}
}

This file was deleted.

0 comments on commit 32a74e4

Please sign in to comment.