Skip to content

Commit

Permalink
Set options.SecurityTokenValidatorsFactory if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoPereyraDiaz committed Jun 26, 2019
1 parent c5be417 commit 6ee2ba3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ public class Startup
{
services.AddTransient<ISecurityTokenValidator, MyCustomTokenValidator>();
services.AddAuthentication()
.AddSimpleTokenAuthentication(options =>
{
options.SecurityTokenValidatorsFactory = () =>
{
return services.BuildServiceProvider().GetServices<ISecurityTokenValidator>();
};
});
.AddSimpleTokenAuthentication();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="MakingSense.AspNetCore.Abstractions" Version="2.0.0-alpha-173" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Linq" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ public class Startup
{
services.AddTransient<ISecurityTokenValidator, MyCustomTokenValidator>();
services.AddAuthentication()
.AddSimpleTokenAuthentication(options =>
{
options.SecurityTokenValidatorsFactory = () =>
{
return services.BuildServiceProvider().GetServices<ISecurityTokenValidator>();
};
});
.AddSimpleTokenAuthentication();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;

namespace MakingSense.AspNetCore.Authentication.SimpleToken
{
public static class SimpleTokenAuthenticationExtensions
{
public static AuthenticationBuilder AddSimpleTokenAuthentication(this AuthenticationBuilder builder)
=> builder.AddSimpleTokenAuthentication(SimpleTokenDefaults.AuthenticationScheme, _ => { });
=> builder.AddSimpleTokenAuthentication(SimpleTokenDefaults.AuthenticationScheme, null);

public static AuthenticationBuilder AddSimpleTokenAuthentication(this AuthenticationBuilder builder,
Action<SimpleTokenAuthenticationOptions> configureOptions)
Expand All @@ -22,7 +24,28 @@ public static AuthenticationBuilder AddSimpleTokenAuthentication(this Authentica
string displayName,
Action<SimpleTokenAuthenticationOptions> configureOptions)
{
return builder.AddScheme<SimpleTokenAuthenticationOptions, SimpleTokenAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
return builder.AddScheme<SimpleTokenAuthenticationOptions, SimpleTokenAuthenticationHandler>(authenticationScheme, displayName,
(SimpleTokenAuthenticationOptions options) => {
configureOptions?.Invoke(options);

if (options.SecurityTokenValidatorsFactory == null)
{
options.SecurityTokenValidatorsFactory = () =>
{
// TODO: fix it because it is using app services, and it should use scope services,
// a work around could be:
// ```
// SecurityTokenValidatorsFactory = () =>
// {
// var context = builder.Services.BuildServiceProvider().GetService<IHttpContextAccessor>().HttpContext;
// return context.RequestServices.GetServices<ISecurityTokenValidator>();
// }
// ```
var serviceProvider = builder.Services.BuildServiceProvider();
return serviceProvider.GetServices<ISecurityTokenValidator>();
};
}
});
}
}
}

0 comments on commit 6ee2ba3

Please sign in to comment.