Skip to content

Commit

Permalink
Fixed loading JWK with private key for signing operation
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-abblix committed Apr 20, 2024
1 parent 1154fbf commit aa4d890
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
10 changes: 2 additions & 8 deletions Abblix.Jwt.UnitTests/JwtEncryptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
// For more information, please refer to the license agreement located at:
// https://github.com/Abblix/Oidc.Server/blob/master/README.md

using System.Security.Cryptography;
using System.Text.Json.Nodes;
using Abblix.Utils;
using Microsoft.IdentityModel.Tokens;
Expand All @@ -37,13 +36,8 @@ namespace Abblix.Jwt.UnitTests;

public class JwtEncryptionTests
{
// Generates an RSA key for encryption.
private static readonly JsonWebKey EncryptingKey = JsonWebKeyFactory.CreateRsa(JsonWebKeyUseNames.Enc, 2048);

// Generates an RSA key for signing.
private static readonly JsonWebKey SigningKey = JsonWebKeyFactory.CreateRsa(JsonWebKeyUseNames.Sig, 2048);

// Helper method to generate RSA JsonWebKey.
private static readonly JsonWebKey EncryptingKey = JsonWebKeyFactory.CreateRsa(JsonWebKeyUseNames.Enc);
private static readonly JsonWebKey SigningKey = JsonWebKeyFactory.CreateRsa(JsonWebKeyUseNames.Sig);

[Fact]
public async Task JwtFullCycleTest()
Expand Down
7 changes: 4 additions & 3 deletions Abblix.Jwt/JsonWebKeyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public static class JsonWebKeyFactory
{
var algorithm = usage switch
{
JsonWebKeyUseNames.Sig => "RS256",
JsonWebKeyUseNames.Enc => "RS256",
_ => throw new ArgumentException("Invalid usage specified. Valid options are 'sig' for signing or 'enc' for encryption.", nameof(usage))
JsonWebKeyUseNames.Sig or JsonWebKeyUseNames.Enc => "RS256",
_ => throw new ArgumentException(
$"Invalid usage specified. Valid options are '{JsonWebKeyUseNames.Sig}' for signing or '{JsonWebKeyUseNames.Enc}' for encryption.",
nameof(usage))
};

using var rsa = RSA.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ClientJwtFormatter(
/// </remarks>
public async Task<string> FormatAsync(JsonWebToken token, ClientInfo clientInfo)
{
var signingCredentials = await _serviceKeysProvider.GetSigningKeys()
var signingCredentials = await _serviceKeysProvider.GetSigningKeys(true)
.FirstByAlgorithmAsync(token.Header.Algorithm);

var encryptingCredentials = await _clientKeysProvider.GetEncryptionKeys(clientInfo)
Expand Down

0 comments on commit aa4d890

Please sign in to comment.