using System.Security.Cryptography; using IdentityShroud.Api.Mappers; using IdentityShroud.Core.Contracts; using IdentityShroud.Core.Messages; using IdentityShroud.Core.Model; using IdentityShroud.TestUtils.Substitutes; using Microsoft.AspNetCore.WebUtilities; namespace IdentityShroud.Api.Tests.Mappers; public class KeyMapperTests { private readonly IEncryptionService _encryptionService = EncryptionServiceSubstitute.CreatePassthrough(); [Fact] public void Test() { // Setup using RSA rsa = RSA.Create(2048); RSAParameters parameters = rsa.ExportParameters(includePrivateParameters: false); Key key = new() { Id = new("60bb79cf-4bac-4521-87f2-ac87cc15541f"), CreatedAt = DateTime.UtcNow, Priority = 10, }; key.SetPrivateKey(_encryptionService, rsa.ExportPkcs8PrivateKey()); // Act KeyMapper mapper = new(_encryptionService); JsonWebKey jwk = mapper.KeyToJsonWebKey(key); Assert.Equal("RSA", jwk.KeyType); Assert.Equal(key.Id.ToString(), jwk.KeyId); Assert.Equal("sig", jwk.Use); Assert.Equal(parameters.Exponent, WebEncoders.Base64UrlDecode(jwk.Exponent)); Assert.Equal(parameters.Modulus, WebEncoders.Base64UrlDecode(jwk.Modulus)); } }