|
6 | 6 | using System.Security; |
7 | 7 | using System.Security.Cryptography; |
8 | 8 | using System.Text; |
| 9 | +using System.Text.RegularExpressions; |
9 | 10 | using Bunq.Sdk.Context; |
10 | 11 | using Bunq.Sdk.Exception; |
11 | 12 | using Bunq.Sdk.Http; |
@@ -77,6 +78,11 @@ public class SecurityUtils |
77 | 78 | /// Number of the very first index in an array or a string. |
78 | 79 | /// </summary> |
79 | 80 | private const int INDEX_FIRST = 0; |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Regex constants. |
| 84 | + /// </summary> |
| 85 | + private const string REGEX_FOR_LOWERCASE_HEADERS = "(-[a-z])"; |
80 | 86 |
|
81 | 87 | /// <summary> |
82 | 88 | /// Generates a base64-representation of RSA/SHA256/PKCS1 signature for a given RequestMessage. |
@@ -127,6 +133,20 @@ private static string GenerateRequestHeadersSortedString(HttpRequestMessage requ |
127 | 133 | ); |
128 | 134 | } |
129 | 135 |
|
| 136 | + private static string GetHeaderNameCorrectyCased(string headerName) |
| 137 | + { |
| 138 | + headerName = headerName.ToLower(); |
| 139 | + headerName = headerName.First().ToString().ToUpper() + headerName.Substring(1); |
| 140 | + var matches = Regex.Matches(headerName, REGEX_FOR_LOWERCASE_HEADERS); |
| 141 | + |
| 142 | + return matches.Cast<Match>().Aggregate( |
| 143 | + headerName, |
| 144 | + (current, match) => current.Replace( |
| 145 | + match.Groups[INDEX_FIRST].Value, match.Groups[INDEX_FIRST].Value.ToUpper() |
| 146 | + ) |
| 147 | + ); |
| 148 | + } |
| 149 | + |
130 | 150 | private static string GenerateHeadersSortedString( |
131 | 151 | IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) |
132 | 152 | { |
@@ -307,8 +327,8 @@ private static string GenerateResponseHeadersSortedString(HttpResponseMessage re |
307 | 327 | { |
308 | 328 | return GenerateHeadersSortedString( |
309 | 329 | responseMessage.Headers.Where(x => |
310 | | - x.Key.StartsWith(HEADER_NAME_PREFIX_X_BUNQ) && |
311 | | - !x.Key.Equals(HEADER_SERVER_SIGNATURE) |
| 330 | + GetHeaderNameCorrectyCased(x.Key).StartsWith(HEADER_NAME_PREFIX_X_BUNQ) && |
| 331 | + !GetHeaderNameCorrectyCased(x.Key).Equals(HEADER_SERVER_SIGNATURE) |
312 | 332 | ) |
313 | 333 | ); |
314 | 334 | } |
|
0 commit comments