Skip to content

Commit

Permalink
fix(DataProtectionStore): change alg creation strategy
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Changed DataProtection protector
  • Loading branch information
brunobritodev committed Sep 12, 2021
1 parent 38f5f54 commit 5dc4e74
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 36 deletions.
16 changes: 2 additions & 14 deletions src/NetDevPack.Security.Jwt/DefaultStore/DataProtectionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public class DataProtectionStore : IJsonWebKeyStore
private readonly IOptions<KeyManagementOptions> _keyManagementOptions;
private readonly IMemoryCache _memoryCache;
private readonly IDataProtector _dataProtector;

private IXmlRepository KeyRepository { get; set; }
private IXmlRepository KeyRepository => _keyManagementOptions.Value.XmlRepository ?? GetFallbackKeyRepositoryEncryptorPair();

private const string Name = "NetDevPackSecurityJwt";

Expand All @@ -53,9 +52,7 @@ public DataProtectionStore(
_options = options;
_keyManagementOptions = keyManagementOptions;
_memoryCache = memoryCache;
_dataProtector = provider.CreateProtector(typeof(SecurityKeyWithPrivate).AssemblyQualifiedName); ;
Check();
// Force it to configure xml repository.
_dataProtector = provider.CreateProtector(nameof(SecurityKeyWithPrivate)); ;
}
public void Save(SecurityKeyWithPrivate securityParamteres)
{
Expand All @@ -78,15 +75,6 @@ public void Save(SecurityKeyWithPrivate securityParamteres)
ClearCache();
}

private void Check()
{
KeyRepository = _keyManagementOptions.Value.XmlRepository;
if (KeyRepository == null)
{
KeyRepository = GetFallbackKeyRepositoryEncryptorPair();
}
}



public SecurityKeyWithPrivate GetCurrentKey(JsonWebKeyType jwkType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace NetDevPack.Security.Jwt.Interfaces
{
public interface IJsonWebKeyService
{
JsonWebKey Generate(Algorithm jwsAlgorithm);
JsonWebKey Generate(Algorithm algorithm);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using NetDevPack.Security.Jwt.DefaultStore;
using NetDevPack.Security.Jwt.DefaultStore.Memory;
using NetDevPack.Security.Jwt.Interfaces;
using NetDevPack.Security.Jwt.Jwk;
using NetDevPack.Security.Jwt.Jwks;
using System;

namespace NetDevPack.Security.Jwt
{
Expand Down
27 changes: 8 additions & 19 deletions src/NetDevPack.Security.Jwt/Jwk/JwkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,8 @@ private JsonWebKey GenerateRsa()
private JsonWebKey GenerateECDsa(Algorithm algorithm)
{
var key = CryptoService.CreateECDsaSecurityKey(algorithm);
var parameters = key.ECDsa.ExportParameters(true);
return new JsonWebKey()
{
Kty = JsonWebAlgorithmsKeyTypes.EllipticCurve,
Use = "sig",
Kid = key.KeyId,
KeyId = key.KeyId,
X = Base64UrlEncoder.Encode(parameters.Q.X),
Y = Base64UrlEncoder.Encode(parameters.Q.Y),
D = Base64UrlEncoder.Encode(parameters.D),
Crv = CryptoService.GetCurveType(algorithm),
Alg = algorithm
};
return JsonWebKeyConverter.ConvertFromECDsaSecurityKey(key);

}
private JsonWebKey GenerateHMAC(Algorithm jwsAlgorithms)
{
Expand All @@ -45,15 +34,15 @@ private JsonWebKey GenerateAES(Algorithm jwsAlgorithms)
return jwk;
}

public JsonWebKey Generate(Algorithm jwsAlgorithm)
public JsonWebKey Generate(Algorithm algorithm)
{
return jwsAlgorithm.KeyType switch
return algorithm.KeyType switch
{
KeyType.RSA => GenerateRsa(),
KeyType.ECDsa => GenerateECDsa(jwsAlgorithm),
KeyType.HMAC => GenerateHMAC(jwsAlgorithm),
KeyType.AES => GenerateAES(jwsAlgorithm),
_ => throw new ArgumentOutOfRangeException(nameof(jwsAlgorithm), jwsAlgorithm, null)
KeyType.ECDsa => GenerateECDsa(algorithm),
KeyType.HMAC => GenerateHMAC(algorithm),
KeyType.AES => GenerateAES(algorithm),
_ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null)
};
}

Expand Down
Binary file modified src/NetDevPack.Security.Jwt/Jwks/JwksService.cs
Binary file not shown.
Binary file modified src/NetDevPack.Security.Jwt/Model/SecurityKeyWithPrivate.cs
Binary file not shown.
1 change: 1 addition & 0 deletions src/NetDevPack.Security.Jwt/NetDevPack.Security.Jwt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.12.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.15" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void ConfigureServices(IServiceCollection services)
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryClients(Config.GetClients());


services.AddJwksManager().IdentityServer4AutoJwksManager();
}

Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 5dc4e74

Please sign in to comment.