Skip to content

Commit

Permalink
Renamed default IdentityFactory, opened up for inheritance, extension (
Browse files Browse the repository at this point in the history
…#428)

* Renamed DefaultIdentityFactory.cs to ClaimsIdentityFactory.cs
* Unsealed the class, made the methods virtual 
* Bumped version to 10.0.0-beta3
  • Loading branch information
abatishchev authored Jul 28, 2022
1 parent 0704bca commit 95b5631
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static AuthenticationBuilder AddJwt(this AuthenticationBuilder builder, s
builder.Services.AddJwtDecoder();
builder.Services.TryAddSingleton<IDateTimeProvider, SystemClockDatetimeProvider>();

builder.Services.TryAddSingleton<IIdentityFactory, DefaultIdentityFactory>();
builder.Services.TryAddSingleton<IIdentityFactory, ClaimsIdentityFactory>();
builder.Services.TryAddSingleton<ITicketFactory, DefaultTicketFactory>();

return builder.AddScheme<JwtAuthenticationOptions, JwtAuthenticationHandler>(authenticationScheme, configureOptions);
Expand All @@ -49,4 +49,4 @@ public static AuthenticationBuilder AddJwt<TFactory>(this AuthenticationBuilder
return builder.AddJwt(authenticationScheme, configureOptions);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,37 @@

namespace JWT.Extensions.AspNetCore.Factories
{
public sealed class DefaultIdentityFactory : IIdentityFactory
public class ClaimsIdentityFactory : IIdentityFactory
{
private readonly IOptionsMonitor<JwtAuthenticationOptions> _options;

public DefaultIdentityFactory(IOptionsMonitor<JwtAuthenticationOptions> options) =>
public ClaimsIdentityFactory(IOptionsMonitor<JwtAuthenticationOptions> options) =>
_options = options ?? throw new ArgumentNullException(nameof(options));

IIdentity IIdentityFactory.CreateIdentity(Type type, object payload)
public IIdentity CreateIdentity(Type type, object payload)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
if (payload is null)
throw new ArgumentException(nameof(payload));

var claims = ReadClaims(type, payload);
return CreateIdentity(claims);
}

protected virtual IEnumerable<Claim> ReadClaims(Type type, object payload)
{
Type targetType = typeof(IDictionary<string, object>);
if (!targetType.IsAssignableFrom(type))
throw new ArgumentOutOfRangeException(nameof(type), $"Type {type} is not assignable to {targetType}");

return CreateIdentity((IDictionary<string, object>)payload);

var dic = (IDictionary<string, object>)payload;
return dic.Select(p => new Claim(p.Key, p.Value.ToString()));
}

/// <summary>
/// Creates user's identity from user's claims
/// </summary>
/// <param name="payload"><see cref="IDictionary{String,String}" /> of user's claims</param>
/// <returns><see cref="ClaimsIdentity" /></returns>
public IIdentity CreateIdentity(IDictionary<string, object> payload)
{
var claims = payload.Select(p => new Claim(p.Key, p.Value.ToString()));
return _options.CurrentValue.IncludeAuthenticationScheme ?
private IIdentity CreateIdentity(IEnumerable<Claim> claims) =>
_options.CurrentValue.IncludeAuthenticationScheme ?
new ClaimsIdentity(claims, JwtAuthenticationDefaults.AuthenticationScheme) :
new ClaimsIdentity(claims);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Authors>Alexander Batishchev</Authors>
<PackageTags>jwt;json;asp.net;asp.net core;.net core;authorization</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>10.0.0-beta2</Version>
<Version>10.0.0-beta3</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT.Extensions.AspNetCore</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static async Task<JwtAuthenticationHandler> CreateHandler(string header)

var handler = new JwtAuthenticationHandler(
decoder,
new DefaultIdentityFactory(optionsMonitor.Object),
new ClaimsIdentityFactory(optionsMonitor.Object),
new DefaultTicketFactory(),
optionsMonitor.Object,
loggerFactory,
Expand All @@ -144,4 +144,4 @@ private static async Task<JwtAuthenticationHandler> CreateHandler(string header)
return handler;
}
}
}
}

0 comments on commit 95b5631

Please sign in to comment.