(refactor): code cleanup

This commit is contained in:
Maksym Sadovnychyy 2025-11-09 15:54:47 +01:00
parent b80fed3245
commit 712b880ab2
15 changed files with 86 additions and 122 deletions

View File

@ -2,7 +2,9 @@
using System.Text.Json.Serialization;
namespace MaksIT.LetsEncrypt.Entities.Jws;
public class Jwk {
/// <summary>
/// "kty" (Key Type) Parameter

View File

@ -1,9 +1,8 @@
using System;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
namespace MaksIT.LetsEncrypt.Entities.Jws;
public class JwsMessage {
public string? Protected { get; set; }

View File

@ -1,5 +1,6 @@
namespace MaksIT.LetsEncrypt.Entities {
public class CachedHostname {
namespace MaksIT.LetsEncrypt.Entities;
public class CachedHostname {
public string Hostname { get; set; }
public DateTime Expires { get; set; }
public bool IsUpcomingExpire { get; set; }
@ -12,5 +13,4 @@
IsUpcomingExpire = isUpcomingExpire;
IsDisabled = isDisabled;
}
}
}

View File

@ -2,9 +2,9 @@
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using MaksIT.LetsEncrypt.Entities.Jws;
namespace MaksIT.LetsEncrypt.Entities;
public class RegistrationCache {

View File

@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt;
namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt {
public enum RevokeReason {
public enum RevokeReason {
Unspecified = 0,
KeyCompromise = 1,
CaCompromise = 2,
@ -14,5 +9,4 @@ namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt {
CessationOfOperation = 5,
PrivilegeWithdrawn = 6,
AaCompromise = 7
}
}

View File

@ -1,13 +1,10 @@
using MaksIT.LetsEncrypt.Models.Responses;
using MaksIT.LetsEncrypt.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt {
public class State {
namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt;
public class State {
public bool IsStaging { get; set; }
public AcmeDirectory? Directory { get; set; }
public JwsService? JwsService { get; set; }
@ -15,5 +12,4 @@ namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt {
public List<AuthorizationChallengeChallenge> Challenges { get; } = new List<AuthorizationChallengeChallenge>();
public string? Nonce { get; set; }
public RegistrationCache? Cache { get; set; }
}
}

View File

@ -1,7 +1,8 @@
using MaksIT.Core.Extensions;
using MaksIT.LetsEncrypt.Models.Responses;
using MaksIT.LetsEncrypt.Models.Responses;
namespace MaksIT.LetsEncrypt.Exceptions;
public class LetsEncrytException : Exception {
public Problem? Problem { get; }

View File

@ -1,9 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using MaksIT.LetsEncrypt.Services;
using Microsoft.Extensions.Configuration;
namespace MaksIT.LetsEncrypt.Extensions;
public static class ServiceCollectionExtensions {
public static void RegisterLetsEncrypt(this IServiceCollection services, ILetsEncryptConfiguration appSettings) {

View File

@ -1,22 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.LetsEncrypt;
namespace MaksIT.LetsEncrypt {
public interface ILetsEncryptConfiguration {
public interface ILetsEncryptConfiguration {
string Production { get; set; }
string Staging { get; set; }
}
}
public class LetsEncryptConfiguration : ILetsEncryptConfiguration {
public class LetsEncryptConfiguration : ILetsEncryptConfiguration {
public required string Production { get; set; }
public required string Staging { get; set; }
}
}

View File

@ -3,13 +3,11 @@
* https://tools.ietf.org/html/rfc4648#section-5
*/
using System.Text;
using System.Security.Cryptography;
using MaksIT.Core.Extensions;
using MaksIT.LetsEncrypt.Entities.Jws;
using MaksIT.Core.Extensions;
namespace MaksIT.LetsEncrypt.Services;

View File

@ -3,7 +3,13 @@
* https://datatracker.ietf.org/doc/html/draft-ietf-acme-acme-12
*/
using System.Text;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using MaksIT.Results;
using MaksIT.Core.Extensions;
using MaksIT.LetsEncrypt.Entities;
using MaksIT.LetsEncrypt.Entities.Jws;
@ -12,17 +18,10 @@ using MaksIT.LetsEncrypt.Exceptions;
using MaksIT.LetsEncrypt.Models.Interfaces;
using MaksIT.LetsEncrypt.Models.Requests;
using MaksIT.LetsEncrypt.Models.Responses;
using MaksIT.Results;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace MaksIT.LetsEncrypt.Services;
public interface ILetsEncryptService {
Task<Result> ConfigureClient(Guid sessionId, bool isStaging);
Task<Result> Init(Guid sessionId,Guid accountId, string description, string[] contacts, RegistrationCache? registrationCache);

View File

@ -11,7 +11,8 @@ public class Settings : DomainObjectBase {
public Settings() { }
public Result<Settings?> Initialize(string pepper) {
var userResult = new User("admin")
var userResult = new User()
.SetName("admin")
.SetPassword("password", pepper);
if (!userResult.IsSuccess || userResult.Value == null) {
@ -47,8 +48,9 @@ public class Settings : DomainObjectBase {
return Result<User?>.Ok(user);
}
public Result<Settings?> AddUser(string name, string password, string pepper) {
var setPasswordResult = new User(name)
public Result<Settings?> CreateUser(string name, string password, string pepper) {
var setPasswordResult = new User()
.SetName(name)
.SetPassword(password, pepper);
if (!setPasswordResult.IsSuccess || setPasswordResult.Value == null)
@ -75,16 +77,6 @@ public class Settings : DomainObjectBase {
return this;
}
public Result<Settings?> RemoveUser(string name) {
if (Users.Any(x => x.Name == name)) {
Users = [.. Users.Where(u => u.Name != name)];
return Result<Settings?>.Ok(this);
}
return Result<Settings?>.NotFound(null, "User not found.");
}
public Result<Settings?> RemoveUser(Guid userId) {
var user = Users.FirstOrDefault(u => u.Id == userId);
if (user == null)

View File

@ -5,21 +5,15 @@ using MaksIT.Results;
namespace MaksIT.LetsEncryptServer.Domain;
public class User(
Guid id,
string name
Guid id
) : DomainDocumentBase<Guid>(id) {
public string Name { get; private set; } = name;
public string Name { get; private set; } = string.Empty;
public string Salt { get; private set; } = string.Empty;
public string Hash { get; private set; } = string.Empty;
public List<JwtToken> JwtTokens { get; private set; } = [];
public DateTime LastLogin { get; private set; }
public User(
string name
) : this(
Guid.NewGuid(),
name
) { }
public User() : this(Guid.NewGuid()) { }
/// <summary>
/// Change user name

View File

@ -25,12 +25,11 @@ public class IdentityService(
private readonly Configuration _appSettings = appsettings.Value;
private readonly ISettingsService _settingsService = settingsService;
#region Login/Refresh/Logout
public async Task<Result<LoginResponse?>> LoginAsync(LoginRequest requestData) {
var loadSettingsResult = await _settingsService.LoadAsync();
var loadSettingsResult = await settingsService.LoadAsync();
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null) {
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
}
@ -73,7 +72,7 @@ public class IdentityService(
user.SetLastLogin();
settings.UpsertUser(user);
var saveSettingsResult = await _settingsService.SaveAsync(settings);
var saveSettingsResult = await settingsService.SaveAsync(settings);
if (!saveSettingsResult.IsSuccess)
return saveSettingsResult.ToResultOfType<LoginResponse?>(default);
@ -89,7 +88,7 @@ public class IdentityService(
}
public async Task<Result<LoginResponse?>> RefreshTokenAsync(RefreshTokenRequest requestData) {
var loadSettingsResult = await _settingsService.LoadAsync();
var loadSettingsResult = await settingsService.LoadAsync();
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null)
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
@ -109,7 +108,7 @@ public class IdentityService(
user.SetLastLogin();
settings.UpsertUser(user);
var saveResult = await _settingsService.SaveAsync(settings);
var saveResult = await settingsService.SaveAsync(settings);
if (!saveResult.IsSuccess)
return saveResult.ToResultOfType<LoginResponse?>(default);
@ -155,7 +154,7 @@ public class IdentityService(
user.SetLastLogin();
settings.UpsertUser(user);
var writeResult = await _settingsService.SaveAsync(settings);
var writeResult = await settingsService.SaveAsync(settings);
if (!writeResult.IsSuccess)
return writeResult.ToResultOfType<LoginResponse?>(default);
@ -169,7 +168,7 @@ public class IdentityService(
}
public async Task<Result> Logout(LogoutRequest requestData) {
var loadSettingsResult = await _settingsService.LoadAsync();
var loadSettingsResult = await settingsService.LoadAsync();
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null)
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);

View File

@ -43,7 +43,8 @@ public class SettingsService : ISettingsService, IDisposable {
var settings = new Settings {
Init = settingsDto.Init,
Users = [.. settingsDto.Users.Select(userDto => new User(userDto.Id, userDto.Name)
Users = [.. settingsDto.Users.Select(userDto => new User(userDto.Id)
.SetName(userDto.Name)
.SetSaltedHash(userDto.Salt, userDto.Hash)
.SetJwtTokens([.. userDto.JwtTokens.Select(jtDto =>
new JwtToken(jtDto.Id)
@ -58,7 +59,7 @@ public class SettingsService : ISettingsService, IDisposable {
catch (Exception ex) {
var message = "Error loading settings file.";
_logger.LogError(ex, message);
return Result<Settings?>.InternalServerError(null, new[] { message }.Concat(ex.ExtractMessages()).ToArray());
return Result<Settings?>.InternalServerError(null, [message, .. ex.ExtractMessages()]);
}
}