mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 15:11:10 +00:00
Adds Infrastructure security cryptography service (#43)
* Adds Infrastructure security cryptography service * Updates GitHub Action script to account for empty Environment values * Updates section of GitHub Action script * Fixes order of configuration settings import * Hardcodes secrets used during integration tests
This commit is contained in:
1 parent
a71487eb05
commit
09928047d0
12 files changed
+280
-8
No files matched your search
@@ -23,7 +23,8 @@ public static class InfrastructureLayerServices
|
||||
// scan the current assembly for validators and add them to the DI container
|
||||
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), ServiceLifetime.Singleton);
|
||||
|
||||
services.AddTransient<IHashService, HashService>();
|
||||
services.AddSingleton<IHashService, HashService>();
|
||||
services.AddSingleton<ICryptographyService, CryptographyService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -43,12 +43,21 @@ public static class SharedConfiguration
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
|
||||
services.AddOptions<MediaSettingsModel>()
|
||||
.Bind(configuration.GetRequiredSection(MediaSettingsModel.SECTION_NAME))
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
services.AddOptions<JwtSettingsModel>()
|
||||
.Bind(configuration.GetRequiredSection(JwtSettingsModel.SECTION_NAME))
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
services.AddOptions<EncryptionSettingsModel>()
|
||||
.Bind(configuration.GetRequiredSection(EncryptionSettingsModel.SECTION_NAME))
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,12 @@ public static partial class Errors
|
||||
public static Error ApplicationThemeCannotBeEmpty => Error.Validation(description: nameof(ApplicationThemeCannotBeEmpty));
|
||||
public static Error DatabaseConnectionStringCannotBeEmpty => Error.Validation(description: nameof(DatabaseConnectionStringCannotBeEmpty));
|
||||
public static Error MediaRootDirectoryCannotBeEmpty => Error.Validation(description: nameof(MediaRootDirectoryCannotBeEmpty));
|
||||
public static Error EncryptionSecretKeyCannotBeEmpty => Error.Validation(description: nameof(EncryptionSecretKeyCannotBeEmpty));
|
||||
public static Error EncryptionSecretKeyMustBeABase64String => Error.Validation(description: nameof(EncryptionSecretKeyMustBeABase64String));
|
||||
public static Error JwtSecretKeyCannotBeEmpty => Error.Validation(description: nameof(JwtSecretKeyCannotBeEmpty));
|
||||
public static Error JwtSecretKeyTooShort => Error.Validation(description: nameof(JwtSecretKeyTooShort));
|
||||
public static Error JwtExpiryMinutesMustBePositive => Error.Validation(description: nameof(JwtExpiryMinutesMustBePositive));
|
||||
public static Error JwtIssuerCannotBeEmpty => Error.Validation(description: nameof(JwtIssuerCannotBeEmpty));
|
||||
public static Error JwtAudienceCannotBeEmpty => Error.Validation(description: nameof(JwtAudienceCannotBeEmpty));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.Models.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Model for deserializing media settings.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{SECTION_NAME}")]
|
||||
public class EncryptionSettingsModel
|
||||
{
|
||||
public const string SECTION_NAME = "EncryptionSettings";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the root directory where media files are stored.
|
||||
/// </summary>
|
||||
public required string SecretKey { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.Models.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Model for deserializing media settings.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{SECTION_NAME}")]
|
||||
public class JwtSettingsModel
|
||||
{
|
||||
public const string SECTION_NAME = "JwtSettings";
|
||||
|
||||
/// <summary>
|
||||
/// The secret key used to sign the JWT. This should be a strong, securely stored key.
|
||||
/// </summary>
|
||||
public required string SecretKey { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The duration (in minutes) for which the JWT is valid.
|
||||
/// </summary>
|
||||
public required int ExpiryMinutes { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The issuer of the JWT, typically the application or service generating the token.
|
||||
/// </summary>
|
||||
public required string Issuer { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The intended audience for the JWT, usually representing the clients that can consume the token.
|
||||
/// </summary>
|
||||
public required string Audience { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using System;
|
||||
using System.Buffers.Text;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for the EncryptionSettings application configuration settings section.
|
||||
/// </summary>
|
||||
public class EncryptionSettingsModelValidator : AbstractValidator<EncryptionSettingsModel>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EncryptionSettingsModelValidator"/> class.
|
||||
/// </summary>
|
||||
public EncryptionSettingsModelValidator()
|
||||
{
|
||||
RuleFor(x => x.SecretKey)
|
||||
.NotEmpty().WithMessage(Errors.Errors.Configuration.EncryptionSecretKeyCannotBeEmpty.Description)
|
||||
.Must(value => Base64.IsValid(value.AsSpan())).WithMessage(Errors.Errors.Configuration.EncryptionSecretKeyMustBeABase64String.Description);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for the JwtSettings application configuration settings section.
|
||||
/// </summary>
|
||||
public class JwtSettingsModelValidator : AbstractValidator<JwtSettingsModel>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JwtSettingsModelValidator"/> class.
|
||||
/// </summary>
|
||||
public JwtSettingsModelValidator()
|
||||
{
|
||||
RuleFor(x => x.SecretKey)
|
||||
.NotEmpty()
|
||||
.WithMessage(Errors.Errors.Configuration.JwtSecretKeyCannotBeEmpty.Description)
|
||||
.MinimumLength(32)
|
||||
.WithMessage(Errors.Errors.Configuration.JwtSecretKeyTooShort.Description);
|
||||
|
||||
RuleFor(x => x.ExpiryMinutes)
|
||||
.GreaterThan(0)
|
||||
.WithMessage(Errors.Errors.Configuration.JwtExpiryMinutesMustBePositive.Description);
|
||||
|
||||
RuleFor(x => x.Issuer)
|
||||
.NotEmpty()
|
||||
.WithMessage(Errors.Errors.Configuration.JwtIssuerCannotBeEmpty.Description);
|
||||
|
||||
RuleFor(x => x.Audience)
|
||||
.NotEmpty()
|
||||
.WithMessage(Errors.Errors.Configuration.JwtAudienceCannotBeEmpty.Description);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.Validators;
|
||||
|
||||
Reference in new issue
Block a user