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); RealmKey realmKey = new() { Id = new("60bb79cf-4bac-4521-87f2-ac87cc15541f"), CreatedAt = DateTime.UtcNow, Priority = 10, }; realmKey.SetPrivateKey(_encryptionService, rsa.ExportPkcs8PrivateKey()); // Act KeyMapper mapper = new(_encryptionService); JsonWebKey jwk = mapper.KeyToJsonWebKey(realmKey); Assert.Equal("RSA", jwk.KeyType); Assert.Equal(realmKey.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)); } }