Add project files.

This commit is contained in:
Sergio Matias Urquin
2025-04-29 18:57:20 -06:00
parent 62e9799537
commit d5925a6476
44 changed files with 2716 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using Azure.Identity;
using Core.Cerberos.Adapters.Common.Constants;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using Microsoft.Extensions.Logging;
namespace Core.Cerberos.Adapters.Helpers
{
public static class AuthHelper
{
private static readonly ILogger logger = LoggerFactory.Create(builder =>
{
builder.AddConsole();
}).CreateLogger("AuthHelper");
public static AuthSettings GetAuthSettings(WebApplicationBuilder builder, string appConfigLabel)
{
builder.Configuration.AddAzureAppConfiguration(options =>
{
var endpoint = builder.Configuration.GetSection("Endpoints:AppConfigurationURI").Value;
if (string.IsNullOrEmpty(endpoint))
throw new ArgumentException("The app configuration is missing");
options.Connect(new Uri(endpoint), new DefaultAzureCredential())
.Select(KeyFilter.Any, "cerberos_common")
.Select(KeyFilter.Any, appConfigLabel);
options.ConfigureKeyVault(keyVaultOptions =>
{
keyVaultOptions.SetCredential(new DefaultAzureCredential());
});
});
return new AuthSettings
{
AzureADInstance = builder.Configuration.GetSection(Secrets.AzureADInstance).Value,
AzureADTenantId = builder.Configuration.GetSection(Secrets.AzureADTenantId).Value,
AzureADClientId = builder.Configuration.GetSection(Secrets.AzureADClientId).Value,
AzureADClientSecret = builder.Configuration.GetSection(Secrets.AzureADClientSecret).Value,
HeathCerberosAppAuthorizationUrl = builder.Configuration.GetSection(Secrets.HeathCerberosAppAuthorizationUrl).Value,
HeathCerberosAppTokenUrl = builder.Configuration.GetSection(Secrets.HeathCerberosAppTokenUrl).Value,
HeathCerberosAppClientId = builder.Configuration.GetSection(Secrets.HeathCerberosAppClientId).Value,
HeathCerberosAppScope = builder.Configuration.GetSection(Secrets.HeathCerberosAppScope).Value,
PrivateKey = builder.Configuration.GetSection(Secrets.PrivateKey).Value,
PublicKey = builder.Configuration.GetSection(Secrets.PublicKey).Value,
};
}
}
}

View File

@@ -0,0 +1,94 @@
// ***********************************************************************
// <copyright file="RsaHelper.cs">
// Heath
// </copyright>
// ***********************************************************************
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;
using System.Text;
namespace Core.Cerberos.Adapters.Helpers
{
/// <summary>
/// Handles all methods related to RSA encryption"/>.
/// </summary>
public class RsaHelper
{
private readonly RSACryptoServiceProvider _privateKey;
private readonly RSACryptoServiceProvider _publicKey;
private readonly string keysFolder = "Keys\\";
private readonly string exeDirectory = AppContext.BaseDirectory;
/// <summary>
/// Initializes a new instance of <see cref="RsaHelper"/>.
/// </summary>
public RsaHelper()
{
exeDirectory = exeDirectory + keysFolder;
_publicKey = GetPublicKeyFromPemFile();
_privateKey = GetPrivateKeyFromPemFile();
}
/// <summary>
/// Encrypts a text using RSA algorithm.
/// </summary>
/// <param name="text">The text to be encrypted.</param>
/// <returns>The encrypted text.</returns>
public string Encrypt(string text)
{
byte[] dataBytes = Encoding.UTF8.GetBytes(text);
var encryptedBytes = _publicKey.Encrypt(Encoding.UTF8.GetBytes(text), true);
return Convert.ToBase64String(encryptedBytes);
}
/// <summary>
/// Decrypts a text using RSA algorithm.
/// </summary>
/// <param name="text">The encrypted text to be decrypted.</param>
/// <returns>The decrypted text.</returns>
public string Decrypt(string encrypted)
{
var decryptedBytes = _privateKey.Decrypt(Convert.FromBase64String(encrypted), true);
return Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length);
}
/// <summary>
///Obtains the private key from a file.
/// </summary>
/// <returns>The private key.</returns>
private RSACryptoServiceProvider GetPrivateKeyFromPemFile()
{
using (TextReader privateKeyTextReader = new StringReader(File.ReadAllText(Path.Combine(exeDirectory, "HeathPrivateKey.pem"))))
{
AsymmetricCipherKeyPair readKeyPair = (AsymmetricCipherKeyPair)new PemReader(privateKeyTextReader).ReadObject();
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)readKeyPair.Private);
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.ImportParameters(rsaParams);
return csp;
}
}
/// <summary>
///Obtains the public key from a file.
/// </summary>
/// <returns>The public key.</returns>
public RSACryptoServiceProvider GetPublicKeyFromPemFile()
{
using (TextReader publicKeyTextReader = new StringReader(File.ReadAllText(Path.Combine(exeDirectory, "HeathPublicKey.pem"))))
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)new PemReader(publicKeyTextReader).ReadObject();
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKeyParam);
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.ImportParameters(rsaParams);
return csp;
}
}
}
}