Skip to content
This repository was archived by the owner on Jan 24, 2021. It is now read-only.

Commit 0843e37

Browse files
Merge pull request #2766 from thecodejunkie/csrf-update-implementation
Changed date parsing and extracted method
2 parents 3156678 + 413c64c commit 0843e37

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/Nancy/Security/Csrf.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ private static CsrfToken GetCookieToken(Request request)
172172
return cookieToken;
173173
}
174174

175-
private static CsrfToken ParseToCsrfToken(string cookieTokenString)
175+
private static void AddTokenValue(Dictionary<string, string> dictionary, string key, string value)
176176
{
177-
var parsed = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
178-
179-
void Add(string key, string value)
177+
if (!string.IsNullOrEmpty(key))
180178
{
181-
if (!string.IsNullOrEmpty(key))
182-
{
183-
parsed.Add(key, value);
184-
}
179+
dictionary.Add(key, value);
185180
}
181+
}
182+
183+
private static CsrfToken ParseToCsrfToken(string cookieTokenString)
184+
{
185+
var parsed = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
186186

187187
var currentKey = string.Empty;
188188
var buffer = new StringBuilder();
@@ -198,7 +198,7 @@ void Add(string key, string value)
198198
buffer.Clear();
199199
break;
200200
case PairDelimiter:
201-
Add(currentKey, buffer.ToString());
201+
AddTokenValue(parsed, currentKey, buffer.ToString());
202202
buffer.Clear();
203203
break;
204204
default:
@@ -207,7 +207,7 @@ void Add(string key, string value)
207207
}
208208
}
209209

210-
Add(currentKey, buffer.ToString());
210+
AddTokenValue(parsed, currentKey, buffer.ToString());
211211

212212
if (parsed.Keys.Count() != 3)
213213
{
@@ -218,7 +218,7 @@ void Add(string key, string value)
218218
{
219219
return new CsrfToken
220220
{
221-
CreatedDate = DateTimeOffset.ParseExact(parsed["CreatedDate"], "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal),
221+
CreatedDate = DateTimeOffset.ParseExact(parsed["CreatedDate"], "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal),
222222
Hmac = Convert.FromBase64String(parsed["Hmac"]),
223223
RandomBytes = Convert.FromBase64String(parsed["RandomBytes"])
224224
};

0 commit comments

Comments
 (0)