Skip to content

Commit

Permalink
Change the way how authnetication is handled. Now by assigning Princi…
Browse files Browse the repository at this point in the history
…pal to context. Allowing events to be used by dependency injection #4.
  • Loading branch information
msmolka committed Jan 20, 2018
1 parent 2eb25fe commit 2cf3940
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 92 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ Install using the [ZNetCS.AspNetCore.Authentication.Basic NuGet package](https:/
PM> Install-Package ZNetCS.AspNetCore.Authentication.Basic
```

# Important change in 3.0.0
The `OnValidatePrincipal` will not return `AuthenticationResult` any more. To simplify process can simply return `Task.CompletedTask`.
Also to make success authentication `Principal` have to be assigned to `ValidatePrincipalContext` context.

## Usage

When you install the package, it should be added to your `.csproj`. Alternatively, you can add it directly by adding:


```xml
<ItemGroup>
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="2.0.0" />
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="3.0.0" />
</ItemGroup>
```

Expand Down Expand Up @@ -64,15 +68,14 @@ public void ConfigureServices(IServiceCollection services)
new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
};

var ticket = new AuthenticationTicket(
new ClaimsPrincipal(new ClaimsIdentity(claims, BasicAuthenticationDefaults.AuthenticationScheme)),
new Microsoft.AspNetCore.Authentication.AuthenticationProperties(),
BasicAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, BasicAuthenticationDefaults.AuthenticationScheme));
context.Principal = principal;

return Task.FromResult(AuthenticateResult.Success(ticket));
// optional with following default.
// context.AuthenticationFailMessage = "Authentication failed.";
}

return Task.FromResult(AuthenticateResult.Fail("Authentication failed."));
return Task.CompletedTask;
}
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public static class BasicAuthenticationExtensions
/// The authentication builder.
/// </param>
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder)
{
return builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme);
}
=> builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme);

/// <summary>
/// Adds basic authentication.
Expand All @@ -45,9 +43,7 @@ public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBu
/// The authentication scheme.
/// </param>
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, string authenticationScheme)
{
return builder.AddBasicAuthentication(authenticationScheme, null);
}
=> builder.AddBasicAuthentication(authenticationScheme, null);

/// <summary>
/// Adds basic authentication.
Expand All @@ -59,9 +55,7 @@ public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBu
/// The configure options.
/// </param>
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, Action<BasicAuthenticationOptions> configureOptions)
{
return builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme, configureOptions);
}
=> builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme, configureOptions);

/// <summary>
/// Adds basic authentication.
Expand All @@ -79,9 +73,7 @@ public static AuthenticationBuilder AddBasicAuthentication(
this AuthenticationBuilder builder,
string authenticationScheme,
Action<BasicAuthenticationOptions> configureOptions)
{
return builder.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>(authenticationScheme, configureOptions);
}
=> builder.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>(authenticationScheme, configureOptions);

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,26 @@ public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> op

#endregion

#region Properties

/// <summary>
/// Gets or sets the events. The handler calls methods on the events which give the application control
/// at certain points where processing is occurring. If it is not provided a default instance
/// is supplied which does nothing when the methods are called.
/// </summary>
protected new BasicAuthenticationEvents Events
{
get => (BasicAuthenticationEvents)base.Events;
set => base.Events = value;
}

#endregion

#region Methods

/// <inheritdoc/>
protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new BasicAuthenticationEvents());

/// <inheritdoc/>
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
Expand Down Expand Up @@ -131,7 +149,14 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
string password = decodedCredentials.Substring(delimiterIndex + 1);

var context = new ValidatePrincipalContext(this.Context, this.Scheme, this.Options, userName, password);
return await this.Options.Events.ValidatePrincipal(context);
await this.Events.ValidatePrincipalAsync(context);

if (context.Principal == null)
{
return AuthenticateResult.Fail(context.AuthenticationFailMessage);
}

return AuthenticateResult.Success(new AuthenticationTicket(context.Principal, context.Properties, this.Scheme.Name));
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace ZNetCS.AspNetCore.Authentication.Basic.Events
using System;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Authentication;

#endregion

/// <summary>
Expand All @@ -28,14 +26,11 @@ public class BasicAuthenticationEvents
/// <summary>
/// Gets or sets a delegate assigned to this property will be invoked when the related method is called.
/// </summary>
public Func<ValidatePrincipalContext, Task<AuthenticateResult>> OnValidatePrincipal { get; set; } =
context => Task.FromResult(AuthenticateResult.Fail("Incorrect credentials."));
public Func<ValidatePrincipalContext, Task> OnValidatePrincipal { get; set; } = context => Task.CompletedTask;

#endregion

#region Implemented Interfaces

#region IBasicAuthenticationEvents
#region Public Methods

/// <summary>
/// Called each time a request principal has been validated by the middleware. By implementing this method the
Expand All @@ -47,9 +42,7 @@ public class BasicAuthenticationEvents
/// <returns>
/// A <see cref="Task"/> representing the completed operation.
/// </returns>
public virtual Task<AuthenticateResult> ValidatePrincipal(ValidatePrincipalContext context) => this.OnValidatePrincipal(context);

#endregion
public virtual Task ValidatePrincipalAsync(ValidatePrincipalContext context) => this.OnValidatePrincipal(context);

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public ValidatePrincipalContext(HttpContext context, AuthenticationScheme scheme

#region Public Properties

/// <summary>
/// Gets or sets the authentication fail message.
/// </summary>
public string AuthenticationFailMessage { get; set; } = "Authentication failed.";

/// <summary>
/// Gets the password.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageLicenseUrl>https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic</RepositoryUrl>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>3.0.0</VersionPrefix>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
Expand All @@ -37,13 +37,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2">
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down
Loading

0 comments on commit 2cf3940

Please sign in to comment.