34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
|
|
using System.Security.Cryptography;
|
||
|
|
using IdentityShroud.Core.Contracts;
|
||
|
|
using IdentityShroud.Core.Messages;
|
||
|
|
using IdentityShroud.Core.Model;
|
||
|
|
using IdentityShroud.Core.Security;
|
||
|
|
using Microsoft.AspNetCore.WebUtilities;
|
||
|
|
|
||
|
|
namespace IdentityShroud.Api.Mappers;
|
||
|
|
|
||
|
|
public class KeyMapper(IEncryptionService encryptionService)
|
||
|
|
{
|
||
|
|
public JsonWebKey KeyToJsonWebKey(Key key)
|
||
|
|
{
|
||
|
|
using var rsa = RsaHelper.LoadFromPkcs8(key.GetPrivateKey(encryptionService));
|
||
|
|
RSAParameters parameters = rsa.ExportParameters(includePrivateParameters: false);
|
||
|
|
|
||
|
|
return new JsonWebKey()
|
||
|
|
{
|
||
|
|
KeyType = rsa.SignatureAlgorithm,
|
||
|
|
KeyId = key.Id.ToString(),
|
||
|
|
Use = "sig",
|
||
|
|
Exponent = WebEncoders.Base64UrlEncode(parameters.Exponent!),
|
||
|
|
Modulus = WebEncoders.Base64UrlEncode(parameters.Modulus!),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public JsonWebKeySet KeyListToJsonWebKeySet(IEnumerable<Key> keys)
|
||
|
|
{
|
||
|
|
return new JsonWebKeySet()
|
||
|
|
{
|
||
|
|
Keys = keys.Select(e => KeyToJsonWebKey(e)).ToList(),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|