Skip to content
Snippets Groups Projects
Commit 8142058d authored by Klaus Fischer's avatar Klaus Fischer
Browse files

Dummy JWE encryption working

parent 199c4683
No related branches found
No related tags found
1 merge request!3Feature/440 mvp net sdk part 1
using FitConnect;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
namespace SenderTest;
public class JweTest {
private Sender _sender;
private ILogger<JweTest> _logger;
[SetUp]
public void SetUp() {
_logger = LoggerFactory.Create(cfg => cfg.AddConsole())
.CreateLogger<JweTest>();
_sender = new Sender(_logger,
FitConnectEndpoints.Create(FitConnectEndpoints.EndpointType.Development));
}
[Test]
public void TestJwe() {
var jwe = _sender.GetTestToken();
Assert.IsNotNull(jwe);
_logger.LogInformation(jwe);
}
}
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
namespace FitConnect;
......@@ -9,13 +12,57 @@ public class EncryptionBaseClass {
private readonly RSA _rsa;
private RSA? _publicKey;
private RSA? _privateKey;
private readonly X509Certificate2? certificate;
protected EncryptionBaseClass(ILogger? logger, X509Certificate2? certificate) {
_logger = logger;
_rsa = RSA.Create(4096);
if (certificate != null)
if (certificate != null) {
this.certificate = certificate;
ImportCertificate(certificate);
}
}
public string GetTestToken() {
var handler = new JwtSecurityTokenHandler();
var token = new SecurityTokenDescriptor {
Issuer = "FitConnect",
Audience = "FitConnect",
EncryptingCredentials =
new X509EncryptingCredentials(certificate ??
new X509Certificate2(CreateSelfSignedCertificate())),
Subject = new ClaimsIdentity(new Claim[] {
new Claim("Content", "Unencrypted content"),
}),
};
return handler.CreateEncodedJwt(token);
}
private X509Certificate2 CreateSelfSignedCertificate(string? exportPath = null) {
var rsa = RSA.Create(4096);
var req = new CertificateRequest("c=DE, cn=fitconnect.de",
rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
var cert = req.CreateSelfSigned(DateTimeOffset.Now.AddSeconds(-5),
DateTimeOffset.Now.AddYears(5));
if (cert.GetRSAPublicKey() == null)
throw new Exception("Certificate does not contain a public key");
if (cert.GetRSAPrivateKey() == null)
throw new Exception("Certificate does not contain a private key");
// Export the certificate to a PEM file, just for
// additional extern testing
// if (exportPath != null) {
// ExportCertificateToFile(exportPath, cert);
// }
return cert;
}
......
......@@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.19.0" />
</ItemGroup>
<ItemGroup>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment