Skip to content

Commit

Permalink
Made addClaims param on extensions and security service optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
VeritasSoftware committed Nov 5, 2018
1 parent 3a65cdb commit 1a69bc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 10 additions & 4 deletions AspNetCore.Security.Jwt/SecurityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static IServiceCollection AddSecurity<TAuthenticator>(this IServiceCollec
/// <returns>The services collection</returns>
public static IServiceCollection AddSecurity<TAuthenticator, TUserModel>(this IServiceCollection services,
IConfiguration configuration,
Action<IIdTypeBuilder<TUserModel>> addClaims,
Action<IIdTypeBuilder<TUserModel>> addClaims = null,
bool addSwaggerSecurity = false)
where TAuthenticator : class, IAuthentication<TUserModel>
where TUserModel : class, IAuthenticationUser
Expand All @@ -73,7 +73,10 @@ public static IServiceCollection AddSecurity<TAuthenticator, TUserModel>(this IS

services.AddSingleton(securitySettings);
services.AddSingleton<BaseSecuritySettings>(securitySettings);
services.AddSingleton<Action<IIdTypeBuilder<TUserModel>>>(x => addClaims);
if (addClaims != null)
{
services.AddSingleton<Action<IIdTypeBuilder<TUserModel>>>(x => addClaims);
}
services.AddScoped<ISecurityService<TUserModel>, SecurityService<TUserModel>>();
services.AddScoped<IAuthentication<TUserModel>, TAuthenticator>();

Expand Down Expand Up @@ -101,7 +104,7 @@ public static IServiceCollection AddSecurity<TAuthenticator, TUserModel>(this IS
/// <returns>The services collection</returns>
public static IServiceCollection AddFacebookSecurity(this IServiceCollection services,
IConfiguration configuration,
Action<IIdTypeBuilder<FacebookAuthModel>> addClaims,
Action<IIdTypeBuilder<FacebookAuthModel>> addClaims = null,
bool addSwaggerSecurity = false)
{
var securitySettings = new FacebookSecuritySettings();
Expand All @@ -110,7 +113,10 @@ public static IServiceCollection AddFacebookSecurity(this IServiceCollection ser

services.AddSingleton(securitySettings);
services.AddSingleton<BaseSecuritySettings>(securitySettings);
services.AddSingleton<Action<IIdTypeBuilder<FacebookAuthModel>>>(x => addClaims);
if (addClaims != null)
{
services.AddSingleton<Action<IIdTypeBuilder<FacebookAuthModel>>>(x => addClaims);
}
services.AddScoped<ISecurityService<FacebookAuthModel>, SecurityService<FacebookAuthModel>>();
services.AddScoped<IAuthentication<FacebookAuthModel>, FacebookAuthenticator>();

Expand Down
12 changes: 9 additions & 3 deletions AspNetCore.Security.Jwt/SecurityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class SecurityService<TUserModel> : ISecurityService<TUserModel>
private Action<IIdTypeBuilder<TUserModel>> addClaims;
const double DEFAULT_TOKEN_EXPIRY_IN_HOURS = 1;

public SecurityService(BaseSecuritySettings securitySettings, Action<IIdTypeBuilder<TUserModel>> addClaims)
public SecurityService(BaseSecuritySettings securitySettings, Action<IIdTypeBuilder<TUserModel>> addClaims = null)
{
this.securitySettings = securitySettings;
this.addClaims = addClaims;
Expand All @@ -81,14 +81,20 @@ public string GenerateToken(TUserModel user)

var builder = new IdTypeBuilder<TUserModel>(user);

this.addClaims(builder);
if (addClaims != null)
{
this.addClaims(builder);
}

var claims = new List<Claim> {
new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddHours(this.securitySettings.TokenExpiryInHours ?? DEFAULT_TOKEN_EXPIRY_IN_HOURS)).ToUnixTimeSeconds()}"),
new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}")
};

claims.AddRange(builder.ToClaims());
if (addClaims != null)
{
claims.AddRange(builder.ToClaims());
}

var token = new JwtSecurityToken(
issuer: this.securitySettings.Issuer,
Expand Down

0 comments on commit 1a69bc4

Please sign in to comment.