|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using Mastercard.Developer.ClientEncryption.Core.Encryption; |
| 5 | +using RestSharp.Portable; |
| 6 | + |
| 7 | +namespace Mastercard.Developer.ClientEncryption.RestSharp.Interceptors |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// A class for encrypting RestSharp requests and decrypting RestSharp responses. |
| 11 | + /// </summary> |
| 12 | + public class RestSharpFieldLevelEncryptionInterceptor |
| 13 | + { |
| 14 | + private readonly FieldLevelEncryptionConfig _config; |
| 15 | + |
| 16 | + public RestSharpFieldLevelEncryptionInterceptor(FieldLevelEncryptionConfig config) |
| 17 | + { |
| 18 | + _config = config; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Encrypt RestSharp request payloads. |
| 23 | + /// </summary> |
| 24 | + /// <param name="request">A RestSharp request object</param> |
| 25 | + public void InterceptRequest(IRestRequest request) |
| 26 | + { |
| 27 | + if (request == null) throw new ArgumentNullException(nameof(request)); |
| 28 | + |
| 29 | + try |
| 30 | + { |
| 31 | + // Check request actually has a payload |
| 32 | + var bodyParam = request.Parameters.FirstOrDefault(param => param.Type == ParameterType.RequestBody); |
| 33 | + if (bodyParam == null) |
| 34 | + { |
| 35 | + // Nothing to encrypt |
| 36 | + return; |
| 37 | + } |
| 38 | + var payload = bodyParam.Value.ToString(); |
| 39 | + if (string.IsNullOrEmpty(payload)) |
| 40 | + { |
| 41 | + // Nothing to encrypt |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Encrypt fields & update headers |
| 46 | + string encryptedPayload; |
| 47 | + if (_config.UseHttpHeaders()) |
| 48 | + { |
| 49 | + // Generate encryption params and add them as HTTP headers |
| 50 | + var parameters = FieldLevelEncryptionParams.Generate(_config); |
| 51 | + UpdateRequestHeader(request, _config.IvHeaderName, parameters.IvValue); |
| 52 | + UpdateRequestHeader(request, _config.EncryptedKeyHeaderName, parameters.EncryptedKeyValue); |
| 53 | + UpdateRequestHeader(request, _config.EncryptionCertificateFingerprintHeaderName, parameters.EncryptionCertificateFingerprintValue); |
| 54 | + UpdateRequestHeader(request, _config.EncryptionKeyFingerprintHeaderName, parameters.EncryptionKeyFingerprintValue); |
| 55 | + UpdateRequestHeader(request, _config.OaepPaddingDigestAlgorithmHeaderName, parameters.OaepPaddingDigestAlgorithmValue); |
| 56 | + encryptedPayload = FieldLevelEncryption.EncryptPayload(payload, _config, parameters); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + // Encryption params will be stored in the payload |
| 61 | + encryptedPayload = FieldLevelEncryption.EncryptPayload(payload, _config); |
| 62 | + } |
| 63 | + |
| 64 | + // Update body and content length |
| 65 | + bodyParam.Value = encryptedPayload; |
| 66 | + UpdateRequestHeader(request, "Content-Length", encryptedPayload.Length); |
| 67 | + } |
| 68 | + catch (EncryptionException) |
| 69 | + { |
| 70 | + throw; |
| 71 | + } |
| 72 | + catch (Exception e) |
| 73 | + { |
| 74 | + throw new EncryptionException("Failed to intercept and encrypt request!", e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Decrypt RestSharp response payloads. |
| 80 | + /// </summary> |
| 81 | + /// <param name="response">A RestSharp response object</param> |
| 82 | + public void InterceptResponse(IRestResponse response) |
| 83 | + { |
| 84 | + if (response == null) throw new ArgumentNullException(nameof(response)); |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + // Read response payload |
| 89 | + var encryptedPayload = response.Content; |
| 90 | + if (string.IsNullOrEmpty(encryptedPayload)) |
| 91 | + { |
| 92 | + // Nothing to decrypt |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // Decrypt fields & update headers |
| 97 | + string decryptedPayload; |
| 98 | + if (_config.UseHttpHeaders()) |
| 99 | + { |
| 100 | + // Read encryption params from HTTP headers and delete headers |
| 101 | + var ivValue = ReadAndRemoveHeader(response, _config.IvHeaderName); |
| 102 | + var encryptedKeyValue = ReadAndRemoveHeader(response, _config.EncryptedKeyHeaderName); |
| 103 | + var oaepPaddingDigestAlgorithmValue = ReadAndRemoveHeader(response, _config.OaepPaddingDigestAlgorithmHeaderName); |
| 104 | + ReadAndRemoveHeader(response, _config.EncryptionCertificateFingerprintHeaderName); |
| 105 | + ReadAndRemoveHeader(response, _config.EncryptionKeyFingerprintHeaderName); |
| 106 | + var parameters = new FieldLevelEncryptionParams(_config, ivValue, encryptedKeyValue, oaepPaddingDigestAlgorithmValue); |
| 107 | + decryptedPayload = FieldLevelEncryption.DecryptPayload(encryptedPayload, _config, parameters); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + // Encryption params are stored in the payload |
| 112 | + decryptedPayload = FieldLevelEncryption.DecryptPayload(encryptedPayload, _config); |
| 113 | + } |
| 114 | + |
| 115 | + // Update body and content length |
| 116 | + var contentTypeInfo = response.GetType().GetTypeInfo().GetDeclaredField("_content"); |
| 117 | + contentTypeInfo.SetValue(response, new Lazy<string>(() => decryptedPayload)); |
| 118 | + UpdateResponseHeader(response, "Content-Length", decryptedPayload.Length.ToString()); |
| 119 | + } |
| 120 | + catch (EncryptionException) |
| 121 | + { |
| 122 | + throw; |
| 123 | + } |
| 124 | + catch (Exception e) |
| 125 | + { |
| 126 | + throw new EncryptionException("Failed to intercept and decrypt response!", e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static void UpdateRequestHeader(IRestRequest request, string name, object value) |
| 131 | + { |
| 132 | + if (string.IsNullOrEmpty(name)) |
| 133 | + { |
| 134 | + // Do nothing |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + request.AddOrUpdateHeader(name, value); |
| 139 | + } |
| 140 | + |
| 141 | + private static void UpdateResponseHeader(IRestResponse response, string name, string value) |
| 142 | + { |
| 143 | + if (string.IsNullOrEmpty(name)) |
| 144 | + { |
| 145 | + // Do nothing |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + response.Headers.Remove(name); |
| 150 | + response.Headers.Add(name, value); |
| 151 | + } |
| 152 | + |
| 153 | + private static string ReadAndRemoveHeader(IRestResponse response, string name) |
| 154 | + { |
| 155 | + if (string.IsNullOrEmpty(name) || !response.Headers.Contains(name)) |
| 156 | + { |
| 157 | + // Do nothing |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + var value = response.Headers.GetValue(name); |
| 162 | + response.Headers.Remove(name); |
| 163 | + return value; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments