|
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;
|
@@ -78,6 +79,16 @@ public class SecurityUtils
|
78 | 79 | /// </summary>
|
79 | 80 | private const int INDEX_FIRST = 0;
|
80 | 81 |
|
| 82 | + /// <summary> |
| 83 | + /// The index after the firts character in a string. |
| 84 | + /// </summary> |
| 85 | + private const int INDEX_LAST_FIRST_CHAR = 1; |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Regex constants. |
| 89 | + /// </summary> |
| 90 | + private const string REGEX_FOR_LOWERCASE_HEADERS = "(-[a-z])"; |
| 91 | + |
81 | 92 | /// <summary>
|
82 | 93 | /// Generates a base64-representation of RSA/SHA256/PKCS1 signature for a given RequestMessage.
|
83 | 94 | /// </summary>
|
@@ -127,6 +138,20 @@ private static string GenerateRequestHeadersSortedString(HttpRequestMessage requ
|
127 | 138 | );
|
128 | 139 | }
|
129 | 140 |
|
| 141 | + private static string GetHeaderNameCorrectlyCased(string headerName) |
| 142 | + { |
| 143 | + headerName = headerName.ToLower(); |
| 144 | + headerName = headerName.First().ToString().ToUpper() + headerName.Substring(INDEX_LAST_FIRST_CHAR); |
| 145 | + var matches = Regex.Matches(headerName, REGEX_FOR_LOWERCASE_HEADERS); |
| 146 | + |
| 147 | + return matches.Cast<Match>().Aggregate( |
| 148 | + headerName, |
| 149 | + (current, match) => current.Replace( |
| 150 | + match.Groups[INDEX_FIRST].Value, match.Groups[INDEX_FIRST].Value.ToUpper() |
| 151 | + ) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
130 | 155 | private static string GenerateHeadersSortedString(
|
131 | 156 | IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
|
132 | 157 | {
|
@@ -307,8 +332,8 @@ private static string GenerateResponseHeadersSortedString(HttpResponseMessage re
|
307 | 332 | {
|
308 | 333 | return GenerateHeadersSortedString(
|
309 | 334 | responseMessage.Headers.Where(x =>
|
310 |
| - x.Key.StartsWith(HEADER_NAME_PREFIX_X_BUNQ) && |
311 |
| - !x.Key.Equals(HEADER_SERVER_SIGNATURE) |
| 335 | + GetHeaderNameCorrectlyCased(x.Key).StartsWith(HEADER_NAME_PREFIX_X_BUNQ) && |
| 336 | + !GetHeaderNameCorrectlyCased(x.Key).Equals(HEADER_SERVER_SIGNATURE) |
312 | 337 | )
|
313 | 338 | );
|
314 | 339 | }
|
|
0 commit comments