mirror of
https://github.com/MAKS-IT-COM/maksit-certs-ui.git
synced 2025-12-31 04:00:03 +01:00
(refactor): code cleanup
This commit is contained in:
parent
b80fed3245
commit
712b880ab2
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Entities.Jws;
|
namespace MaksIT.LetsEncrypt.Entities.Jws;
|
||||||
|
|
||||||
public class Jwk {
|
public class Jwk {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// "kty" (Key Type) Parameter
|
/// "kty" (Key Type) Parameter
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
using System;
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Entities.Jws;
|
namespace MaksIT.LetsEncrypt.Entities.Jws;
|
||||||
|
|
||||||
|
|
||||||
public class JwsMessage {
|
public class JwsMessage {
|
||||||
|
|
||||||
public string? Protected { get; set; }
|
public string? Protected { get; set; }
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
namespace MaksIT.LetsEncrypt.Entities {
|
namespace MaksIT.LetsEncrypt.Entities;
|
||||||
public class CachedHostname {
|
|
||||||
public string Hostname { get; set; }
|
|
||||||
public DateTime Expires { get; set; }
|
|
||||||
public bool IsUpcomingExpire { get; set; }
|
|
||||||
|
|
||||||
public bool IsDisabled { get; set; }
|
public class CachedHostname {
|
||||||
|
public string Hostname { get; set; }
|
||||||
|
public DateTime Expires { get; set; }
|
||||||
|
public bool IsUpcomingExpire { get; set; }
|
||||||
|
|
||||||
public CachedHostname(string hostname, DateTime expires, bool isUpcomingExpire, bool isDisabled) {
|
public bool IsDisabled { get; set; }
|
||||||
Hostname = hostname;
|
|
||||||
Expires = expires;
|
public CachedHostname(string hostname, DateTime expires, bool isUpcomingExpire, bool isDisabled) {
|
||||||
IsUpcomingExpire = isUpcomingExpire;
|
Hostname = hostname;
|
||||||
IsDisabled = isDisabled;
|
Expires = expires;
|
||||||
}
|
IsUpcomingExpire = isUpcomingExpire;
|
||||||
|
IsDisabled = isDisabled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,9 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
using MaksIT.LetsEncrypt.Entities.Jws;
|
using MaksIT.LetsEncrypt.Entities.Jws;
|
||||||
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Entities;
|
namespace MaksIT.LetsEncrypt.Entities;
|
||||||
|
|
||||||
public class RegistrationCache {
|
public class RegistrationCache {
|
||||||
|
|||||||
@ -1,18 +1,12 @@
|
|||||||
using System;
|
namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Entities.LetsEncrypt {
|
public enum RevokeReason {
|
||||||
public enum RevokeReason {
|
Unspecified = 0,
|
||||||
Unspecified = 0,
|
KeyCompromise = 1,
|
||||||
KeyCompromise = 1,
|
CaCompromise = 2,
|
||||||
CaCompromise = 2,
|
AffiliationChanged = 3,
|
||||||
AffiliationChanged = 3,
|
Superseded = 4,
|
||||||
Superseded = 4,
|
CessationOfOperation = 5,
|
||||||
CessationOfOperation = 5,
|
PrivilegeWithdrawn = 6,
|
||||||
PrivilegeWithdrawn = 6,
|
AaCompromise = 7
|
||||||
AaCompromise = 7
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,19 +1,15 @@
|
|||||||
using MaksIT.LetsEncrypt.Models.Responses;
|
using MaksIT.LetsEncrypt.Models.Responses;
|
||||||
using MaksIT.LetsEncrypt.Services;
|
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 bool IsStaging { get; set; }
|
|
||||||
public AcmeDirectory? Directory { get; set; }
|
public class State {
|
||||||
public JwsService? JwsService { get; set; }
|
public bool IsStaging { get; set; }
|
||||||
public Order? CurrentOrder { get; set; }
|
public AcmeDirectory? Directory { get; set; }
|
||||||
public List<AuthorizationChallengeChallenge> Challenges { get; } = new List<AuthorizationChallengeChallenge>();
|
public JwsService? JwsService { get; set; }
|
||||||
public string? Nonce { get; set; }
|
public Order? CurrentOrder { get; set; }
|
||||||
public RegistrationCache? Cache { get; set; }
|
public List<AuthorizationChallengeChallenge> Challenges { get; } = new List<AuthorizationChallengeChallenge>();
|
||||||
}
|
public string? Nonce { get; set; }
|
||||||
|
public RegistrationCache? Cache { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
using MaksIT.Core.Extensions;
|
using MaksIT.LetsEncrypt.Models.Responses;
|
||||||
using MaksIT.LetsEncrypt.Models.Responses;
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Exceptions;
|
namespace MaksIT.LetsEncrypt.Exceptions;
|
||||||
|
|
||||||
public class LetsEncrytException : Exception {
|
public class LetsEncrytException : Exception {
|
||||||
|
|
||||||
public Problem? Problem { get; }
|
public Problem? Problem { get; }
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
using MaksIT.LetsEncrypt.Services;
|
using MaksIT.LetsEncrypt.Services;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Extensions;
|
namespace MaksIT.LetsEncrypt.Extensions;
|
||||||
|
|
||||||
public static class ServiceCollectionExtensions {
|
public static class ServiceCollectionExtensions {
|
||||||
public static void RegisterLetsEncrypt(this IServiceCollection services, ILetsEncryptConfiguration appSettings) {
|
public static void RegisterLetsEncrypt(this IServiceCollection services, ILetsEncryptConfiguration appSettings) {
|
||||||
|
|
||||||
|
|||||||
@ -1,22 +1,11 @@
|
|||||||
using System;
|
namespace MaksIT.LetsEncrypt;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt {
|
|
||||||
|
|
||||||
|
|
||||||
public interface ILetsEncryptConfiguration {
|
|
||||||
string Production { get; set; }
|
|
||||||
string Staging { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public class LetsEncryptConfiguration : ILetsEncryptConfiguration {
|
|
||||||
public required string Production { get; set; }
|
|
||||||
public required string Staging { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
public interface ILetsEncryptConfiguration {
|
||||||
|
string Production { get; set; }
|
||||||
|
string Staging { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LetsEncryptConfiguration : ILetsEncryptConfiguration {
|
||||||
|
public required string Production { get; set; }
|
||||||
|
public required string Staging { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,13 +3,11 @@
|
|||||||
* https://tools.ietf.org/html/rfc4648#section-5
|
* https://tools.ietf.org/html/rfc4648#section-5
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using MaksIT.Core.Extensions;
|
||||||
using MaksIT.LetsEncrypt.Entities.Jws;
|
using MaksIT.LetsEncrypt.Entities.Jws;
|
||||||
|
|
||||||
using MaksIT.Core.Extensions;
|
|
||||||
|
|
||||||
namespace MaksIT.LetsEncrypt.Services;
|
namespace MaksIT.LetsEncrypt.Services;
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,13 @@
|
|||||||
* https://datatracker.ietf.org/doc/html/draft-ietf-acme-acme-12
|
* 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.Core.Extensions;
|
||||||
using MaksIT.LetsEncrypt.Entities;
|
using MaksIT.LetsEncrypt.Entities;
|
||||||
using MaksIT.LetsEncrypt.Entities.Jws;
|
using MaksIT.LetsEncrypt.Entities.Jws;
|
||||||
@ -12,17 +18,10 @@ using MaksIT.LetsEncrypt.Exceptions;
|
|||||||
using MaksIT.LetsEncrypt.Models.Interfaces;
|
using MaksIT.LetsEncrypt.Models.Interfaces;
|
||||||
using MaksIT.LetsEncrypt.Models.Requests;
|
using MaksIT.LetsEncrypt.Models.Requests;
|
||||||
using MaksIT.LetsEncrypt.Models.Responses;
|
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;
|
namespace MaksIT.LetsEncrypt.Services;
|
||||||
|
|
||||||
|
|
||||||
public interface ILetsEncryptService {
|
public interface ILetsEncryptService {
|
||||||
Task<Result> ConfigureClient(Guid sessionId, bool isStaging);
|
Task<Result> ConfigureClient(Guid sessionId, bool isStaging);
|
||||||
Task<Result> Init(Guid sessionId,Guid accountId, string description, string[] contacts, RegistrationCache? registrationCache);
|
Task<Result> Init(Guid sessionId,Guid accountId, string description, string[] contacts, RegistrationCache? registrationCache);
|
||||||
|
|||||||
@ -11,7 +11,8 @@ public class Settings : DomainObjectBase {
|
|||||||
public Settings() { }
|
public Settings() { }
|
||||||
|
|
||||||
public Result<Settings?> Initialize(string pepper) {
|
public Result<Settings?> Initialize(string pepper) {
|
||||||
var userResult = new User("admin")
|
var userResult = new User()
|
||||||
|
.SetName("admin")
|
||||||
.SetPassword("password", pepper);
|
.SetPassword("password", pepper);
|
||||||
|
|
||||||
if (!userResult.IsSuccess || userResult.Value == null) {
|
if (!userResult.IsSuccess || userResult.Value == null) {
|
||||||
@ -47,8 +48,9 @@ public class Settings : DomainObjectBase {
|
|||||||
return Result<User?>.Ok(user);
|
return Result<User?>.Ok(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<Settings?> AddUser(string name, string password, string pepper) {
|
public Result<Settings?> CreateUser(string name, string password, string pepper) {
|
||||||
var setPasswordResult = new User(name)
|
var setPasswordResult = new User()
|
||||||
|
.SetName(name)
|
||||||
.SetPassword(password, pepper);
|
.SetPassword(password, pepper);
|
||||||
|
|
||||||
if (!setPasswordResult.IsSuccess || setPasswordResult.Value == null)
|
if (!setPasswordResult.IsSuccess || setPasswordResult.Value == null)
|
||||||
@ -75,16 +77,6 @@ public class Settings : DomainObjectBase {
|
|||||||
return this;
|
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) {
|
public Result<Settings?> RemoveUser(Guid userId) {
|
||||||
var user = Users.FirstOrDefault(u => u.Id == userId);
|
var user = Users.FirstOrDefault(u => u.Id == userId);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
|
|||||||
@ -5,21 +5,15 @@ using MaksIT.Results;
|
|||||||
namespace MaksIT.LetsEncryptServer.Domain;
|
namespace MaksIT.LetsEncryptServer.Domain;
|
||||||
|
|
||||||
public class User(
|
public class User(
|
||||||
Guid id,
|
Guid id
|
||||||
string name
|
|
||||||
) : DomainDocumentBase<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 Salt { get; private set; } = string.Empty;
|
||||||
public string Hash { get; private set; } = string.Empty;
|
public string Hash { get; private set; } = string.Empty;
|
||||||
public List<JwtToken> JwtTokens { get; private set; } = [];
|
public List<JwtToken> JwtTokens { get; private set; } = [];
|
||||||
public DateTime LastLogin { get; private set; }
|
public DateTime LastLogin { get; private set; }
|
||||||
|
|
||||||
public User(
|
public User() : this(Guid.NewGuid()) { }
|
||||||
string name
|
|
||||||
) : this(
|
|
||||||
Guid.NewGuid(),
|
|
||||||
name
|
|
||||||
) { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Change user name
|
/// Change user name
|
||||||
|
|||||||
@ -25,12 +25,11 @@ public class IdentityService(
|
|||||||
|
|
||||||
|
|
||||||
private readonly Configuration _appSettings = appsettings.Value;
|
private readonly Configuration _appSettings = appsettings.Value;
|
||||||
private readonly ISettingsService _settingsService = settingsService;
|
|
||||||
|
|
||||||
#region Login/Refresh/Logout
|
#region Login/Refresh/Logout
|
||||||
public async Task<Result<LoginResponse?>> LoginAsync(LoginRequest requestData) {
|
public async Task<Result<LoginResponse?>> LoginAsync(LoginRequest requestData) {
|
||||||
|
|
||||||
var loadSettingsResult = await _settingsService.LoadAsync();
|
var loadSettingsResult = await settingsService.LoadAsync();
|
||||||
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null) {
|
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null) {
|
||||||
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
|
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
|
||||||
}
|
}
|
||||||
@ -73,7 +72,7 @@ public class IdentityService(
|
|||||||
user.SetLastLogin();
|
user.SetLastLogin();
|
||||||
settings.UpsertUser(user);
|
settings.UpsertUser(user);
|
||||||
|
|
||||||
var saveSettingsResult = await _settingsService.SaveAsync(settings);
|
var saveSettingsResult = await settingsService.SaveAsync(settings);
|
||||||
if (!saveSettingsResult.IsSuccess)
|
if (!saveSettingsResult.IsSuccess)
|
||||||
return saveSettingsResult.ToResultOfType<LoginResponse?>(default);
|
return saveSettingsResult.ToResultOfType<LoginResponse?>(default);
|
||||||
|
|
||||||
@ -89,7 +88,7 @@ public class IdentityService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result<LoginResponse?>> RefreshTokenAsync(RefreshTokenRequest requestData) {
|
public async Task<Result<LoginResponse?>> RefreshTokenAsync(RefreshTokenRequest requestData) {
|
||||||
var loadSettingsResult = await _settingsService.LoadAsync();
|
var loadSettingsResult = await settingsService.LoadAsync();
|
||||||
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null)
|
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null)
|
||||||
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
|
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
|
||||||
|
|
||||||
@ -109,7 +108,7 @@ public class IdentityService(
|
|||||||
user.SetLastLogin();
|
user.SetLastLogin();
|
||||||
settings.UpsertUser(user);
|
settings.UpsertUser(user);
|
||||||
|
|
||||||
var saveResult = await _settingsService.SaveAsync(settings);
|
var saveResult = await settingsService.SaveAsync(settings);
|
||||||
if (!saveResult.IsSuccess)
|
if (!saveResult.IsSuccess)
|
||||||
return saveResult.ToResultOfType<LoginResponse?>(default);
|
return saveResult.ToResultOfType<LoginResponse?>(default);
|
||||||
|
|
||||||
@ -155,7 +154,7 @@ public class IdentityService(
|
|||||||
user.SetLastLogin();
|
user.SetLastLogin();
|
||||||
settings.UpsertUser(user);
|
settings.UpsertUser(user);
|
||||||
|
|
||||||
var writeResult = await _settingsService.SaveAsync(settings);
|
var writeResult = await settingsService.SaveAsync(settings);
|
||||||
if (!writeResult.IsSuccess)
|
if (!writeResult.IsSuccess)
|
||||||
return writeResult.ToResultOfType<LoginResponse?>(default);
|
return writeResult.ToResultOfType<LoginResponse?>(default);
|
||||||
|
|
||||||
@ -169,7 +168,7 @@ public class IdentityService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result> Logout(LogoutRequest requestData) {
|
public async Task<Result> Logout(LogoutRequest requestData) {
|
||||||
var loadSettingsResult = await _settingsService.LoadAsync();
|
var loadSettingsResult = await settingsService.LoadAsync();
|
||||||
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null)
|
if (!loadSettingsResult.IsSuccess || loadSettingsResult.Value == null)
|
||||||
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
|
return loadSettingsResult.ToResultOfType<LoginResponse?>(_ => null);
|
||||||
|
|
||||||
|
|||||||
@ -43,14 +43,15 @@ public class SettingsService : ISettingsService, IDisposable {
|
|||||||
|
|
||||||
var settings = new Settings {
|
var settings = new Settings {
|
||||||
Init = settingsDto.Init,
|
Init = settingsDto.Init,
|
||||||
Users = [.. settingsDto.Users.Select(userDto => new User(userDto.Id, userDto.Name)
|
Users = [.. settingsDto.Users.Select(userDto => new User(userDto.Id)
|
||||||
.SetSaltedHash(userDto.Salt, userDto.Hash)
|
.SetName(userDto.Name)
|
||||||
.SetJwtTokens([.. userDto.JwtTokens.Select(jtDto =>
|
.SetSaltedHash(userDto.Salt, userDto.Hash)
|
||||||
new JwtToken(jtDto.Id)
|
.SetJwtTokens([.. userDto.JwtTokens.Select(jtDto =>
|
||||||
.SetAccessTokenData(jtDto.Token, jtDto.IssuedAt, jtDto.ExpiresAt)
|
new JwtToken(jtDto.Id)
|
||||||
.SetRefreshTokenData(jtDto.RefreshToken, jtDto.RefreshTokenExpiresAt)
|
.SetAccessTokenData(jtDto.Token, jtDto.IssuedAt, jtDto.ExpiresAt)
|
||||||
)])
|
.SetRefreshTokenData(jtDto.RefreshToken, jtDto.RefreshTokenExpiresAt)
|
||||||
.SetLastLogin(userDto.LastLogin)
|
)])
|
||||||
|
.SetLastLogin(userDto.LastLogin)
|
||||||
)]
|
)]
|
||||||
};
|
};
|
||||||
return Result<Settings?>.Ok(settings);
|
return Result<Settings?>.Ok(settings);
|
||||||
@ -58,7 +59,7 @@ public class SettingsService : ISettingsService, IDisposable {
|
|||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
var message = "Error loading settings file.";
|
var message = "Error loading settings file.";
|
||||||
_logger.LogError(ex, message);
|
_logger.LogError(ex, message);
|
||||||
return Result<Settings?>.InternalServerError(null, new[] { message }.Concat(ex.ExtractMessages()).ToArray());
|
return Result<Settings?>.InternalServerError(null, [message, .. ex.ExtractMessages()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user