Skip to content

Commit

Permalink
fix: declarative extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobritodev committed Apr 18, 2022
1 parent afe8269 commit 6c726bb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ It's possible to configure some aspects of token
You will need to set a single dependency in your Authentication Controller:

```csharp

public AuthController(IJwtBuilder jwtBuilder)
{
_jwtBuilder = jwtBuilder;
Expand Down
2 changes: 0 additions & 2 deletions src/NetDevPack.Identity/Jwt/Abstractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public static IServiceCollection AddJwtConfiguration(this IServiceCollection ser
ValidIssuer = appSettings.Issuer,
};
});
services.AddDataProtection();
services.AddJwksManager().UseJwtValidation();
}

return services;
Expand Down
48 changes: 36 additions & 12 deletions src/NetDevPack.Identity/JwtBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,57 @@ namespace Microsoft.Extensions.DependencyInjection;

public static class JwtBuilderExtensions
{
public static IJwksBuilder AddNetDevPackIdentity<TIdentityUser, TKey>(this IServiceCollection services, Action<JwtOptions> options = null)
where TIdentityUser : IdentityUser<TKey> where TKey : IEquatable<TKey>
public static IJwksBuilder AddNetDevPackIdentity<TIdentityUser, TKey>(this IServiceCollection services, Action<JwtOptions> options = null)
where TIdentityUser : IdentityUser<TKey>
where TKey : IEquatable<TKey>
{
services.AddDataProtection();
services.AddHttpContextAccessor();
services.AddScoped<IJwtBuilder, JwtBuilderInject<TIdentityUser, TKey>>();
return services.AddHttpContextAccessor().AddJwksManager();
return services.AddJwksManager(options);
}
public static IJwksBuilder AddNetDevPackIdentity<TIdentityUser>(this IServiceCollection services, Action<JwtOptions> options = null)

public static IJwksBuilder AddNetDevPackIdentity<TIdentityUser>(this IServiceCollection services, Action<JwtOptions> options = null)
where TIdentityUser : IdentityUser
{
services.AddDataProtection();
services.AddHttpContextAccessor();
services.AddScoped<IJwtBuilder, JwtBuilderInject<TIdentityUser, string>>();
return services.AddHttpContextAccessor().AddJwksManager();
return services.AddJwksManager(options);
}

public static IJwksBuilder AddNetDevPackIdentity(this IServiceCollection services, Action<JwtOptions> options = null)
{
services.AddDataProtection();
services.AddHttpContextAccessor();
services.AddScoped<IJwtBuilder, JwtBuilderInject<IdentityUser, string>>();
return services.AddHttpContextAccessor().AddJwksManager(options);
return services.AddJwksManager(options);
}

public static IJwksBuilder AddNetDevPackIdentity<TIdentityUser, TKey>(this IJwksBuilder services)
where TIdentityUser : IdentityUser<TKey>
where TKey : IEquatable<TKey>
{
services.Services.AddHttpContextAccessor();
services.Services.AddScoped<IJwtBuilder, JwtBuilderInject<TIdentityUser, TKey>>();
return services;
}

public static IJwksBuilder AddNetDevPackIdentity<TIdentityUser>(this IJwksBuilder services)
where TIdentityUser : IdentityUser
{
services.Services.AddHttpContextAccessor();
services.Services.AddScoped<IJwtBuilder, JwtBuilderInject<TIdentityUser, string>>();
return services;
}

public static IJwksBuilder AddNetDevPackIdentity(this IJwksBuilder services)
{
services.Services.AddHttpContextAccessor();
services.Services.AddScoped<IJwtBuilder, JwtBuilderInject<IdentityUser, string>>();
return services;
}

public static IdentityBuilder AddIdentityConfiguration(this IServiceCollection services)
{
if (services == null) throw new ArgumentException(nameof(services));
services.AddNetDevPackIdentity();

return services
.AddIdentity<IdentityUser, IdentityRole>()
Expand All @@ -47,7 +73,6 @@ public static IdentityBuilder AddIdentityConfiguration(this IServiceCollection s
public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services, Action<IdentityOptions> options = null)
{
if (services == null) throw new ArgumentException(nameof(services));
services.AddNetDevPackIdentity<IdentityUser>();
return services
.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders();
Expand All @@ -67,7 +92,6 @@ public static IdentityBuilder AddCustomIdentity<TIdentityUser>(this IServiceColl
where TKey : IEquatable<TKey>
{
if (services == null) throw new ArgumentException(nameof(services));
services.AddNetDevPackIdentity<TIdentityUser, TKey>();
return services.AddIdentity<TIdentityUser, IdentityRole<TKey>>(options)
.AddDefaultTokenProviders();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void AddCustomIdentityAndKeyConfiguration(this IServiceCollection
.AddDefaultTokenProviders();

// Ours JWT configuration
services.AddJwtConfiguration(configuration);
services.AddJwtConfiguration(configuration).AddNetDevPackIdentity<MyIntIdentityUser, int>();
}
}

Expand Down

0 comments on commit 6c726bb

Please sign in to comment.