-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
281 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
FakeServer/Authentication/Basic/BasicAuthenticationMiddleware.cs
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
FakeServer/Authentication/Custom/AllowAllAuthentication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
FakeServer/Authentication/Custom/AllowAllAuthenticationMiddleware.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.