diff --git a/src/Infrastructure/Extensions/AuthenticationSetup.cs b/src/Infrastructure/Extensions/AuthenticationSetup.cs index 094ce74..0b9c963 100644 --- a/src/Infrastructure/Extensions/AuthenticationSetup.cs +++ b/src/Infrastructure/Extensions/AuthenticationSetup.cs @@ -38,7 +38,7 @@ public static class AuthenticationSetup services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton, JwtBearerOptionsPostConfigureOptions>(); - services.TryAddSingleton(typeof(ITokenBuilder<>),typeof(TokenBuilderBase<>)); + services.TryAddSingleton(typeof(ITokenBuilder), typeof(TokenBuilderBase<>)); var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret; ArgumentException.ThrowIfNullOrEmpty(key); diff --git a/src/Infrastructure/Security/DefaultTokenBuilder.cs b/src/Infrastructure/Security/DefaultTokenBuilder.cs new file mode 100644 index 0000000..ad3dad5 --- /dev/null +++ b/src/Infrastructure/Security/DefaultTokenBuilder.cs @@ -0,0 +1,11 @@ +using System.IdentityModel.Tokens.Jwt; + +namespace Infrastructure.Security; + +public class DefaultTokenBuilder( + JwtOptions jwtOptions, + JwtSecurityTokenHandler jwtSecurityTokenHandler, + IEncryptionService encryptionService) + : TokenBuilderBase(jwtOptions, jwtSecurityTokenHandler, encryptionService) +{ +} \ No newline at end of file diff --git a/src/Infrastructure/Security/ITokenBuilder.cs b/src/Infrastructure/Security/ITokenBuilder.cs index 6252712..164b045 100644 --- a/src/Infrastructure/Security/ITokenBuilder.cs +++ b/src/Infrastructure/Security/ITokenBuilder.cs @@ -7,9 +7,9 @@ using Microsoft.IdentityModel.Tokens; namespace Infrastructure.Security; -public interface ITokenBuilder where TId : IEquatable +public interface ITokenBuilder { - IList GetClaimsFromUserContext(IUserContext userContext); + IList GetClaimsFromUserContext(IUserContext userContext) where TId : IEquatable; void SetUserContext(TokenValidatedContext context); @@ -20,9 +20,9 @@ public abstract class TokenBuilderBase( JwtOptions jwtOptions, JwtSecurityTokenHandler jwtSecurityTokenHandler, IEncryptionService encryptionService) - : ITokenBuilder where TId : IEquatable + : ITokenBuilder where TId : IEquatable { - public IList GetClaimsFromUserContext(IUserContext userContext) + public virtual IList GetClaimsFromUserContext(IUserContext userContext) where TId : IEquatable { var claims = new List() { @@ -39,7 +39,7 @@ public abstract class TokenBuilderBase( return claims; } - public void SetUserContext(TokenValidatedContext context) + public virtual void SetUserContext(TokenValidatedContext context) { var userContext = context.HttpContext.RequestServices.GetService(typeof(IUserContext)) as IUserContext ?? @@ -55,7 +55,7 @@ public abstract class TokenBuilderBase( userContext.RemoteIpAddress = context.HttpContext.GetRequestIp()!; } - public JwtTokenInfo GenerateJwtTokenInfo(IReadOnlyCollection claims) + public virtual JwtTokenInfo GenerateJwtTokenInfo(IReadOnlyCollection claims) { var jwtToken = new JwtSecurityToken( issuer: jwtOptions.Issuer, diff --git a/src/Infrastructure/Security/IUserContext.cs b/src/Infrastructure/Security/IUserContext.cs index 15f36fc..89f668d 100644 --- a/src/Infrastructure/Security/IUserContext.cs +++ b/src/Infrastructure/Security/IUserContext.cs @@ -28,4 +28,8 @@ public abstract class UserContextBase : IUserContext where TId : IEqua public string[] RoleIds { get; set; } public string RemoteIpAddress { get; set; } +} + +public class DefaultUserContext : UserContextBase +{ } \ No newline at end of file diff --git a/src/Infrastructure/Security/JwtBearerOptionsPostConfigureOptions.cs b/src/Infrastructure/Security/JwtBearerOptionsPostConfigureOptions.cs index 715b2ce..f0e161b 100644 --- a/src/Infrastructure/Security/JwtBearerOptionsPostConfigureOptions.cs +++ b/src/Infrastructure/Security/JwtBearerOptionsPostConfigureOptions.cs @@ -4,7 +4,7 @@ namespace Infrastructure.Security; public class JwtBearerOptionsPostConfigureOptions( DefaultTokenHandler tokenHandler, - ITokenBuilder tokenBuilder) + ITokenBuilder tokenBuilder) : IPostConfigureOptions { public void PostConfigure(string? name, JwtBearerOptions options)