Skip to content

Commit 00a074f

Browse files
authored
Merge pull request #5 from codetronics/master
Parse Digest string with Regex
2 parents 6c6f9fd + d9a775d commit 00a074f

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

src/DigestAuthenticator/DigestAuthenticatorManager.cs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net;
77
using System.Security.Cryptography;
88
using System.Text;
9+
using System.Text.RegularExpressions;
910
using System;
1011

1112
using RestSharp;
@@ -174,7 +175,7 @@ private void GetDigestDataFromException(WebException ex)
174175
.Next(123400, 9999999)
175176
.ToString(CultureInfo.InvariantCulture);
176177

177-
_realm = wwwAuthenticateHeader.GetFirstHeader(DIGEST_REALM, REALM);
178+
_realm = wwwAuthenticateHeader.GetHeader(REALM);
178179
_nonce = wwwAuthenticateHeader.GetHeader(NONCE);
179180
_qop = wwwAuthenticateHeader.GetHeader(QOP);
180181
}
@@ -186,12 +187,46 @@ private void GetDigestDataFromException(WebException ex)
186187
/// <returns>A instance of <see cref="IDictionary{K,V}"/>.</returns>
187188
private static IDictionary<string, string> TransformHeaderToDictionary(string wwwAuthenticateHeader)
188189
{
189-
return wwwAuthenticateHeader
190-
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
191-
.Select(part => part.Split('='))
192-
.ToDictionary(
193-
split => split[0].Trim(),
194-
split => split[1].Replace('"', ' ').Trim());
190+
var regex = new Regex("realm=\"(?<realm>.*?)\"|qop=\"(?<qop>.*?)\"|nonce=\"(?<nonce>.*?)\"|stale=\"(?<stale>.*?)\"|opaque=\"(?<opaque>.*?)\"|domain=\"(?<domain>.*?)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
191+
var matches = regex.Matches(wwwAuthenticateHeader);
192+
193+
var dict = new Dictionary<string, string>();
194+
195+
// There should be 6 matches
196+
foreach (Match m in matches)
197+
{
198+
if (!m.Success)
199+
{
200+
continue;
201+
}
202+
203+
if (m.Groups[QOP].Success)
204+
{
205+
dict.Add(QOP, m.Groups[QOP].Value);
206+
}
207+
if (m.Groups[REALM].Success)
208+
{
209+
dict.Add(REALM, m.Groups[REALM].Value);
210+
}
211+
if (m.Groups[NONCE].Success)
212+
{
213+
dict.Add(NONCE, m.Groups[NONCE].Value);
214+
}
215+
//if (m.Groups[STALE].Success)
216+
//{
217+
// dict.Add(STALE, m.Groups[STALE].Value);
218+
//}
219+
//if (m.Groups[OPAQUE].Success)
220+
//{
221+
// dict.Add(OPAQUE, m.Groups[OPAQUE].Value);
222+
//}
223+
//if (m.Groups[DOMAIN].Success)
224+
//{
225+
// dict.Add(DOMAIN, m.Groups[DOMAIN].Value);
226+
//}
227+
}
228+
229+
return dict;
195230
}
196231
}
197232

0 commit comments

Comments
 (0)