|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Security.Cryptography; |
4 | | -using System.Security.Cryptography.X509Certificates; |
5 | | - |
6 | | -namespace Mastercard.Developer.ClientEncryption.Core.Encryption |
7 | | -{ |
8 | | - /// <summary> |
9 | | - /// A builder class for <see cref="JweConfig"/>. |
10 | | - /// </summary> |
11 | | - public class JweConfigBuilder : EncryptionConfigBuilder |
12 | | - { |
13 | | - private JweConfigBuilder() { } |
14 | | - |
15 | | - /// <summary> |
16 | | - /// Get an instance of the builder. |
17 | | - /// </summary> |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Security.Cryptography.X509Certificates; |
| 5 | + |
| 6 | +namespace Mastercard.Developer.ClientEncryption.Core.Encryption |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A builder class for <see cref="JweConfig"/>. |
| 10 | + /// </summary> |
| 11 | + public class JweConfigBuilder : EncryptionConfigBuilder |
| 12 | + { |
| 13 | + private JweConfigBuilder() { } |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Get an instance of the builder. |
| 17 | + /// </summary> |
18 | 18 | public static JweConfigBuilder AJweEncryptionConfig() => new JweConfigBuilder(); |
19 | 19 |
|
20 | | - /// <summary> |
21 | | - /// Get an instance of the builder. |
22 | | - /// </summary> |
23 | | - [Obsolete("Use AJweEncryptionConfig instead.")] |
24 | | - public static JweConfigBuilder AJweConfigBuilder() => new JweConfigBuilder(); |
25 | | - |
26 | | - /// <summary> |
27 | | - /// See: <see cref="EncryptionConfig.EncryptionCertificate"/> |
28 | | - /// </summary> |
29 | | - public JweConfigBuilder WithEncryptionCertificate(X509Certificate2 encryptionCertificate) |
30 | | - { |
31 | | - _encryptionCertificate = encryptionCertificate; |
32 | | - return this; |
33 | | - } |
34 | | - |
35 | | - /// <summary> |
36 | | - /// See: <see cref="EncryptionConfig.DecryptionKey"/> |
37 | | - /// </summary> |
38 | | - public JweConfigBuilder WithDecryptionKey(RSA decryptionKey) |
39 | | - { |
40 | | - _decryptionKey = decryptionKey; |
41 | | - return this; |
42 | | - } |
43 | | - |
44 | | - /// <summary> |
45 | | - /// See: <see cref="EncryptionConfig.EncryptionPaths"/> |
46 | | - /// </summary> |
47 | | - public JweConfigBuilder WithEncryptionPath(string jsonPathIn, string jsonPathOut) |
48 | | - { |
49 | | - _encryptionPaths.Add(jsonPathIn, jsonPathOut); |
50 | | - return this; |
51 | | - } |
52 | | - |
53 | | - /// <summary> |
54 | | - /// See: <see cref="EncryptionConfig.DecryptionPaths"/> |
55 | | - /// </summary> |
56 | | - public JweConfigBuilder WithDecryptionPath(string jsonPathIn, string jsonPathOut) |
57 | | - { |
58 | | - _decryptionPaths.Add(jsonPathIn, jsonPathOut); |
59 | | - return this; |
60 | | - } |
61 | | - |
62 | | - /// <summary> |
63 | | - /// See: <see cref="EncryptionConfig.EncryptedValueFieldName"/> |
64 | | - /// </summary> |
65 | | - public JweConfigBuilder WithEncryptedValueFieldName(string encryptedValueFieldName) |
66 | | - { |
67 | | - _encryptedValueFieldName = encryptedValueFieldName; |
68 | | - return this; |
69 | | - } |
70 | | - |
71 | | - /// <summary> |
72 | | - /// Build a <see cref="JweConfig"/> |
73 | | - /// </summary> |
74 | | - public JweConfig Build() |
75 | | - { |
76 | | - CheckParameterValues(); |
77 | | - ComputeEncryptionKeyFingerprintWhenNeeded(); |
78 | | - CheckJsonPathParameterValues(); |
79 | | - |
80 | | - return new JweConfig |
81 | | - { |
82 | | - EncryptionCertificate = _encryptionCertificate, |
83 | | - EncryptionKeyFingerprint = _encryptionKeyFingerprint, |
84 | | - DecryptionKey = _decryptionKey, |
85 | | - EncryptionPaths = _encryptionPaths.Count == 0 ? new Dictionary<string, string> { { "$", "$" } } : _encryptionPaths, |
86 | | - DecryptionPaths = _decryptionPaths.Count == 0 ? new Dictionary<string, string> { { "$.encryptedData", "$" } } : _decryptionPaths, |
87 | | - EncryptedValueFieldName = _encryptedValueFieldName ?? "encryptedData", |
88 | | - Scheme = EncryptionConfig.EncryptionScheme.Jwe |
89 | | - }; |
90 | | - } |
91 | | - |
92 | | - private void CheckParameterValues() |
93 | | - { |
94 | | - if (_decryptionKey == null && _encryptionCertificate == null) |
95 | | - { |
96 | | - throw new ArgumentException("You must include at least an encryption certificate or a decryption key"); |
97 | | - } |
98 | | - } |
99 | | - } |
100 | | -} |
| 20 | + /// <summary> |
| 21 | + /// See: <see cref="EncryptionConfig.EncryptionCertificate"/> |
| 22 | + /// </summary> |
| 23 | + public JweConfigBuilder WithEncryptionCertificate(X509Certificate2 encryptionCertificate) |
| 24 | + { |
| 25 | + _encryptionCertificate = encryptionCertificate; |
| 26 | + return this; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// See: <see cref="EncryptionConfig.DecryptionKey"/> |
| 31 | + /// </summary> |
| 32 | + public JweConfigBuilder WithDecryptionKey(RSA decryptionKey) |
| 33 | + { |
| 34 | + _decryptionKey = decryptionKey; |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// See: <see cref="EncryptionConfig.EncryptionPaths"/> |
| 40 | + /// </summary> |
| 41 | + public JweConfigBuilder WithEncryptionPath(string jsonPathIn, string jsonPathOut) |
| 42 | + { |
| 43 | + _encryptionPaths.Add(jsonPathIn, jsonPathOut); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// See: <see cref="EncryptionConfig.DecryptionPaths"/> |
| 49 | + /// </summary> |
| 50 | + public JweConfigBuilder WithDecryptionPath(string jsonPathIn, string jsonPathOut) |
| 51 | + { |
| 52 | + _decryptionPaths.Add(jsonPathIn, jsonPathOut); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// See: <see cref="EncryptionConfig.EncryptedValueFieldName"/> |
| 58 | + /// </summary> |
| 59 | + public JweConfigBuilder WithEncryptedValueFieldName(string encryptedValueFieldName) |
| 60 | + { |
| 61 | + _encryptedValueFieldName = encryptedValueFieldName; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Build a <see cref="JweConfig"/> |
| 67 | + /// </summary> |
| 68 | + public JweConfig Build() |
| 69 | + { |
| 70 | + CheckParameterValues(); |
| 71 | + ComputeEncryptionKeyFingerprintWhenNeeded(); |
| 72 | + CheckJsonPathParameterValues(); |
| 73 | + |
| 74 | + return new JweConfig |
| 75 | + { |
| 76 | + EncryptionCertificate = _encryptionCertificate, |
| 77 | + EncryptionKeyFingerprint = _encryptionKeyFingerprint, |
| 78 | + DecryptionKey = _decryptionKey, |
| 79 | + EncryptionPaths = _encryptionPaths.Count == 0 ? new Dictionary<string, string> { { "$", "$" } } : _encryptionPaths, |
| 80 | + DecryptionPaths = _decryptionPaths.Count == 0 ? new Dictionary<string, string> { { "$.encryptedData", "$" } } : _decryptionPaths, |
| 81 | + EncryptedValueFieldName = _encryptedValueFieldName ?? "encryptedData", |
| 82 | + Scheme = EncryptionConfig.EncryptionScheme.Jwe |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + private void CheckParameterValues() |
| 87 | + { |
| 88 | + if (_decryptionKey == null && _encryptionCertificate == null) |
| 89 | + { |
| 90 | + throw new ArgumentException("You must include at least an encryption certificate or a decryption key"); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments