133 Commits

Author SHA1 Message Date
Developer 02
ad8d15314f chore(Security): Description fixed 2025-01-14 20:20:25 +01:00
Developer 02
f1efbae6a4 feat(Sicherheit): Paketdefinition hinzugefügt. 2025-01-14 20:14:59 +01:00
Developer 02
051567aa0a refactor(Abstraktionen): hochgestuft auf 3.1 2025-01-14 19:51:58 +01:00
Developer 02
287871ddc6 refactor(TokenDescriptor): ApiRoute-Eigenschaft entfernt 2025-01-14 19:45:44 +01:00
Developer 02
a0ad8d732d refactor(RSATokenDescriptor): Lifetime-Eigenschaft hinzugefügt 2025-01-14 17:04:54 +01:00
Developer 02
3ad08e2a86 feat(AsymmetricTokenValidator): SecurityKey-Eigenschaft hinzugefügt. 2025-01-13 09:52:52 +01:00
Developer 02
b90a52412c feat(IAsymmetricTokenDescriptor): Methode Validator.get mit Lazy Loading hinzugefügt. 2025-01-10 23:32:53 +01:00
Developer 02
39091ff5cf refactor(IAsymmetricKey): Id nullbar gemacht.
- Benennung der perm-Datei aktualisiert.
2025-01-10 15:47:02 +01:00
Developer 02
22040cf1e7 fix(CryptoFactoryParams): Die Zuweisung des Standard-Depotnamens wurde unter das Ereignis afterCreate verschoben. 2025-01-09 23:59:58 +01:00
Developer 02
af4b7d5438 fix(CryptoFactoryParams): Aktualisiert, um den ersten Decryptor zu verwenden, um die Standard-Depot-ID zu setzen. 2025-01-09 23:39:42 +01:00
Developer 02
211064d44e refactor(RSATokenDescriptor): Standard-Id mit Issuer und Audience hinzugefügt.
- Issuer und Audience erforderlich gemacht.
2025-01-09 23:25:17 +01:00
Developer 02
66e3c771dd refactor(IAsymmetricKey): Die Implementierung von IUniqueSecurityContext wurde entfernt und stattdessen die Eigenschaft Id hinzugefügt.
- Aktualisierte verwandte Implementierungen.
2025-01-09 22:57:04 +01:00
Developer 02
97c4f7bf8f refactor(CryptographParams): umbenannt in CryptoFactoryParams 2025-01-09 22:33:56 +01:00
Developer 02
5981ba7a8d refactor(Abstractions.Security): Unnötige public Schlüsselwörter in Schnittstellen entfernt. 2025-01-09 22:27:33 +01:00
Developer 02
21e164ceb7 refactor(IAsymmetricKey): Unnötige Initor-Methoden entfernt. 2025-01-09 22:15:45 +01:00
Developer 02
1875bf46fa feat(DIExtensions): Umbenennung der AddCryptograph-Methoden in AddCryptoFactory 2025-01-09 20:38:15 +01:00
Developer 02
7f9459f6cf feat(SecurityExtensions): Erforderliche WriteToken-Methoden über Erweiterungen anstelle der Schnittstellenimplementierung hinzugefügt 2025-01-09 20:30:38 +01:00
Developer 02
079f0c69c7 fix(Kryptograph): Umbenannt in Krypto-Fabrik. 2025-01-09 20:10:45 +01:00
Developer 02
d98b3f2867 fix(JwtSignatureHandler): Aktualisierte Methoden, um IAsymmetricTokenDescriptor verwenden zu können 2025-01-09 19:54:05 +01:00
Developer 02
3761c13dba refactor(ICryptograph): Eigenschaft „TokenDescriptors“ hinzugefügt. 2025-01-09 19:34:29 +01:00
Developer 02
8acbbaeb2e refactor(IAsymmetricTokenDescriptor): Erforderliche Proportionen für SecurityTokenDescriptor-Zuordnung hinzugefügt. 2025-01-09 19:16:56 +01:00
Developer 02
60e1ec78b3 refactor(Kryptograph): Entfernte Verschlüsselungen. 2025-01-09 18:52:59 +01:00
Developer 02
e623575fe8 refactor(Kryptograph): Aktualisiert, um TokenDescriptors aus CryptographParams hinzuzufügen. 2025-01-09 18:33:51 +01:00
Developer 02
60ae8de550 refactor(CryptographParams): Aktualisierung, um TokenDeskriptoren mit Decryptoren zu initialisieren 2025-01-09 18:28:20 +01:00
Developer 02
87ad45f42a refactor(RSATokenDescriptor): Die Klasse RSA Token Descriptor wurde erstellt und die Funktionen wurden dorthin verschoben, um eine einfache und saubere Konfiguration zu ermöglichen. 2025-01-09 17:59:13 +01:00
Developer 02
2557525f06 refactor(PrivateKeyTokenDescriptor): superwarning zur Deaktivierung der Warnung vor Null 2025-01-09 11:08:06 +01:00
Developer 02
7a938f0379 refactor(Privatekey): Die Klasse encryptor wurde erstellt und die Verschlüsselungsfunktionen wurden zur einfachen und sauberen Konfiguration dorthin verschoben. 2025-01-08 20:03:25 +01:00
Developer 02
9f0facc487 refactor(Privatekey): Die Klasse decryptor wurde erstellt und die Verschlüsselungsfunktionen für eine einfache und saubere Konfiguration dorthin verschoben. 2025-01-08 18:45:36 +01:00
Developer 02
608d266d1c refactor(IAsymmetricKey): Umwandlung von RsaSecurityKey in SecurityKey zur besseren Abstraktion.
- RSAEncryptionPadding entfernen
 - Pem als Inhalt Content
2025-01-07 16:53:05 +01:00
Developer 02
34e14fd2f5 refactor(RSATokenDescriptor): In die Abstraktionsschicht verschoben und in PrivateKeyTokenDescriptor umbenannt 2025-01-07 16:34:19 +01:00
Developer 02
dc45cf2c08 refactor(JwtSignatureHandler): Aktualisiert, um RSAPrivateKey anstelle des Deskriptors zu verwenden 2025-01-07 13:55:30 +01:00
Developer 02
09a31b5a3d refactor(TokenDescription): Nach RSAKey verschoben, um unter RSAPrivateKey definiert werden zu können 2025-01-07 13:22:45 +01:00
Developer 02
b5cecac745 refactor(DIExtensions): Umbenennung der Methode AddAsymCryptHandler in AddCryptograph 2025-01-07 12:19:42 +01:00
Developer 02
0f4b5430a3 refactor(AsymCryptParams): Umbenennen in CryptographParams. 2025-01-07 12:12:50 +01:00
Developer 02
7f2d2dadfa refactor(DigitalData.Core.Security): Umbenennung des Unternamensraums von Cryptographer in RSAKey 2025-01-07 12:09:34 +01:00
Developer 02
ac0b6f739b refactor(AsymCryptHandler): Renamed to Cryptograph 2025-01-07 12:03:01 +01:00
Developer 02
d9d61368e3 refactor(IAsymCryptHandler): Umbenannt in ICryptograph 2025-01-07 12:01:39 +01:00
Developer 02
e8c98115b6 refactor(IRSAFactory): umbenannt in IAsymmetricKey 2025-01-07 11:48:27 +01:00
Developer 02
09dae1b1ac refactor(IRSAEncryptor): umbenannt in RSAPublicKey 2025-01-07 11:39:12 +01:00
Developer 02
9aafc9e467 refactor(IRSAEncryptor): umbenannt in IAsymmetricPublicKey 2025-01-07 11:33:53 +01:00
Developer 02
4ce738957d refactor(RSADecryptor): umbenennen in RSAPrivateKey 2025-01-07 11:20:24 +01:00
Developer 02
5e1bf16b6d refactor(IRSADecryptor): Umbenennung in IAsymmetricPrivateKey 2025-01-07 11:16:12 +01:00
Developer 02
4f96d271f3 refactor(IRSACryptographer): Umbenennung in IAsymmetricKey 2025-01-07 11:03:14 +01:00
Developer 02
14485af448 fix(DIExtensions): Umbenennung der Methode AddAsymCryptService in AddAsymCryptHandler 2025-01-07 10:38:10 +01:00
Developer 02
c27e21a702 fix(JwtSignatureHandler): The nullability of TokenParams has been removed. 2025-01-07 10:36:17 +01:00
Developer 02
4874079b69 fix: TokenParams-Kaliber erstellt, um Token-Beschreibungen über IOptions zu konfigurieren 2025-01-07 10:21:25 +01:00
Developer 02
15e909064f feat(IJwtSignatureHandler): Unterstützung für die Erstellung von Token durch den Routenwert der Tokenbeschreibung hinzugefügt. 2025-01-07 09:35:09 +01:00
Developer 02
d17c5ca6cd feat(JwtSignatureHandler): Unterstützung für die Erstellung von Token durch den Routenwert der Tokenbeschreibung hinzugefügt. 2025-01-07 09:30:33 +01:00
Developer 02
592b949f57 feat: Unterstützung für Token-Beschreibungen im JwtSignatureHandler hinzugefügt
- Methoden eingeführt, um Token-Beschreibungen im DI-Container zu konfigurieren und zu registrieren.
- Überladungen zu `AddJwtSignatureHandler` hinzugefügt, um sowohl konfigurationsbasierte als auch Inline-Token-Beschreibungen zu unterstützen.
2025-01-06 16:32:32 +01:00
Developer 02
8850ac4ac9 refactor(DIExtensions): Methode AddJwtSignatureHandler hinzugefügt 2025-01-06 16:08:45 +01:00
Developer 02
8ccf6f31ae refactor(JwtSignatureHandler): Umbenennung von CreateAndWriteToken in WriteToken 2025-01-06 15:35:51 +01:00
Developer 02
9875d023e3 Revert "refactor(TokenDescription): In die Abstraktionsschicht verschoben."
This reverts commit 2cf0eb3977.
2025-01-06 15:31:26 +01:00
Developer 02
62afba7c23 refactor: Entfernen redundanter Methodenüberladungen in IJwtSignatureHandler
- Entfernte doppelte Methodensignaturen für CreateToken und CreateAndWriteToken, die TokenDescription akzeptieren.
- Vereinfachte das Interface, um sich auf die wesentlichen Methoden für die Token-Erstellung und -Schreibung zu konzentrieren.
2025-01-06 15:26:31 +01:00
Developer 02
1d4882cfbc refactor(JwtSignatureService): umbenannt in JwtSignatureHandler.
- seine Schnittstelle umbenannt
2025-01-06 15:15:46 +01:00
Developer 02
275b9ec858 feat(IJwtSignatureService): Erstellt und implementiert 2025-01-06 15:06:31 +01:00
Developer 02
2cf0eb3977 refactor(TokenDescription): In die Abstraktionsschicht verschoben. 2025-01-06 14:39:45 +01:00
Developer 02
4ab5393deb refactor(JwtSignatureService): Injizieren von IAsymCryptHandler anstelle von AsymCryptHandler zur Abstraktion. 2025-01-06 13:13:57 +01:00
Developer 02
a2dc59d5ef refactor(AsymCryptParams): Entfernte TokenDescriptions 2025-01-06 13:11:05 +01:00
Developer 02
ed041bf7cb refactor(JwtSignatureService): CreateToken und CreateAndWriteToken Methoden mit Issuer und Audience Inputs hinzugefügt 2025-01-06 12:13:41 +01:00
Developer 02
c70327e7f4 refactor(IRSADecryptor): Methode CreateSigningCredentials hinzugefügt 2025-01-06 11:32:17 +01:00
Developer 02
0a3ce89c0d refactor(IRSACryptographer): RsaSecurityKey-Eigenschaft hinzugefügt 2025-01-06 11:29:19 +01:00
Developer 02
389d64c25d refactor(AsymCryptService): umbenannt in AsymCryptHandler 2025-01-06 10:44:03 +01:00
Developer 02
a3931414e3 refactor(AsymCryptService): Indexer und IEnumerable-Implementierung zur Vereinfachung entfernt 2024-12-21 10:10:50 +01:00
Developer 02
0dd897625a feat(SecurityExtensions): Die Match- und TryMatch-Erweiterungsmethoden wurden hinzugefügt, um die Funktionalität in SecurityExtensions zu erweitern. Diese Methoden vereinfachen das Matching von IUniqueSecurityContext durch direkte Verwendung eines Lookup-Kontexts. 2024-12-20 23:34:21 +01:00
Developer 02
351a6732cf refactor(SecurityExtensions): Nullbarkeit des out-Wertes in der TryGet-Methode wurde entfernt.
- Warnung ist deaktiviert.
 - Null-Prüfung in der Verwendung entfernt.
2024-12-20 22:59:21 +01:00
Developer 02
5a1808c6a6 refactor(CryptographerExtensions): Aktualisiert zur Verwendung von IUniqueSecurityContext anstelle von IRSACryptographer.
- umbenannt in SecurityExtensions.
2024-12-20 18:54:33 +01:00
Developer 02
50c42e9cdd rename(ISecurityIdentifier): Umbenannt in IUniqueSecurityContext und Kommentare zur Dokumentation hinzugefügt. 2024-12-20 14:42:07 +01:00
Developer 02
ec126be2aa feat(ISecurityIdentifier): Implementiert in IRSACryptographer und TokenDescription 2024-12-20 14:35:10 +01:00
Developer 02
9953bbd2ef feat(ISecurityIdentifier): Wird als Basisbezeichner in den Sicherheitsklassen erstellt. 2024-12-20 14:11:16 +01:00
Developer 02
dbecfa92f4 refactor(Extension): Zusammenlegung der Erweiterungsklassen zur Vereinfachung.
- Methoden und Klasse intern gemacht, um Komplexität zu vermeiden.
2024-12-20 10:48:37 +01:00
Developer 02
e007f15bce refactor(JwtSignatureService): removed primary constructor because this feature is not available in C# 11.0.
- Added GlobalSuppressions to avoid editor to offer this.
2024-12-20 10:40:35 +01:00
Developer 02
79dffef528 Refactor: Entfernung der generischen IRSAFactory und IAsymCryptService.
- RSAFactory und AsymCryptService aktualisiert.
 - Aktualisierte DI-Erweiterungen
2024-12-20 10:30:12 +01:00
Developer 02
af478e974c refactor(TokenDescriptorProvider): entfernt und eine Mapper-Erweiterungsmethode mit derselben Funktionalität zur Vereinfachung hinzugefügt. 2024-12-20 09:44:04 +01:00
Developer 02
435c91955c refactor(JwtSignatureService): verwendete primäre Struktur. 2024-12-20 09:29:49 +01:00
Developer 02
4142d2d948 refactor(TokenDescriptorProvider): verwendete Primärstruktur. 2024-12-20 09:27:08 +01:00
Developer 02
06260e0edb feat(JwtSignatureService): Erstellt mit grundlegender Funktionalität, um mit Token Beschreibung erstellen zu können. 2024-12-20 01:51:48 +01:00
Developer 02
2d675a16ad feat(TokenDescriptorProvider): Erstellt, um eine beliebige TokenDescription auf SecurityTokenDescriptor abzubilden. 2024-12-20 01:42:23 +01:00
Developer 02
5469b20e4f feat(AsymCryptParams): AfterCreate Ereignis für TokenDescriptions hinzugefügt.
- Eigenschaften SigningCredentials, SigningAlgorithm und SigningDigest zu TokenDescription hinzugefügt.
2024-12-20 01:16:56 +01:00
Developer 02
6f5b4efefb feat(ClaimDescriptor): Erstellt, um claimsMapper und subjectMapper von TPrincipal für SecurityTokenDescriptor zu registrieren.
- DI-Erweiterungsmethode erstellt.
2024-12-20 00:48:55 +01:00
Developer 02
b6b12c7702 feat(MappingProfile): Mapping-Profil hinzugefügt, um TokenDescription in SecurityTokenDescriptor über AutoMapper zu konvertieren 2024-12-20 00:11:33 +01:00
Developer 02
ce716d2bab feat(TokenDescription): Erstellt als eingeschränkte Version von SecurityTokenDescriptor als Wurzelbeschreibungen ohne Ansprüche 2024-12-20 00:08:31 +01:00
Developer 02
bf672d8b8c feat(RSADecryptor): Methode CreateSigningCredentials hinzugefügt 2024-12-19 01:53:20 +01:00
Developer 02
ed29c9f990 feat(RSACryptographer): RsaSecurityKey.get Eigenschaft mit Lazy Loading hinzugefügt 2024-12-19 01:52:00 +01:00
Developer 02
66ed34b664 refactor(StringExtensions): Fehlermeldungen wurden ausgearbeitet und der falsche Variablenname „mode“ wurde in „divisor“ umbenannt. 2024-12-18 18:19:17 +01:00
Developer 02
d7b4c382cd fix(StringExtensions): Die Hauptformel der ToTag-Methode wurde von (x / y) in [(x - 1) / y + 1] geändert, um korrekt zu gruppieren.
- Aktualisierte Dokumentationskommentare
2024-12-18 18:10:52 +01:00
Developer 02
4f6ca3524a feat(AsymCryptParams): DateTagFormat als Subtext des Dateinamens für die periodische Aktualisierung von pem-Dateien hinzugefügt. 2024-12-18 17:51:02 +01:00
Developer 02
bd1ae4246d refactor(Extensoin): entfernt, um die Projektanzahl zu minimieren. 2024-12-18 14:09:53 +01:00
Developer 02
d92475c230 feat: Indexer hinzugefügt, um RSA-Dechiffrierer über den Index im AsymCryptService zuzugreifen
- Neuer Indexer eingeführt, um den Zugriff auf Dechiffrierer über den Index zu ermöglichen und so flexibleren Zugriff auf Elemente in der Decryptors-Sammlung zu bieten.
- Validierung der Indexgrenzen hinzugefügt, um eine ArgumentOutOfRangeException zu werfen, wenn ein ungültiger Index angegeben wird.
- Bestehende Funktionalität zum Zugriff auf Dechiffrierer über den Schlüssel beibehalten.
2024-12-18 14:05:31 +01:00
Developer 02
15705cccc4 feat(AsymCryptParams): Schlüsselgröße zum Parameter Dateiname hinzugefügt 2024-12-18 13:45:51 +01:00
Developer 02
a8403087f6 feat(DIExtensions): Die Methode AddCryptographerConverter wurde entfernt. 2024-12-18 13:17:18 +01:00
Developer 02
0235c83075 feat(RSAFactoryParams): Umbenennung von PbeHashAlgorithmName in PbeHashAlgorithm und Hinzufügen von String-Proportionen mit dem Namen von PbeHashAlgorithmName.
- PbeHashAlgorithmName.init Methoden hinzugefügt, um zu versuchen, PbeHashAlgorithm mit Reflection zu initialisieren. Wenn er null ist, wird er nach dem Namen generiert.
- PbeHashAlgorithmName.get hinzugefügt, um den Namen von PbeHashAlgorithm zu erhalten.
2024-12-18 13:04:15 +01:00
Developer 02
63aeba982f feat(RSACryptographer): Eigenschaft PaddingName hinzugefügt, um Padding mit dem Namen RSAEncryptionPadding zu initialisieren. 2024-12-18 11:36:45 +01:00
Developer 02
514495fc8d refactor: Aktualisierung der String-Verschlüsselungsmethoden in RSAEncryptor
- `Encrypt`-Methode überarbeitet, um `ToBytes` und `ToBase64String` für Konsistenz und bessere Lesbarkeit zu verwenden.
- Parametername in der Methode `Encrypt(string)` von `data` zu `strData` geändert, um die Verständlichkeit zu verbessern.
2024-12-17 20:49:34 +01:00
Developer 02
9752fb14ec fix: Unterdrückung der Nullable-Warnung für den Pem-Getter in RSADecryptor
- CS8603-Warnung für den `Pem`-Getter in `RSADecryptor` mit `#pragma warning disable/restore` unterdrückt.
- Konsistentes Verhalten sichergestellt, ohne die Laufzeitlogik zu ändern.
2024-12-17 15:29:28 +01:00
Developer 02
b3629661a1 fix(AsymCryptParams): Dateinamenerweiterung hinzugefügt. 2024-12-17 14:06:09 +01:00
Developer 02
f38bad8531 refactor(gitignore): ignored Tests.API 2024-12-16 18:07:47 +01:00
Developer 02
154478c318 feat(ParamsConfigureOptions): Erstellt, um nach der Konfiguration über appsettings initialisiert zu werden.
- DI Extension Methoden wurden entsprechend bearbeitet.
2024-12-16 17:20:40 +01:00
Developer 02
155eb563d1 feat: Lazy-Initialisierung für threadsichere RSAFactoryParams-Initialisierung hinzugefügt
- Lazy-Initialisierungsmechanismus für threadsichere Handhabung der _pbeParameters eingeführt.
- IsInitialized-Eigenschaft hinzugefügt, um den Initialisierungsstatus zu verfolgen.
- Konstruktor geändert, um das Lazy-Objekt zu initialisieren und das AfterCreate-Ereignis auszulösen.
- Sichergestellt, dass die OnDeserialized-Methode Init aufruft, um das Objekt korrekt zu initialisieren.
2024-12-16 16:37:48 +01:00
Developer 02
4aacc3f650 feat(AsymCryptService): Vault.get RSADecryptor hinzugefügt
- Optionen aktualisiert, um Vault-Parameter hinzufügen zu können. Wenn es null ist, ist Vault der erste Entschlüsseler.
 - Standard-Entschlüssler entfernt.
2024-12-16 12:56:30 +01:00
Developer 02
f40c86ed63 feat: Erweiterung des IAsymCryptService-Interfaces um zusätzliche Verschlüsselungs- und Entschlüsselungsfunktionen
- Hinzugefügt: `IEnumerable<IRSADecryptor>` und `IEnumerable<IRSAEncryptor>` zum `IAsymCryptService`-Interface.
- Einführung einer `Default`-Eigenschaft für den einfachen Zugriff auf einen Standard-Entschlüsseler.
- Aktualisierung des `IAsymCryptService`-Interfaces zur Unterstützung sowohl von Entschlüsselungs- als auch Verschlüsselungsfunktionen.
2024-12-16 11:41:52 +01:00
Developer 02
b32f0df125 refactor(AsymCryptService): Methode Default.get hinzugefügt, um den ersten Decryptor zu erhalten.
- Wirft InvalidOperationException, wenn kein Decryptor verfügbar ist.
2024-12-16 10:54:48 +01:00
Developer 02
324a5bdb1e refactor(RSAFactory): isEncrypted-Eingang von CreateDecryptor umbenennen in encrypt. 2024-12-16 10:51:46 +01:00
Developer 02
e0a6787a87 feat(RSAFactory): Unterstützung für die Erstellung von RSA-Decryptors hinzugefügt
- Methode `CreateDecryptor` hinzugefügt, um die Erstellung von `IRSADecryptor`-Instanzen zu vereinfachen.
- Stellt sicher, dass Decryptors mit PEM, Aussteller, Empfänger, Verschlüsselungsstatus und Padding-Einstellungen initialisiert werden.
- Bestehende Funktionalität zur Erstellung privater und verschlüsselter privater Schlüssel beibehalten.
- Die RSA-Factory verbessert, um Workflows zur Entschlüsselung besser zu unterstützen.
2024-12-16 10:37:56 +01:00
Developer 02
c6a4038eab refactor: Umbenennung von Core.Security.Extensions in Core.Extension 2024-12-16 10:24:19 +01:00
Developer 02
58c8520c08 refactor(RSADecryptor): Verbesserung der PEM-Initialisierung und Konsistenz
- Die `Pem`-Eigenschaft aktualisiert, sodass während der Initialisierung automatisch `Init()` aufgerufen wird, um eine konsistente Einrichtung sicherzustellen.
- Die Methode `SetPem` überarbeitet, um nach dem Setzen des PEM-Werts `Init()` aufzurufen.
- Die Methode `Init()` verbessert, um null- oder leere PEM-Werte robuster zu behandeln.
- Fehlermeldungen für mehr Klarheit und bessere Debugging-Unterstützung verbessert.
- Interne RSA-Initialisierungslogik an die Verarbeitung von Verschlüsselungen angepasst.
2024-12-16 10:05:51 +01:00
Developer 02
eced1a5afc feat(RSAFactory): Optionale Verschlüsselungseingabe in CreatePrivateKeyPem Methode hinzugefügt.
- Falsch als Standard eingestellt.
2024-12-16 10:01:09 +01:00
Developer 02
7da93c6719 refactor(DIExtensions): Verbesserung der Registrierung von AsymCrypt-Diensten und Vereinfachung von Overloads
- `AddAsymCryptService` aktualisiert, um eine Standardimplementierung mit `AsymCryptParams` ohne generische Typen bereitzustellen.
- Neue Überladung von `AddAsymCryptService` hinzugefügt, die eine `IConfigurationSection` für Standardparameter akzeptiert.
- Lebensdauer der Service-Registrierungen für `IAsymCryptService` von `Scoped` auf `Singleton` geändert, um Konsistenz und geringeren Overhead zu gewährleisten.
2024-12-16 09:44:51 +01:00
Developer 02
6a92466490 feat: hinzugefügte Index-Eigenschaft zur IAsymCryptService-Schnittstelle
- Neue Index-Eigenschaft `this[string key]` zur `IAsymCryptService`-Schnittstelle hinzugefügt.
- Ermöglicht das Abrufen spezifischer `IRSADecryptor`-Instanzen anhand eines Schlüsselstrings.
- Schnittstellendefinition aktualisiert, um die Funktionalität für implementierende Klassen zu erweitern.
2024-12-13 16:59:02 +01:00
Developer 02
5d9d756b91 feat: hinzugefügte Index-Eigenschaft zur Abfrage eines spezifischen IRSADecryptor anhand eines Schlüssels
- Neue Index-Eigenschaft `this[string key]` in `AsymCryptService` eingeführt, um spezifische `IRSADecryptor`-Instanzen basierend auf Issuer- und Audience-Schlüsseln abzurufen.
- Validierung des Schlüsselformats und Fehlerbehandlung für Fälle hinzugefügt, in denen kein passender Decryptor gefunden wird.
- Implementierung aktualisiert, um die Kompatibilität mit der bestehenden Decryptor-Enumerationslogik sicherzustellen.
2024-12-13 16:57:30 +01:00
Developer 02
f14aaa75e1 refactor(AsymCryptParams): Umbenennung von Separator in FileNameSeparator.
- KeyNameSeparator hinzugefügt.
2024-12-13 16:40:00 +01:00
Developer 02
249f5a0ae5 feat(AsymCryptService): Encryptors.get hinzugefügt, um die Verschlüsseler der Entschlüsseler zu numerieren. 2024-12-13 16:34:53 +01:00
Developer 02
30177cf0c7 feat(AsymCryptService): Implementiert IEnumerable<IRSADecryptor> 2024-12-13 16:25:21 +01:00
Developer 02
68ef0a7537 refactor(RSACryptographer): Entfernen von _pem, IsPemNull, SetPem, Init und Methoden zur Vereinfachung von RSAEncryptor 2024-12-13 16:17:35 +01:00
Developer 02
fe2ee78d14 refactor(RSADecryptor): Umbenennung der Eigenschaft Encrypt in IsEncrypted 2024-12-13 16:02:25 +01:00
Developer 02
53e6f37a09 refactor(AsymCryptParams): Umbenennung von crypt in der Schleife in decryptor 2024-12-13 15:59:28 +01:00
Developer 02
7ec85b4e30 refactor(AsymCryptParams): Unnötige Methoden entfernt 2024-12-13 15:57:17 +01:00
Developer 02
a9ebc406f3 refactor(RSAFactory): Methode CreateEncryptedPrivateKeyPem hinzugefügt, um mit direkt benutzerdefinierten pbeParametern zu erstellen.
- Umbenennung der Methode CreateRSAPrivateKeyPem in CreatePrivateKeyPem
2024-12-13 15:45:09 +01:00
Developer 02
d013d3edfa refactor(AsymCryptService): Verschlüsselungen entfernen, da sie von Entschlüsselungen erzeugt werden müssen. 2024-12-13 15:38:50 +01:00
Developer 02
f267fe955b feat(AsymCryptParams): Funktionalität erstellt, um pem aus der Datei zu setzen, wenn diese null ist.
- Wenn die pem Datei nicht existiert, erstellt
2024-12-13 15:29:42 +01:00
Developer 02
644283cf8f refactor(AsymCryptService): Nicht-generische Schnittstelle erstellt.
- Geordnete DI-Registrierungsmethoden.
2024-12-13 14:44:09 +01:00
Developer 02
82aa8d1143 refactor(DIExtensions): Alle TryAddSingelton wurden in AddSingelton umgewandelt, um im Falle einer falschen Ausnahme den Fehler zu protokollieren.
- SetAsDefault-Parameter hinzugefügt, um nicht-generische IRSAFactory im Falle einer Konfiguration über appsettings registrieren zu können.
2024-12-13 14:00:43 +01:00
Developer 02
7459f05748 refactor(RSAFactory): Schaffung einer nicht-generischen, getrennten Schnittstelle, um eine statische Standardinstanz erstellen zu können.
- Statische Instanzklasse erstellt.
 - Geordnete DI-Registrierungsmethoden.
2024-12-13 13:47:44 +01:00
Developer 02
36f75d003a refactor(AsymCryptParams): Umbenennung von Directory in PemDirectory, um Namenskonflikte zu vermeiden. 2024-12-13 11:11:12 +01:00
Developer 02
76ce64691a refactor(RSACryptographer): Verzeichnis und Dateiname wurden entfernt.
- Datei-Leseprozess in init-Methode entfernt.
2024-12-13 10:29:49 +01:00
Developer 02
7c03282066 refactor(RSACryptographer): Interne Methoden zur Konfiguration von RSACryptographen hinzugefügt.
- IsPemNull.get-Methode hinzugefügt, um zu prüfen, ob _pem null ist.
 - SetPem-Methode hinzugefügt, um pem im Projekt aktualisieren zu können.
2024-12-13 10:15:22 +01:00
Developer 02
7ae95b729f Fix: Upgrade aufgrund einer Nuget-Paket-Abhängigkeit der abstrakten Schicht 2024-12-10 23:32:49 +01:00
Developer 02
9ee8a51664 chore(Abstraktionen): Aktualisiert von 2.2.1 auf 3.0.0 2024-12-10 23:28:34 +01:00
Developer 02
b1d1a898b8 chore(API): Aktualisiert von 2.0.1 auf 2.1 2024-12-10 23:20:40 +01:00
Developer 02
4ed3e79565 chore(Anwendung): Hochgestuft von 2 auf 3 2024-12-10 23:19:27 +01:00
Developer 02
8d9de4502e refactor(Application): Verbesserung der CRUDService zur Steigerung der Typflexibilität und Wartbarkeit
- Entfernen redundanter generischer Einschränkungen für `TUpdateDto` in `CRUDService`.
- Aktualisierung der Methode `UpdateAsync`, um einen generischen Parameter für eine bessere Typflexibilität von `TUpdateDto` einzuschließen.
- Verbesserung der Typ-Einschränkungen durch Durchsetzung von `IUnique<TId>` direkt im Methodenumfang, wo zutreffend.
- Vereinfachung der Klasse zur Einhaltung bewährter Praktiken im Design generischer Dienste.
2024-12-10 23:17:41 +01:00
Developer 02
7dd91c73c4 Merge branch 'feat/client' 2024-12-10 23:03:01 +01:00
Developer 02
65c64a3f9a chore(API): Aktualisiert auf 2.0.1 2024-12-02 11:35:33 +01:00
Developer 02
1d600aa453 refactor: Nullbarkeitsannotation zu ControllerExtensions-Methoden hinzugefügt 2024-12-02 11:33:52 +01:00
62 changed files with 1073 additions and 534 deletions

1
.gitignore vendored
View File

@@ -410,3 +410,4 @@ FodyWeavers.xsd
/DigitalData.Core.ConsoleApp/FooHttpOptions.cs /DigitalData.Core.ConsoleApp/FooHttpOptions.cs
/DigitalData.Core.Tests/obj/ /DigitalData.Core.Tests/obj/
/DigitalData.Core.Terminal /DigitalData.Core.Terminal
/DigitalData.Core.Tests.API

View File

@@ -7,7 +7,7 @@ namespace DigitalData.Core.API
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class BasicCRUDControllerBase<TCRUDService, TDto, TEntity, TId> : CRUDControllerBase<TCRUDService, TDto, TDto, TDto, TEntity, TId> public class BasicCRUDControllerBase<TCRUDService, TDto, TEntity, TId> : CRUDControllerBase<TCRUDService, TDto, TDto, TDto, TEntity, TId>
where TCRUDService : ICRUDService<TDto, TDto, TDto, TEntity, TId> where TCRUDService : ICRUDService<TDto, TDto, TEntity, TId>
where TDto : class, IUnique<TId> where TDto : class, IUnique<TId>
where TEntity : class, IUnique<TId> where TEntity : class, IUnique<TId>
{ {

View File

@@ -11,13 +11,12 @@ namespace DigitalData.Core.API
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam> /// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam> /// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam> /// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
/// <typeparam name="TUpdateDto">The Data Transfer Object type for update operations.</typeparam>
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam> /// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
/// <typeparam name="TId">The type of the entity's identifier.</typeparam> /// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class CRUDControllerBase<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase public class CRUDControllerBase<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId> where TCRUDService : ICRUDService<TCreateDto, TReadDto, TEntity, TId>
where TCreateDto : class where TCreateDto : class
where TReadDto : class where TReadDto : class
where TUpdateDto : class, IUnique<TId> where TUpdateDto : class, IUnique<TId>

View File

@@ -12,13 +12,12 @@ namespace DigitalData.Core.API
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam> /// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam> /// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam> /// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
/// <typeparam name="TUpdateDto">The Data Transfer Object type for update operations.</typeparam>
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam> /// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
/// <typeparam name="TId">The type of the entity's identifier.</typeparam> /// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase public class CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId> where TCRUDService : ICRUDService<TCreateDto, TReadDto, TEntity, TId>
where TCreateDto : class where TCreateDto : class
where TReadDto : class where TReadDto : class
where TUpdateDto : class, IUnique<TId> where TUpdateDto : class, IUnique<TId>

View File

@@ -19,7 +19,7 @@ namespace DigitalData.Core.API
/// <param name="index">The key in the ViewData dictionary where the value will be stored.</param> /// <param name="index">The key in the ViewData dictionary where the value will be stored.</param>
/// <param name="value">The value to be stored in the ViewData dictionary.</param> /// <param name="value">The value to be stored in the ViewData dictionary.</param>
/// <returns>The same ViewResult object with updated ViewData, allowing for additional chained operations.</returns> /// <returns>The same ViewResult object with updated ViewData, allowing for additional chained operations.</returns>
public static ViewResult WithData(this ViewResult viewResult, string index, object value) public static ViewResult WithData(this ViewResult viewResult, string index, object? value)
{ {
viewResult.ViewData[index] = value; viewResult.ViewData[index] = value;
return viewResult; return viewResult;

View File

@@ -8,7 +8,7 @@
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<Description>This package provides a comprehensive set of API controllers and related utilities for the DigitalData.Core library. It includes generic CRUD controllers, localization extensions, middleware for security policies, and application model conventions.</Description> <Description>This package provides a comprehensive set of API controllers and related utilities for the DigitalData.Core library. It includes generic CRUD controllers, localization extensions, middleware for security policies, and application model conventions.</Description>
<PackageId>DigitalData.Core.API</PackageId> <PackageId>DigitalData.Core.API</PackageId>
<Version>2.0.0.0</Version> <Version>2.1.1</Version>
<Authors>Digital Data GmbH</Authors> <Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company> <Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.API</Product> <Product>DigitalData.Core.API</Product>
@@ -16,6 +16,8 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core api</PackageTags> <PackageTags>digital data core api</PackageTags>
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<AssemblyVersion>2.1.1</AssemblyVersion>
<FileVersion>2.1.1</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -15,7 +15,7 @@ namespace DigitalData.Core.Abstractions.Application
/// This interface is useful for entities that do not require different DTOs for different operations, /// This interface is useful for entities that do not require different DTOs for different operations,
/// allowing for a more concise and maintainable codebase when implementing services for such entities. /// allowing for a more concise and maintainable codebase when implementing services for such entities.
/// </remarks> /// </remarks>
public interface IBasicCRUDService<TDto, TEntity, TId> : ICRUDService<TDto, TDto, TDto, TEntity, TId> public interface IBasicCRUDService<TDto, TEntity, TId> : ICRUDService<TDto, TDto, TEntity, TId>
where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId> where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId>
{ {
} }

View File

@@ -2,8 +2,8 @@
namespace DigitalData.Core.Abstractions.Application namespace DigitalData.Core.Abstractions.Application
{ {
public interface ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : IReadService<TReadDto, TEntity, TId> public interface ICRUDService<TCreateDto, TReadDto, TEntity, TId> : IReadService<TReadDto, TEntity, TId>
where TCreateDto : class where TReadDto : class where TUpdateDto : IUnique<TId> where TEntity : class, IUnique<TId> where TCreateDto : class where TReadDto : class where TEntity : class, IUnique<TId>
{ {
/// <summary> /// <summary>
/// Asynchronously creates a new entity based on the provided <paramref name="createDto"/> and returns the identifier of the created entity wrapped in a <see cref="DataResult{TId}"/>. /// Asynchronously creates a new entity based on the provided <paramref name="createDto"/> and returns the identifier of the created entity wrapped in a <see cref="DataResult{TId}"/>.
@@ -20,6 +20,6 @@ namespace DigitalData.Core.Abstractions.Application
/// </summary> /// </summary>
/// <param name="updateDto">The updateDTO with updated values for the entity.</param> /// <param name="updateDto">The updateDTO with updated values for the entity.</param>
/// <returns>An Result indicating the outcome of the update operation, with an appropriate message.</returns> /// <returns>An Result indicating the outcome of the update operation, with an appropriate message.</returns>
Task<Result> UpdateAsync(TUpdateDto updateDto); Task<Result> UpdateAsync<TUpdateDto>(TUpdateDto updateDto) where TUpdateDto : IUnique<TId>;
} }
} }

View File

@@ -9,7 +9,7 @@
<Authors>Digital Data GmbH</Authors> <Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company> <Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.Abstractions</Product> <Product>DigitalData.Core.Abstractions</Product>
<Description>This package contains abstractions for the DigitalData.Core.Abstractions library, developed according to the principles of Clean Architecture. It promotes separation of concerns and enables independent core logic.</Description> <Description>This package contains abstractions for the DigitalData.Core library, developed according to the principles of Clean Architecture. It promotes separation of concerns and enables independent core logic.</Description>
<PackageTags>digital data core abstractions clean architecture</PackageTags> <PackageTags>digital data core abstractions clean architecture</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Copyright>Copyright 2024</Copyright> <Copyright>Copyright 2024</Copyright>
@@ -17,9 +17,9 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackAsTool>False</PackAsTool> <PackAsTool>False</PackAsTool>
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<Version>2.2.1</Version> <Version>3.1.0</Version>
<AssemblyVersion>2.2.1</AssemblyVersion> <AssemblyVersion>3.1.0</AssemblyVersion>
<FileVersion>2.2.1</FileVersion> <FileVersion>3.1.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,21 +0,0 @@
namespace DigitalData.Core.Abstractions.Security
{
public static class CryptographerExtensions
{
public static IEnumerable<TRSACryptographer> GetByIssuer<TRSACryptographer>(this IEnumerable<TRSACryptographer> cryptographers, string issuer) where TRSACryptographer: IRSACryptographer
=> cryptographers.Where(c => c.Issuer == issuer);
public static IEnumerable<TRSACryptographer> GetByAudience<TRSACryptographer>(this IEnumerable<TRSACryptographer> cryptographers, string audience) where TRSACryptographer : IRSACryptographer
=> cryptographers.Where(c => c.Audience == audience);
public static TRSACryptographer Get<TRSACryptographer>(this IEnumerable<TRSACryptographer> cryptographers, string issuer, string audience) where TRSACryptographer : IRSACryptographer
=> cryptographers.Where(c => c.Issuer == issuer && c.Audience == audience).SingleOrDefault()
?? throw new InvalidOperationException($"No {typeof(TRSACryptographer).GetType().Name.TrimStart('I')} found with Issuer: {issuer} and Audience: {audience}.");
public static bool TryGet<TRSACryptographer>(this IEnumerable<TRSACryptographer> cryptographers, string issuer, string audience, out TRSACryptographer? cryptographer) where TRSACryptographer : IRSACryptographer
{
cryptographer = cryptographers.SingleOrDefault(c => c.Issuer == issuer && c.Audience == audience);
return cryptographer is not null;
}
}
}

View File

@@ -1,9 +0,0 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymCryptService<TParams> : IRSAFactory<TParams>
{
public IEnumerable<IRSADecryptor> Decryptors { get; }
public IEnumerable<IRSAEncryptor> Encryptors { get; }
}
}

View File

@@ -0,0 +1,9 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricDecryptor : IAsymmetricPrivateKey
{
byte[] Decrypt(byte[] data);
IAsymmetricEncryptor Encryptor { get; }
}
}

View File

@@ -0,0 +1,7 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricEncryptor : IAsymmetricPublicKey
{
byte[] Encrypt(byte[] data);
}
}

View File

@@ -0,0 +1,9 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricKey
{
string? Id { get; }
string Content { get; }
}
}

View File

@@ -0,0 +1,23 @@
using System.Security.Cryptography;
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricKeyFactory
{
string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false);
string CreateEncryptedPrivateKeyPem(
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null,
int? keySizeInBits = null,
string? password = null);
string CreateEncryptedPrivateKeyPem(
PbeParameters pbeParameters,
int? keySizeInBits = null,
string? password = null);
IAsymmetricDecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null);
}
}

View File

@@ -0,0 +1,9 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricPrivateKey : IAsymmetricKey
{
bool IsEncrypted { get; }
IAsymmetricPublicKey PublicKey { get; }
}
}

View File

@@ -0,0 +1,6 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricPublicKey : IAsymmetricKey
{
}
}

View File

@@ -0,0 +1,74 @@
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Abstractions.Security
{
/// <summary>
/// Contains some information which used to create a security token. Designed to abstract <see cref="SecurityTokenDescriptor"/>
/// </summary>
public interface IAsymmetricTokenDescriptor : IAsymmetricPrivateKey, IUniqueSecurityContext
{
IAsymmetricTokenValidator Validator { get; }
TimeSpan Lifetime { get; init; }
#region SecurityTokenDescriptor Map
/// <summary>
/// Defines the compression algorithm that will be used to compress the JWT token payload.
/// </summary>
string CompressionAlgorithm { get; }
/// <summary>
/// Gets or sets the <see cref="EncryptingCredentials"/> used to create a encrypted security token.
/// </summary>
EncryptingCredentials EncryptingCredentials { get; }
/// <summary>
/// Gets or sets the value of the 'expiration' claim. This value should be in UTC.
/// </summary>
DateTime? Expires { get; }
/// <summary>
/// Gets or sets the time the security token was issued. This value should be in UTC.
/// </summary>
DateTime? IssuedAt { get; }
/// <summary>
/// Gets or sets the notbefore time for the security token. This value should be in UTC.
/// </summary>
DateTime? NotBefore { get; }
/// <summary>
/// Gets or sets the token type.
/// <remarks> If provided, this will be added as the value for the 'typ' header parameter. In the case of a JWE, this will be added to both the inner (JWS) and the outer token (JWE) header. By default, the value used is 'JWT'.
/// If <see cref="AdditionalHeaderClaims"/> also contains 'typ' header claim value, it will override the TokenType provided here.
/// This value is used only for JWT tokens and not for SAML/SAML2 tokens</remarks>
/// </summary>
string TokenType { get; }
/// <summary>
/// Gets or sets the <see cref="Dictionary{TKey, TValue}"/> which contains any custom header claims that need to be added to the JWT token header.
/// The 'alg', 'kid', 'x5t', 'enc', and 'zip' claims are added by default based on the <see cref="SigningCredentials"/>,
/// <see cref="EncryptingCredentials"/>, and/or <see cref="CompressionAlgorithm"/> provided and SHOULD NOT be included in this dictionary as this
/// will result in an exception being thrown.
/// <remarks> These claims are only added to the outer header (in case of a JWE).</remarks>
/// </summary>
IDictionary<string, object> AdditionalHeaderClaims { get; }
/// <summary>
/// Gets or sets the <see cref="Dictionary{TKey, TValue}"/> which contains any custom header claims that need to be added to the inner JWT token header.
/// The 'alg', 'kid', 'x5t', 'enc', and 'zip' claims are added by default based on the <see cref="SigningCredentials"/>,
/// <see cref="EncryptingCredentials"/>, and/or <see cref="CompressionAlgorithm"/> provided and SHOULD NOT be included in this dictionary as this
/// will result in an exception being thrown.
/// <remarks>
/// For JsonWebTokenHandler, these claims are merged with <see cref="AdditionalHeaderClaims"/> while adding to the inner JWT header.
/// </remarks>
/// </summary>
IDictionary<string, object> AdditionalInnerHeaderClaims { get; }
/// <summary>
/// Gets or sets the <see cref="SigningCredentials"/> used to create a security token.
/// </summary>
SigningCredentials SigningCredentials { get; }
#endregion SecurityTokenDescriptor
}
}

View File

@@ -0,0 +1,9 @@
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricTokenValidator : IAsymmetricPublicKey
{
SecurityKey SecurityKey { get; }
}
}

View File

@@ -0,0 +1,11 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface ICryptoFactory : IAsymmetricKeyFactory
{
IEnumerable<IAsymmetricDecryptor> Decryptors { get; }
IAsymmetricDecryptor VaultDecryptor { get; }
IEnumerable<IAsymmetricTokenDescriptor> TokenDescriptors { get; }
}
}

View File

@@ -0,0 +1,15 @@
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Abstractions.Security
{
public interface IJwtSignatureHandler<TPrincipal>
{
SecurityToken CreateToken(SecurityTokenDescriptor tokenDescriptor);
SecurityToken CreateToken(TPrincipal subject, IAsymmetricTokenDescriptor descriptor);
SecurityToken CreateToken(TPrincipal subject, string issuer, string audience);
string WriteToken(SecurityToken token);
}
}

View File

@@ -1,21 +0,0 @@
using System.Security.Cryptography;
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSACryptographer
{
public string Pem { get; init; }
public RSAEncryptionPadding Padding { get; init; }
public string? Directory { get; set; }
public string? FileName { get; set; }
public string Issuer { get; init; }
public string Audience { get; init; }
public void Init();
}
}

View File

@@ -1,13 +0,0 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSADecryptor : IRSACryptographer
{
public bool Encrypt { get; init; }
IRSAEncryptor Encryptor { get; }
byte[] Decrypt(byte[] data);
string Decrypt(string data);
}
}

View File

@@ -1,11 +0,0 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSAEncryptor : IRSACryptographer
{
public byte[] Encrypt(byte[] data);
public string Encrypt(string data);
public bool Verify(string data, string signature) => Encrypt(data) == signature;
}
}

View File

@@ -1,16 +0,0 @@
using System.Security.Cryptography;
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSAFactory<TParams>
{
string CreateRSAPrivateKeyPem(int? keySizeInBits = null);
string CreateEncryptedPrivateKeyPem(
int? keySizeInBits = null,
string? password = null,
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null);
}
}

View File

@@ -0,0 +1,24 @@
namespace DigitalData.Core.Abstractions.Security
{
/// <summary>
/// Represents a unique security context that identifies an issuer and an audience.
/// </summary>
public interface IUniqueSecurityContext
{
/// <summary>
/// Gets the issuer identifier for this security context.
/// </summary>
/// <remarks>
/// The issuer typically represents the entity that issues a token or a cryptographic key.
/// </remarks>
string Issuer { get; }
/// <summary>
/// Gets the audience identifier for this security context.
/// </summary>
/// <remarks>
/// The audience typically represents the intended recipient or target of a token or cryptographic operation.
/// </remarks>
string Audience { get; }
}
}

View File

@@ -0,0 +1,62 @@
using Microsoft.IdentityModel.Tokens;
using System.Text;
namespace DigitalData.Core.Abstractions.Security
{
public static class SecurityExtensions
{
#region Unique Security Context
public static IEnumerable<TUniqueSecurityContext> GetByIssuer<TUniqueSecurityContext>(this IEnumerable<TUniqueSecurityContext> contextes, string issuer) where TUniqueSecurityContext: IUniqueSecurityContext
=> contextes.Where(c => c.Issuer == issuer);
public static IEnumerable<TUniqueSecurityContext> GetByAudience<TUniqueSecurityContext>(this IEnumerable<TUniqueSecurityContext> contextes, string audience) where TUniqueSecurityContext : IUniqueSecurityContext
=> contextes.Where(c => c.Audience == audience);
public static TUniqueSecurityContext Get<TUniqueSecurityContext>(this IEnumerable<TUniqueSecurityContext> contextes, string issuer, string audience) where TUniqueSecurityContext : IUniqueSecurityContext
=> contextes.Where(c => c.Issuer == issuer && c.Audience == audience).SingleOrDefault()
?? throw new InvalidOperationException($"Exactly one {typeof(TUniqueSecurityContext).Name} must exist with Issuer: '{issuer}' and Audience: '{audience}'.");
public static bool TryGet<TUniqueSecurityContext>(this IEnumerable<TUniqueSecurityContext> contextes, string issuer, string audience, out TUniqueSecurityContext context) where TUniqueSecurityContext : IUniqueSecurityContext
{
#pragma warning disable CS8601 // Possible null reference assignment.
context = contextes.SingleOrDefault(c => c.Issuer == issuer && c.Audience == audience);
#pragma warning restore CS8601 // Possible null reference assignment.
return context is not null;
}
public static TUniqueSecurityContext Match<TUniqueSecurityContext>(this IEnumerable<TUniqueSecurityContext> contextes, IUniqueSecurityContext lookupContext) where TUniqueSecurityContext : IUniqueSecurityContext
=> contextes.Get(lookupContext.Issuer, lookupContext.Audience);
public static bool TryMatch<TUniqueSecurityContext>(this IEnumerable<TUniqueSecurityContext> contextes, IUniqueSecurityContext lookupContext, out TUniqueSecurityContext context) where TUniqueSecurityContext : IUniqueSecurityContext
=> contextes.TryGet(lookupContext.Issuer, lookupContext.Audience, out context);
#endregion Unique Security Context
#region De/serilization
internal static byte[] Base64ToByte(this string base64String) => Convert.FromBase64String(base64String);
internal static string BytesToString(this byte[] bytes) => Encoding.UTF8.GetString(bytes);
internal static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
internal static byte[] ToBytes(this string str) => System.Text.Encoding.UTF8.GetBytes(str);
public static string Decrypt(this IAsymmetricDecryptor decryptor, string data) => decryptor
.Decrypt(data.Base64ToByte()).BytesToString();
#endregion De/serilization
#region Asymmetric Encryptor
public static string Encrypt(this IAsymmetricEncryptor encryptor, string data) => encryptor.Encrypt(data.ToBytes()).ToBase64String();
#endregion Asymmetric Encryptor
#region Jwt Signature Handler
public static string WriteToken<TPrincipal>(this IJwtSignatureHandler<TPrincipal> handler, SecurityTokenDescriptor descriptor)
=> handler.WriteToken(handler.CreateToken(descriptor));
public static string WriteToken<TPrincipal>(this IJwtSignatureHandler<TPrincipal> handler, TPrincipal subject, IAsymmetricTokenDescriptor descriptor)
=> handler.WriteToken(handler.CreateToken(subject: subject, descriptor: descriptor));
public static string WriteToken<TPrincipal>(this IJwtSignatureHandler<TPrincipal> handler, TPrincipal subject, string issuer, string audience)
=> handler.WriteToken(handler.CreateToken(subject: subject, issuer: issuer, audience: audience));
#endregion Jwt Signature Handler
}
}

View File

@@ -19,7 +19,7 @@ namespace DigitalData.Core.Application
/// and a culture-specific translation service for any necessary text translations, ensuring a versatile and internationalized approach to CRUD operations. /// and a culture-specific translation service for any necessary text translations, ensuring a versatile and internationalized approach to CRUD operations.
/// </remarks> /// </remarks>
public class BasicCRUDService<TCRUDRepository, TDto, TEntity, TId> : public class BasicCRUDService<TCRUDRepository, TDto, TEntity, TId> :
CRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId>, IBasicCRUDService<TDto, TEntity, TId> CRUDService<TCRUDRepository, TDto, TDto, TEntity, TId>, IBasicCRUDService<TDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId> where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId>
{ {
/// <summary> /// <summary>

View File

@@ -12,11 +12,10 @@ namespace DigitalData.Core.Application
/// </summary> /// </summary>
/// <typeparam name="TCreateDto">The DTO type for create operations.</typeparam> /// <typeparam name="TCreateDto">The DTO type for create operations.</typeparam>
/// <typeparam name="TReadDto">The DTO type for read operations.</typeparam> /// <typeparam name="TReadDto">The DTO type for read operations.</typeparam>
/// <typeparam name="TUpdateDto">The DTO type for update operations.</typeparam>
/// <typeparam name="TEntity">The entity type.</typeparam> /// <typeparam name="TEntity">The entity type.</typeparam>
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam> /// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ReadService<TCRUDRepository, TReadDto, TEntity, TId>, ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId> public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TEntity, TId> : ReadService<TCRUDRepository, TReadDto, TEntity, TId>, ICRUDService<TCreateDto, TReadDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : IUnique<TId> where TEntity : class, IUnique<TId> where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TEntity : class, IUnique<TId>
{ {
/// <summary> /// <summary>
@@ -45,7 +44,7 @@ namespace DigitalData.Core.Application
/// </summary> /// </summary>
/// <param name="updateDto">The DTO to update an entity from.</param> /// <param name="updateDto">The DTO to update an entity from.</param>
/// <returns>A service message indicating success or failure.</returns> /// <returns>A service message indicating success or failure.</returns>
public virtual async Task<Result> UpdateAsync(TUpdateDto updateDto) public virtual async Task<Result> UpdateAsync<TUpdateDto>(TUpdateDto updateDto) where TUpdateDto : IUnique<TId>
{ {
var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id); var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id);

View File

@@ -40,7 +40,6 @@ namespace DigitalData.Core.Application
/// <typeparam name="TCRUDRepository">The repository type that provides CRUD operations for entities of type TEntity.</typeparam> /// <typeparam name="TCRUDRepository">The repository type that provides CRUD operations for entities of type TEntity.</typeparam>
/// <typeparam name="TCreateDto">The DTO type used for create operations.</typeparam> /// <typeparam name="TCreateDto">The DTO type used for create operations.</typeparam>
/// <typeparam name="TReadDto">The DTO type used for read operations.</typeparam> /// <typeparam name="TReadDto">The DTO type used for read operations.</typeparam>
/// <typeparam name="TUpdateDto">The DTO type used for update operations.</typeparam>
/// <typeparam name="TEntity">The entity type corresponding to the DTOs.</typeparam> /// <typeparam name="TEntity">The entity type corresponding to the DTOs.</typeparam>
/// <typeparam name="TId">The type of the entity's identifier.</typeparam> /// <typeparam name="TId">The type of the entity's identifier.</typeparam>
/// <typeparam name="TProfile">The AutoMapper profile type for configuring mappings between the DTOs and the entity.</typeparam> /// <typeparam name="TProfile">The AutoMapper profile type for configuring mappings between the DTOs and the entity.</typeparam>
@@ -48,9 +47,9 @@ namespace DigitalData.Core.Application
/// <param name="configureService">An optional action to configure additional services for the CRUD service.</param> /// <param name="configureService">An optional action to configure additional services for the CRUD service.</param>
/// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns> /// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
public static IServiceCollection AddCleanCRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId, TProfile>(this IServiceCollection services, Action<IServiceCollection>? configureService = null) public static IServiceCollection AddCleanCRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId, TProfile>(this IServiceCollection services, Action<IServiceCollection>? configureService = null)
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : class, IUnique<TId> where TEntity : class, IUnique<TId> where TProfile : Profile where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class, IUnique<TId> where TEntity : class, IUnique<TId> where TProfile : Profile
{ {
services.AddScoped<ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>>(); services.AddScoped<ICRUDService<TCreateDto, TReadDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TEntity, TId>>();
configureService?.Invoke(services); configureService?.Invoke(services);
services.AddAutoMapper(typeof(TProfile).Assembly); services.AddAutoMapper(typeof(TProfile).Assembly);

View File

@@ -14,7 +14,9 @@
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core application clean architecture</PackageTags> <PackageTags>digital data core application clean architecture</PackageTags>
<Version>2.0.0.0</Version> <Version>3.0.1</Version>
<AssemblyVersion>3.0.1</AssemblyVersion>
<FileVersion>3.0.1</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,13 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,13 +0,0 @@
namespace DigitalData.Core.Security.Extensions
{
public static class Extensions
{
public static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
public static byte[] Base64ToByte(this string base64String) => Convert.FromBase64String(base64String);
public static byte[] ToBytes(this string str) => System.Text.Encoding.UTF8.GetBytes(str);
public static string BytesToString(this byte[] bytes) => System.Text.Encoding.UTF8.GetString(bytes);
}
}

View File

@@ -1,60 +0,0 @@
using DigitalData.Core.Abstractions.Security;
using System.Collections.Concurrent;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.Extensions
{
public static class RSAExtensions
{
public static RSA ToRSA(this string pem)
{
var rsa = RSA.Create();
rsa.ImportFromPem(pem);
return rsa;
}
private static string CreatePath(string filename, string? directory = null)
{
directory ??= Environment.CurrentDirectory;
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
return Path.Combine(directory, $"{filename}.pem");
}
private static readonly ConcurrentDictionary<string, SemaphoreSlim> FileLocks = new();
public static void SavePem(this IRSACryptographer decryptor, string key, string? directory = null)
{
var filePath = CreatePath(filename: key, directory : directory);
var fileLock = FileLocks.GetOrAdd(filePath, _ => new (1, 1));
fileLock.Wait();
try
{
File.WriteAllText(filePath, decryptor.Pem);
}
finally
{
fileLock.Release();
}
}
public static async Task SavePemAsync(this IRSACryptographer decryptor, string key, string? directory = null)
{
var filePath = CreatePath(filename: key, directory: directory);
var fileLock = FileLocks.GetOrAdd(filePath, _ => new (1, 1));
await fileLock.WaitAsync();
try
{
await File.WriteAllTextAsync(filePath, decryptor.Pem);
}
finally
{
fileLock.Release();
}
}
}
}

View File

@@ -1,20 +0,0 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Security
{
public class AsymCryptService<TAsymCryptParams> : RSAFactory<TAsymCryptParams>, IAsymCryptService<TAsymCryptParams>, IRSAFactory<TAsymCryptParams> where TAsymCryptParams : AsymCryptParams
{
public IEnumerable<IRSADecryptor> Decryptors => _params.Decryptors;
public IEnumerable<IRSAEncryptor> Encryptors => _params.Encryptors;
public AsymCryptService(IOptions<TAsymCryptParams> options, ILogger<AsymCryptService<TAsymCryptParams>>? logger = null) : base(options)
{
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
}
}
}

View File

@@ -1,59 +0,0 @@
using DigitalData.Core.Abstractions.Security;
namespace DigitalData.Core.Security.Config
{
public class AsymCryptParams : RSAFactoryParams
{
public string Directory { get; init; } = string.Empty;
/// <summary>
/// 0: Issuer - 1: Audience - 2: Type tag - 3: Version
/// </summary>
public string FileNameFormat { get; init; } = "{0}_-_{1}_-_{2}_-_{3}.pem";
public string EncryptorTag { get; init; } = "public";
public string DecryptorTag { get; init; } = "private";
public string EncryptedDecryptorTag { get; init; } = "enc-private";
public IEnumerable<IRSADecryptor> Decryptors { get; init; } = new List<IRSADecryptor>();
public IEnumerable<IRSAEncryptor> Encryptors { get; init; } = new List<IRSAEncryptor>();
private string TypeTagOf(IRSACryptographer crypt)
{
if (crypt is IRSAEncryptor)
return EncryptorTag;
else if (crypt is IRSADecryptor decryptor)
return decryptor.Encrypt ? EncryptedDecryptorTag : DecryptorTag;
else
throw new InvalidOperationException(
"Unknown cryptographer type. The crypt parameter must be either IRSAEncryptor or IRSADecryptor.");
}
public override void OnDeserialized()
{
base.OnDeserialized();
var cryptographers = Encryptors.Cast<IRSACryptographer>().Concat(Decryptors.Cast<IRSACryptographer>());
foreach (var crypt in cryptographers)
{
// set default path
if (crypt.Pem is null)
{
crypt.Directory ??= Directory;
crypt.FileName ??= string.Format(
FileNameFormat,
crypt.Issuer,
crypt.Audience,
TypeTagOf(crypt),
Secrets.Version);
}
crypt.Init();
}
}
}
}

View File

@@ -0,0 +1,11 @@
using System.Security.Claims;
namespace DigitalData.Core.Security.Config
{
public class ClaimDescriptor<TPrincipal>
{
public Func<TPrincipal, IDictionary<string, object>>? CreateClaims { get; init; }
public Func<TPrincipal, ClaimsIdentity>? CreateSubject { get; init; }
}
}

View File

@@ -0,0 +1,104 @@
using DigitalData.Core.Security.RSAKey;
namespace DigitalData.Core.Security.Config
{
public class CryptoFactoryParams : RSAFactoryParams
{
public string PemDirectory { get; init; } = string.Empty;
/// <summary>
/// Represents the separator used to concatenate the components of a file-related token string.
/// </summary>
/// <remarks>
/// The resulting file-related token string is constructed as follows:
/// <c>string.Join(FileNameSeparator, Issuer, Audience, Secret_version)</c>.
/// If <c>Secret_version</c> is not null, it will be included in the concatenation.
/// </remarks>
/// <example>
/// For example, if <c>FileNameSeparator = "_-_"</c>, the output might look like:
/// <c>"Issuer_-_Audience_-_Secret_version"</c>.
/// </example>
public string FileNameSeparator { get; init; } = "_-_";
public string FileExtension { get; init; } = "pem";
/// <summary>
///This is the subtext of the pem file name. For the file to be automatically renewed, the name must be assigned to change periodically. For example, by default MM/2 will be refreshed every 2 months.
/// <br />
/// - <see cref="StringExtensions.ToTag(DateTime, string)" /> is used when converting to tag.
/// <br />
/// - If the format contains the symbol “//”, the method divides the numeric value obtained from the left side of the format
/// by one minus the numeric value obtained from the right side of the format string and adds one. For instance:
/// <br />
/// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), subtracts one (3), divides it by 2,
/// rounds down the outgoing number (1), adds one to the number (resulting in 2).
/// <br />
/// - If the format does not contain "//", the method uses the default <see cref="DateTime.ToString"/> format.
/// <br />
/// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division.
/// </summary>
public string DateTagFormat { get; init; } = "MM//2";
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
public IEnumerable<RSATokenDescriptor> TokenDescriptors { get; init; } = new List<RSATokenDescriptor>();
public RSADecryptor? VaultDecryptor { get; init; }
public CryptoFactoryParams()
{
// init decryptors
AfterCreate += () =>
{
// Create root folder if it does not exist
if (!Directory.Exists(PemDirectory))
Directory.CreateDirectory(PemDirectory);
var privateKeys = new List<RSAPrivateKey>();
privateKeys.AddRange(Decryptors);
privateKeys.AddRange(TokenDescriptors);
if (VaultDecryptor is not null)
privateKeys.Add(VaultDecryptor);
foreach (var privateKey in privateKeys)
{
// set default path
if (privateKey.IsPemNull)
{
// file name
var file_name_params = new List<object>();
if (privateKey.Id is not null)
file_name_params.Add(privateKey.Id);
else if (privateKey is RSATokenDescriptor descriptor)
file_name_params.Add(descriptor.Issuer);
file_name_params.Add(KeySizeInBits);
file_name_params.Add(DateTime.Now.ToTag(DateTagFormat));
if (privateKey.IsEncrypted)
file_name_params.Add(Secrets.Version);
var file_name = $"{string.Join(FileNameSeparator, file_name_params)}.{FileExtension}";
var path = Path.Combine(PemDirectory, file_name);
if (File.Exists(path))
privateKey.SetPem(File.ReadAllText(path));
else
{
var pem = privateKey.IsEncrypted
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD)
: Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits);
privateKey.SetPem(pem);
// Save file in background
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
}
}
}
};
}
}
}

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using DigitalData.Core.Abstractions.Security;
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Security.Config
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<IAsymmetricTokenDescriptor, SecurityTokenDescriptor>();
}
}
}

View File

@@ -0,0 +1,9 @@
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Security.Config
{
public class ParamsConfigureOptions<TParams> : IConfigureOptions<TParams> where TParams : RSAFactoryParams
{
public void Configure(TParams options) => options.Init();
}
}

View File

@@ -1,4 +1,5 @@
using System.Security.Cryptography; using System.Reflection;
using System.Security.Cryptography;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace DigitalData.Core.Security.Config namespace DigitalData.Core.Security.Config
@@ -11,7 +12,16 @@ namespace DigitalData.Core.Security.Config
public PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } = PbeEncryptionAlgorithm.Aes256Cbc; public PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } = PbeEncryptionAlgorithm.Aes256Cbc;
public HashAlgorithmName PbeHashAlgorithmName { get; init; } = HashAlgorithmName.SHA256; public HashAlgorithmName PbeHashAlgorithm { get; init; } = HashAlgorithmName.SHA256;
// TODO: add as json converter to IConfigurIConfiguration.Config
public string PbeHashAlgorithmName
{
get => PbeHashAlgorithm.ToString();
init => PbeHashAlgorithm = (typeof(HashAlgorithmName).GetProperty(value, BindingFlags.Public | BindingFlags.Static)?.GetValue(null) is HashAlgorithmName hashAlgorithmName)
? hashAlgorithmName
: new(value);
}
public int PbeIterationCount { get; init; } = 100_000; public int PbeIterationCount { get; init; } = 100_000;
@@ -22,6 +32,28 @@ namespace DigitalData.Core.Security.Config
[JsonIgnore] [JsonIgnore]
public PbeParameters PbeParameters => _pbeParameters!; public PbeParameters PbeParameters => _pbeParameters!;
public virtual void OnDeserialized() => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount); /// <summary>
/// Provides a thread-safe initialization mechanism using Lazy initialization.
/// </summary>
private readonly Lazy<bool> _lazyInitializer;
public bool IsInitialized => _lazyInitializer.IsValueCreated;
public RSAFactoryParams()
{
_lazyInitializer = new(() =>
{
AfterCreate?.Invoke();
return true;
});
AfterCreate += () => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithm, PbeIterationCount);
}
protected event Action AfterCreate;
public void Init() => _ = _lazyInitializer.Value;
public void OnDeserialized() => Init();
} }
} }

View File

@@ -0,0 +1,38 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.RSAKey;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Security
{
public class CryptoFactory : RSAFactory<CryptoFactoryParams>, ICryptoFactory, IAsymmetricKeyFactory
{
public IEnumerable<IAsymmetricDecryptor> Decryptors { get; }
/// <summary>
/// It is a separate decryptor for permanently stored encrypted data. It is assigned to the first Default decryptor by default.
/// </summary>
public IAsymmetricDecryptor VaultDecryptor { get; }
public IEnumerable<IAsymmetricTokenDescriptor> TokenDescriptors { get; init; } = new List<IAsymmetricTokenDescriptor>();
public CryptoFactory(IOptions<CryptoFactoryParams> options, ILogger<CryptoFactory>? logger = null) : base(options)
{
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
if (!_params.Decryptors.Any())
throw new InvalidOperationException(
"Any decryptor is not found. Ensure that at least one decryptor is configured in the provided parameters. " +
"This issue typically arises if the configuration for decryptors is incomplete or missing. " +
"Check the 'Decryptors' collection in the configuration and verify that it contains valid entries."
);
Decryptors = _params.Decryptors;
TokenDescriptors = _params.TokenDescriptors;
VaultDecryptor = _params.VaultDecryptor ?? Decryptors.First();
}
}
}

View File

@@ -1,53 +0,0 @@
using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSACryptographer : IRSACryptographer
{
protected string? _pem;
public string Pem
{
get => _pem
?? throw new InvalidOperationException($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}.");
init => _pem = value;
}
public string? PemPath => FileName is null ? null : Path.Combine(Directory ?? string.Empty, FileName);
public string? Directory { get; set; }
public string? FileName { get; set; }
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
protected virtual RSA RSA { get; } = RSA.Create();
public string Issuer { get; init; } = string.Empty;
public string Audience { get; init; } = string.Empty;
internal RSACryptographer() { }
public virtual void UnableToInitPemEvent() => throw new InvalidOperationException(
$"Pem is not initialized and pem file is null. Issuer is {Issuer} and audience {Audience}.");
public virtual void FileNotFoundEvent() => throw new FileNotFoundException(
$"Pem is not initialized and pem file is not found in {PemPath}. Issuer is {Issuer} and audience {Audience}.");
// TODO: make file read asynchronous, consider multiple routing
public virtual void Init()
{
if(_pem is null)
{
if(PemPath is null)
UnableToInitPemEvent();
if (File.Exists(PemPath))
_pem = File.ReadAllText(PemPath);
else
FileNotFoundEvent();
}
}
}
}

View File

@@ -1,55 +0,0 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Extensions;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
{
public bool Encrypt { get; init; }
private readonly Lazy<IRSAEncryptor> _lazyEncryptor;
public IRSAEncryptor Encryptor => _lazyEncryptor.Value;
public RSADecryptor()
{
_lazyEncryptor = new(() => new RSAEncryptor()
{
Pem = RSA.ExportRSAPublicKeyPem(),
Padding = Padding
});
}
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString();
public override void Init()
{
base.Init();
if (Encrypt)
RSA.ImportFromEncryptedPem(Pem, Secrets.PBE_PASSWORD.AsSpan());
else
RSA.ImportFromPem(Pem);
}
public override void FileNotFoundEvent()
{
var new_decryptor = new RSADecryptor()
{
Pem = RSAFactory<RSAFactoryParams>.Static.CreateRSAPrivateKeyPem(),
Encrypt = Encrypt
};
_pem = new_decryptor.Pem;
if (PemPath is not null)
Task.Run(async () =>
{
await File.WriteAllTextAsync(_pem, PemPath);
});
}
}
}

View File

@@ -1,37 +0,0 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Extensions;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
{
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString();
public bool Verify(string data, string signature) => Encrypt(data) == signature;
public override void Init()
{
base.Init();
RSA.ImportFromPem(base.Pem);
}
public override void FileNotFoundEvent()
{
var new_decryptor = new RSADecryptor()
{
Pem = RSAFactory<RSAFactoryParams>.Static.CreateRSAPrivateKeyPem()
};
_pem = new_decryptor.Encryptor.Pem;
if (PemPath is not null)
Task.Run(async () =>
{
await File.WriteAllTextAsync(_pem, PemPath);
});
}
}
}

View File

@@ -1,44 +0,0 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using Microsoft.Extensions.Options;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSAFactory<TRSAFactoryParams> : IRSAFactory<TRSAFactoryParams> where TRSAFactoryParams : RSAFactoryParams
{
private static readonly Lazy<RSAFactory<RSAFactoryParams>> LazyInstance = new(() => new(Options.Create<RSAFactoryParams>(new())));
public static RSAFactory<RSAFactoryParams> Static => LazyInstance.Value;
protected readonly TRSAFactoryParams _params;
public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;
public string CreateRSAPrivateKeyPem(int? keySizeInBits = null)
=> RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem();
public string CreateEncryptedPrivateKeyPem(
int? keySizeInBits = null,
string? password = null,
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null)
{
password ??= _params.PbePassword;
var pbeParameters = pbeEncryptionAlgorithm is null && hashAlgorithmName is null && iterationCount is null
? new PbeParameters(
pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm,
hashAlgorithmName ?? _params.PbeHashAlgorithmName,
iterationCount ?? _params.PbeIterationCount)
: _params.PbeParameters;
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
return new string(pemChars);
}
}
}

View File

@@ -1,55 +1,71 @@
using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config; using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer; using DigitalData.Core.Security.RSAKey;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Text.Json; using System.Security.Claims;
using System.Text.Json.Serialization;
namespace DigitalData.Core.Security namespace DigitalData.Core.Security
{ {
public static class DIExtensions public static class DIExtensions
{ {
public static JsonSerializerOptions AddCryptographerConverter(this JsonSerializerOptions options) private static IServiceCollection AddParamsConfigureOptions<TParams>(this IServiceCollection services) where TParams : RSAFactoryParams
{ => services.AddSingleton<IConfigureOptions<TParams>, ParamsConfigureOptions<TParams>>();
if (!options.Converters.OfType<HashAlgorithmNameConverter>().Any())
options.Converters.Add(new HashAlgorithmNameConverter());
if (!options.Converters.OfType<JsonStringEnumConverter>().Any()) private static IServiceCollection AddCryptoFactory(this IServiceCollection services) => services
options.Converters.Add(new JsonStringEnumConverter()); .AddParamsConfigureOptions<CryptoFactoryParams>()
return options; .AddAutoMapper(typeof(MappingProfile).Assembly)
.AddSingleton<ICryptoFactory, CryptoFactory>();
/// <summary>
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
/// </summary>
/// <param name="services"></param>
/// <param name="section"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddCryptoFactory(this IServiceCollection services, IConfigurationSection section) => services
.Configure<CryptoFactoryParams>(section)
.AddCryptoFactory();
/// <summary>
/// Registers an asym crypt service with the specified parameters from the given instance.
/// </summary>
/// <param name="services"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddCryptoFactory(this IServiceCollection services, CryptoFactoryParams? factoryParams = null) => services
.AddSingleton(Options.Create(factoryParams ?? new()))
.AddCryptoFactory();
/// <summary>
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
/// </summary>
/// <param name="services"></param>
/// <param name="section"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) => services
.AddParamsConfigureOptions<RSAFactoryParams>()
.Configure<RSAFactoryParams>(section)
.AddSingleton<IAsymmetricKeyFactory, RSAFactory<RSAFactoryParams>>();
private static IServiceCollection AddClaimDescriptor<TPrincipal>(this IServiceCollection services,
Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null,
Func<TPrincipal, ClaimsIdentity>? subjectMapper = null)
{
var descriptor = new ClaimDescriptor<TPrincipal>
{
CreateClaims = claimsMapper,
CreateSubject = subjectMapper
};
return services.AddSingleton(sp => Options.Create(descriptor));
} }
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services) public static IServiceCollection AddJwtSignatureHandler<TPrincipal>(this IServiceCollection services,
where TAsymCryptParams : AsymCryptParams Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null,
{ Func<TPrincipal, ClaimsIdentity>? subjectMapper = null)
services.TryAddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>(); => services
return services; .AddClaimDescriptor(claimsMapper: claimsMapper, subjectMapper: subjectMapper)
} .AddSingleton<IJwtSignatureHandler<TPrincipal>, JwtSignatureHandler<TPrincipal>>();
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section)
where TAsymCryptParams : AsymCryptParams
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>();
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param)
where TAsymCryptParams : AsymCryptParams
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>();
private static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services)
where TRSAFactoryParams : RSAFactoryParams
{
services.TryAddScoped<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
return services;
}
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section)
where TRSAFactoryParams : RSAFactoryParams
=> services.Configure<TRSAFactoryParams>(section).AddRSAFactory<TRSAFactoryParams>();
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams param)
where TRSAFactoryParams : RSAFactoryParams
=> services.AddSingleton(Options.Create(param)).AddRSAFactory<TRSAFactoryParams>();
} }
} }

View File

@@ -4,15 +4,35 @@
<TargetFrameworks>net7.0;net8.0</TargetFrameworks> <TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PackageId>DigitalData.Core.Security</PackageId>
<Version>1.0.0</Version>
<Company>Digital Data GmbH</Company>
<Product>Digital Data GmbH</Product>
<Description>This package provides RSA-based security functionalities as an implementation of the DigitalData.Core.Abstractions.Security library. It supports robust encryption and decryption operations, as well as JWT signing and validation for secure authentication and data integrity.</Description>
<Authors>Digital Data GmbH</Authors>
<Copyright>Copyright 2025</Copyright>
<PackageProjectUrl></PackageProjectUrl>
<PackageIcon>core_icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core security</PackageTags>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\nuget-package-icons\core_icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" /> <ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
<ProjectReference Include="..\DigitalData.Core.Security.Extensions\DigitalData.Core.Security.Extensions.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,89 @@
using AutoMapper;
using DigitalData.Core.Abstractions.Security;
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Security
{
internal static class Extension
{
/// <summary>
/// Converts a <see cref="DateTime"/> to a formatted string based on the specified format string.
/// <br />
/// - If the format contains the symbol “//”, the method divides the numeric value obtained from the left side of the format
/// by one minus the numeric value obtained from the right side of the format string and adds one. For instance:
/// <br />
/// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), subtracts one (3), divides it by 2,
/// rounds down the outgoing number (1), adds one to the number (resulting in 2).
/// <br />
/// - If the format does not contain "//", the method uses the default <see cref="DateTime.ToString"/> format.
/// <br />
/// </summary>
/// <param name="date">The <see cref="DateTime"/> value to be formatted.</param>
/// <param name="format">The format string that dictates the formatting of the date. If the format includes the "//" symbol,
/// it splits the string at "//" and divides the left-side value by the right-side value. The format string can include standard
/// <see cref="DateTime.ToString"/> format patterns.</param>
/// <returns>A string representation of the formatted date, or the result of the division operation if "//" is present in the format.</returns>
/// <exception cref="ArgumentException">Thrown if the format string is invalid, such as having an incorrect number of parts after "//".</exception>
/// <exception cref="DivideByZeroException">Thrown if the right side of the "//" contains a zero, resulting in division by zero.</exception>
/// <exception cref="FormatException">Thrown if either the left-side or right-side value of "//" cannot be parsed as an integer.</exception>
internal static string ToTag(this DateTime date, string format)
{
if (format is not null && format.Contains("//"))
{
var subStrings = format.Split("//");
if (subStrings.Length != 2)
throw new ArgumentException($"Date tag format {format} is invalid. It must contain exactly one '//' separator.", nameof(format));
var formattedLeft = date.ToString(subStrings[0]);
if (!int.TryParse(formattedLeft, out var dateValue))
throw new FormatException($"The left-side value ({formattedLeft}) of the format could not be parsed to an integer.");
if (!int.TryParse(subStrings[1], out var divisor))
throw new FormatException($"The right-side value ({divisor}) of the format could not be parsed to an integer.");
if (divisor == 0)
throw new DivideByZeroException($"Date tag format {format} includes division by zero, which is not allowed.");
var result = (dateValue - 1) / divisor + 1;
return result.ToString();
}
return date.ToString(format);
}
/// <summary>
/// Converts a <see cref="DateTime"/> to a formatted string based on the specified format string.
/// <br />
/// - If the format contains the symbol “//”, the method divides the numeric value obtained from the left side of the format
/// by one minus the numeric value obtained from the right side of the format string and adds one. For instance:
/// <br />
/// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), subtracts one (3), divides it by 2,
/// rounds down the outgoing number (1), adds one to the number (resulting in 2).
/// <br />
/// - If the format does not contain "//", the method uses the default <see cref="DateTime.ToString"/> format.
/// <br />
/// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division.
/// </summary>
/// <param name="date">The <see cref="DateOnly"/> value to be formatted. It will convert to DateTime to use the method shared with DateTime.</param>
/// <param name="format">The format string that dictates the formatting of the date. If the format includes the "//" symbol,
/// it splits the string at "//" and divides the left-side value by the right-side value. The format string can include standard
/// <see cref="DateTime.ToString"/> format patterns.</param>
/// <returns>A string representation of the formatted date, or the result of the division operation if "//" is present in the format.</returns>
/// <exception cref="ArgumentException">Thrown if the format string is invalid, such as having an incorrect number of parts after "//".</exception>
/// <exception cref="DivideByZeroException">Thrown if the right side of the "//" contains a zero, resulting in division by zero.</exception>
/// <exception cref="FormatException">Thrown if either the left-side or right-side value of "//" cannot be parsed as an integer.</exception>
internal static string ToTag(this DateOnly date, string format) => date.ToDateTime(new()).ToTag(format);
/// <summary>
/// Maps a <see cref="RSATokenDescriptor"/> to a <see cref="SecurityTokenDescriptor"/>.
/// </summary>
/// <param name="mapper">The <see cref="IMapper"/> instance used for mapping.</param>
/// <param name="description">The <see cref="RSATokenDescriptor"/> instance to be mapped.</param>
/// <returns>A <see cref="SecurityTokenDescriptor"/> instance populated with the mapped values.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="mapper"/> or <paramref name="description"/> is <c>null</c>.</exception>
internal static SecurityTokenDescriptor Map(this IMapper mapper, IAsymmetricTokenDescriptor description)
=> mapper.Map(description, new SecurityTokenDescriptor());
}
}

View File

@@ -0,0 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0290:Use primary constructor", Justification = "<Pending>", Scope = "member", Target = "~M:DigitalData.Core.Security.JwtSignatureHandler`1.#ctor(Microsoft.Extensions.Options.IOptions{DigitalData.Core.Security.Config.ClaimDescriptor{`0}},AutoMapper.IMapper)")]

View File

@@ -1,13 +0,0 @@
using System.Security.Cryptography;
using System.Text.Json.Serialization;
using System.Text.Json;
namespace DigitalData.Core.Security
{
public class HashAlgorithmNameConverter : JsonConverter<HashAlgorithmName>
{
public override HashAlgorithmName Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new(reader.GetString() ?? string.Empty);
public override void Write(Utf8JsonWriter writer, HashAlgorithmName value, JsonSerializerOptions options) => writer.WriteStringValue(value.Name);
}
}

View File

@@ -0,0 +1,14 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.RSAKey;
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Security
{
public static class Instance
{
private static readonly Lazy<RSAFactory<RSAFactoryParams>> LazyInstance = new(() => new(Options.Create<RSAFactoryParams>(new())));
public static IAsymmetricKeyFactory RSAFactory => LazyInstance.Value;
}
}

View File

@@ -0,0 +1,40 @@
using AutoMapper;
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
namespace DigitalData.Core.Security
{
public class JwtSignatureHandler<TPrincipal> : JwtSecurityTokenHandler, IJwtSignatureHandler<TPrincipal>
{
private readonly ClaimDescriptor<TPrincipal> _claimDescriptor;
private readonly IMapper _mapper;
private readonly ICryptoFactory _cryptoFactory;
public JwtSignatureHandler(IOptions<ClaimDescriptor<TPrincipal>> claimDescriptorOptions, IMapper mapper, ICryptoFactory cryptoFactory)
{
_claimDescriptor = claimDescriptorOptions.Value;
_mapper = mapper;
_cryptoFactory = cryptoFactory;
}
public SecurityToken CreateToken(TPrincipal subject, IAsymmetricTokenDescriptor descriptor)
{
var sDescriptor = _mapper.Map(descriptor);
sDescriptor.Claims = _claimDescriptor.CreateClaims?.Invoke(subject);
sDescriptor.Subject = _claimDescriptor.CreateSubject?.Invoke(subject);
return CreateToken(sDescriptor);
}
public SecurityToken CreateToken(TPrincipal subject, string issuer, string audience)
{
var descriptor = _cryptoFactory.TokenDescriptors.Get(issuer: issuer, audience: audience)
?? throw new InvalidOperationException($"No or multiple token description found for issuer '{issuer}' and audience '{audience}'.");
return CreateToken(subject: subject, descriptor: descriptor);
}
}
}

View File

@@ -0,0 +1,33 @@
using DigitalData.Core.Abstractions.Security;
using System.Reflection;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.RSAKey
{
public class RSADecryptor : RSAPrivateKey, IAsymmetricDecryptor
{
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
// TODO: add as json converter to IConfigurIConfiguration.Config
public string PaddingName
{
get => Padding.ToString();
init => Padding = typeof(RSAEncryptionPadding).GetProperty(value, BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as RSAEncryptionPadding ?? throw new ArgumentException($"Padding '{value}' not found.");
}
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
private readonly Lazy<IAsymmetricEncryptor> _lazyEncryptor;
public IAsymmetricEncryptor Encryptor => _lazyEncryptor.Value;
public RSADecryptor()
{
_lazyEncryptor = new(() => new RSAEncryptor()
{
Content = RSA.ExportRSAPublicKeyPem(),
Padding = Padding
});
}
}
}

View File

@@ -0,0 +1,20 @@
using DigitalData.Core.Abstractions.Security;
using System.Reflection;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.RSAKey
{
public class RSAEncryptor : RSAPublicKey, IAsymmetricEncryptor
{
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
// TODO: add as json converter to IConfigurIConfiguration.Config
public string PaddingName
{
get => Padding.ToString();
init => Padding = typeof(RSAEncryptionPadding).GetProperty(value, BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as RSAEncryptionPadding ?? throw new ArgumentException($"Padding '{value}' not found.");
}
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
}
}

View File

@@ -0,0 +1,66 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config;
using Microsoft.Extensions.Options;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.RSAKey
{
public class RSAFactory<TRSAFactoryParams> : IAsymmetricKeyFactory where TRSAFactoryParams : RSAFactoryParams
{
protected readonly TRSAFactoryParams _params;
public RSAFactory(IOptions<TRSAFactoryParams> options)
{
options.Value.Init();
_params = options.Value;
}
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
: RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem();
public string CreateEncryptedPrivateKeyPem(
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null,
int? keySizeInBits = null,
string? password = null)
{
password ??= _params.PbePassword;
var pbeParameters = pbeEncryptionAlgorithm is null && hashAlgorithmName is null && iterationCount is null
? new PbeParameters(
pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm,
hashAlgorithmName ?? _params.PbeHashAlgorithm,
iterationCount ?? _params.PbeIterationCount)
: _params.PbeParameters;
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
return new string(pemChars);
}
public string CreateEncryptedPrivateKeyPem(
PbeParameters pbeParameters,
int? keySizeInBits = null,
string? password = null)
{
password ??= _params.PbePassword;
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
return new string(pemChars);
}
public IAsymmetricDecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null) => new RSADecryptor()
{
Content = pem,
IsEncrypted = encrypt,
Padding = padding ?? RSAEncryptionPadding.OaepSHA256
};
}
}

View File

@@ -0,0 +1,16 @@
using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.RSAKey
{
public class RSAKeyBase : IAsymmetricKey
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public virtual string Content { get; init; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public string? Id { get; init; }
protected virtual RSA RSA { get; } = RSA.Create();
}
}

View File

@@ -0,0 +1,55 @@
using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography;
namespace DigitalData.Core.Security.RSAKey
{
public class RSAPrivateKey : RSAKeyBase, IAsymmetricPrivateKey, IAsymmetricKey
{
private string? _pem;
public override string Content
{
#pragma warning disable CS8603 // Possible null reference return.
get => _pem;
#pragma warning restore CS8603 // Possible null reference return.
init
{
_pem = value;
Init();
}
}
public bool IsPemNull => _pem is null;
public bool IsEncrypted { get; init; }
protected TPublicKey CreatePublicKey<TPublicKey>() where TPublicKey : RSAPublicKey, new()
=> new() { Content = RSA.ExportRSAPublicKeyPem() };
private readonly Lazy<RSAPublicKey> _lazyPublicKey;
public IAsymmetricPublicKey PublicKey => _lazyPublicKey.Value;
public RSAPrivateKey()
{
_lazyPublicKey = new(CreatePublicKey<RSAPublicKey>);
}
internal void SetPem(string pem)
{
_pem = pem;
Init();
}
private void Init()
{
if (string.IsNullOrEmpty(_pem))
throw new InvalidOperationException ($"The content of RSA private key is null or empty. Id: {Id}.");
if (IsEncrypted)
RSA.ImportFromEncryptedPem(Content, Secrets.PBE_PASSWORD.AsSpan());
else
RSA.ImportFromPem(Content);
}
}
}

View File

@@ -0,0 +1,17 @@
using DigitalData.Core.Abstractions.Security;
namespace DigitalData.Core.Security.RSAKey
{
public class RSAPublicKey : RSAKeyBase, IAsymmetricPublicKey, IAsymmetricKey
{
public override string Content
{
get => base.Content;
init
{
base.Content = value;
RSA.ImportFromPem(value);
}
}
}
}

View File

@@ -0,0 +1,119 @@
using DigitalData.Core.Abstractions.Security;
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Security.RSAKey
{
/// <summary>
/// Contains some information which used to create a security token. Designed to abstract <see cref="SecurityTokenDescriptor"/>
/// </summary>
public class RSATokenDescriptor : RSAPrivateKey, IAsymmetricTokenDescriptor
{
private readonly Lazy<RSATokenValidator> _lazyTokenValidator;
public IAsymmetricTokenValidator Validator => _lazyTokenValidator.Value;
public required TimeSpan Lifetime { get; init; }
#region SecurityTokenDescriptor Map
/// <summary>
/// Gets or sets the value of the 'audience' claim.
/// </summary>
public required string Audience { get; set; }
/// <summary>
/// Defines the compression algorithm that will be used to compress the JWT token payload.
/// </summary>
public string CompressionAlgorithm { get; set; }
/// <summary>
/// Gets or sets the <see cref="EncryptingCredentials"/> used to create a encrypted security token.
/// </summary>
public EncryptingCredentials EncryptingCredentials { get; set; }
/// <summary>
/// Gets or sets the value of the 'expiration' claim. This value should be in UTC.
/// The expiration time is the sum of DateTime.Now and LifeTime.
/// </summary>
public DateTime? Expires => DateTime.Now.AddTicks(Lifetime.Ticks);
/// <summary>
/// Gets or sets the issuer of this <see cref="SecurityTokenDescriptor"/>.
/// </summary>
public required string Issuer { get; set; }
/// <summary>
/// Gets or sets the time the security token was issued. This value should be in UTC.
/// </summary>
public DateTime? IssuedAt { get; set; }
/// <summary>
/// Gets or sets the notbefore time for the security token. This value should be in UTC.
/// </summary>
public DateTime? NotBefore { get; set; }
/// <summary>
/// Gets or sets the token type.
/// <remarks> If provided, this will be added as the value for the 'typ' header parameter. In the case of a JWE, this will be added to both the inner (JWS) and the outer token (JWE) header. By default, the value used is 'JWT'.
/// If <see cref="AdditionalHeaderClaims"/> also contains 'typ' header claim value, it will override the TokenType provided here.
/// This value is used only for JWT tokens and not for SAML/SAML2 tokens</remarks>
/// </summary>
public string TokenType { get; set; }
/// <summary>
/// Gets or sets the <see cref="Dictionary{TKey, TValue}"/> which contains any custom header claims that need to be added to the JWT token header.
/// The 'alg', 'kid', 'x5t', 'enc', and 'zip' claims are added by default based on the <see cref="SigningCredentials"/>,
/// <see cref="EncryptingCredentials"/>, and/or <see cref="CompressionAlgorithm"/> provided and SHOULD NOT be included in this dictionary as this
/// will result in an exception being thrown.
/// <remarks> These claims are only added to the outer header (in case of a JWE).</remarks>
/// </summary>
public IDictionary<string, object> AdditionalHeaderClaims { get; set; }
/// <summary>
/// Gets or sets the <see cref="Dictionary{TKey, TValue}"/> which contains any custom header claims that need to be added to the inner JWT token header.
/// The 'alg', 'kid', 'x5t', 'enc', and 'zip' claims are added by default based on the <see cref="SigningCredentials"/>,
/// <see cref="EncryptingCredentials"/>, and/or <see cref="CompressionAlgorithm"/> provided and SHOULD NOT be included in this dictionary as this
/// will result in an exception being thrown.
/// <remarks>
/// For JsonWebTokenHandler, these claims are merged with <see cref="AdditionalHeaderClaims"/> while adding to the inner JWT header.
/// </remarks>
/// </summary>
public IDictionary<string, object> AdditionalInnerHeaderClaims { get; set; }
/// <summary>
/// Gets or sets the <see cref="SigningCredentials"/> used to create a security token.
/// </summary>
public SigningCredentials SigningCredentials => _lazySigningCredentials.Value;
#endregion SecurityTokenDescriptor
private readonly Lazy<RsaSecurityKey> _lazyRsaSecurityKey;
public SecurityKey SecurityKey => _lazyRsaSecurityKey.Value;
private readonly Lazy<SigningCredentials> _lazySigningCredentials;
/// <summary>
/// Specifies the signature algorithm to be applied to the <see cref="SigningCredentials"/>.
/// Default is <see cref="SecurityAlgorithms.RsaSha256"/>.
/// </summary>
public string SigningAlgorithm { get; init; } = SecurityAlgorithms.RsaSha256;
/// <summary>
/// Optionally specifies the digest algorithm to be applied during the signing process for the <see cref="SigningCredentials"/>.
/// If not provided, the default algorithm is used.
/// </summary>
public string? SigningDigest { get; init; }
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public RSATokenDescriptor()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
_lazyTokenValidator = new(CreatePublicKey<RSATokenValidator>);
_lazyRsaSecurityKey = new(() => new RsaSecurityKey(RSA));
_lazySigningCredentials = new(() => SigningDigest is null
? new(SecurityKey, SigningAlgorithm)
: new(SecurityKey, SigningAlgorithm, SigningDigest));
}
}
}

View File

@@ -0,0 +1,17 @@
using DigitalData.Core.Abstractions.Security;
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Security.RSAKey
{
public class RSATokenValidator : RSAPublicKey, IAsymmetricTokenValidator
{
private readonly Lazy<RsaSecurityKey> _lazyRsaSecurityKey;
public SecurityKey SecurityKey => _lazyRsaSecurityKey.Value;
public RSATokenValidator()
{
_lazyRsaSecurityKey = new(() => new RsaSecurityKey(RSA));
}
}
}

View File

@@ -23,9 +23,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Legacy.Cli
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Security", "DigitalData.Core.Security\DigitalData.Core.Security.csproj", "{47D80C65-74A2-4EB8-96A5-D571A9108FB3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Security", "DigitalData.Core.Security\DigitalData.Core.Security.csproj", "{47D80C65-74A2-4EB8-96A5-D571A9108FB3}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Security.Extensions", "DigitalData.Core.Security.Extensions\DigitalData.Core.Security.Extensions.csproj", "{D740182D-82DA-480A-9F87-BFB4A8620A00}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Terminal", "DigitalData.Core.Terminal\DigitalData.Core.Terminal.csproj", "{0FA93730-8084-4907-B172-87D610323796}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Terminal", "DigitalData.Core.Terminal\DigitalData.Core.Terminal.csproj", "{0FA93730-8084-4907-B172-87D610323796}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Tests.API", "DigitalData.Core.Tests.API\DigitalData.Core.Tests.API.csproj", "{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -68,18 +68,18 @@ Global
{E009A053-A9F4-48F2-984F-EF5C376A9B14}.Debug|Any CPU.Build.0 = Release|Any CPU {E009A053-A9F4-48F2-984F-EF5C376A9B14}.Debug|Any CPU.Build.0 = Release|Any CPU
{E009A053-A9F4-48F2-984F-EF5C376A9B14}.Release|Any CPU.ActiveCfg = Release|Any CPU {E009A053-A9F4-48F2-984F-EF5C376A9B14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E009A053-A9F4-48F2-984F-EF5C376A9B14}.Release|Any CPU.Build.0 = Release|Any CPU {E009A053-A9F4-48F2-984F-EF5C376A9B14}.Release|Any CPU.Build.0 = Release|Any CPU
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU {47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Debug|Any CPU.Build.0 = Release|Any CPU
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU {47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.Build.0 = Release|Any CPU {47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.Build.0 = Release|Any CPU
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Release|Any CPU.Build.0 = Release|Any CPU
{0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.Build.0 = Debug|Any CPU {0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.ActiveCfg = Release|Any CPU {0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.Build.0 = Release|Any CPU {0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.Build.0 = Release|Any CPU
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BC2DEC5-E89D-48CC-9A51-4D94496EE4A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE