Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ GetDigestTokenForCredential()

static async Task< string > System.Net.Http.AuthenticationHelper.GetDigestTokenForCredential ( NetworkCredential credential,
HttpRequestMessage request,
DigestResponse digestResponse )
inlinestatic

Definition at line 471 of file AuthenticationHelper.cs.

472 {
474 string algorithm;
475 bool isAlgorithmSpecified = digestResponse.Parameters.TryGetValue("algorithm", out algorithm);
477 {
478 if (!algorithm.Equals("SHA-256", StringComparison.OrdinalIgnoreCase) && !algorithm.Equals("MD5", StringComparison.OrdinalIgnoreCase) && !algorithm.Equals("SHA-256-sess", StringComparison.OrdinalIgnoreCase) && !algorithm.Equals("MD5-sess", StringComparison.OrdinalIgnoreCase))
479 {
480 if (System.Net.NetEventSource.Log.IsEnabled())
481 {
482 System.Net.NetEventSource.Error(digestResponse, $"Algorithm not supported: {algorithm}", "GetDigestTokenForCredential");
483 }
484 return null;
485 }
486 }
487 else
488 {
489 algorithm = "MD5";
490 }
491 if (!digestResponse.Parameters.TryGetValue("nonce", out var nonce))
492 {
493 if (System.Net.NetEventSource.Log.IsEnabled())
494 {
495 System.Net.NetEventSource.Error(digestResponse, "Nonce missing", "GetDigestTokenForCredential");
496 }
497 return null;
498 }
499 digestResponse.Parameters.TryGetValue("opaque", out var opaque);
500 if (!digestResponse.Parameters.TryGetValue("realm", out var value))
501 {
502 if (System.Net.NetEventSource.Log.IsEnabled())
503 {
504 System.Net.NetEventSource.Error(digestResponse, "Realm missing", "GetDigestTokenForCredential");
505 }
506 return null;
507 }
508 if (digestResponse.Parameters.TryGetValue("userhash", out var value2) && value2 == "true")
509 {
510 sb.AppendKeyValue("username", ComputeHash(credential.UserName + ":" + value, algorithm));
511 sb.AppendKeyValue("userhash", value2, includeQuotes: false);
512 }
514 {
515 string value3 = HeaderUtilities.Encode5987(credential.UserName);
516 sb.AppendKeyValue("username*", value3, includeQuotes: false);
517 }
518 else
519 {
520 sb.AppendKeyValue("username", credential.UserName);
521 }
522 sb.AppendKeyValue("realm", value);
523 sb.AppendKeyValue("nonce", nonce);
524 sb.AppendKeyValue("uri", request.RequestUri.PathAndQuery);
525 string qop = "auth";
526 bool isQopSpecified = digestResponse.Parameters.ContainsKey("qop");
527 if (isQopSpecified)
528 {
529 int num = digestResponse.Parameters["qop"].IndexOf("auth-int", StringComparison.Ordinal);
530 if (num != -1)
531 {
532 int num2 = digestResponse.Parameters["qop"].IndexOf("auth", StringComparison.Ordinal);
533 if (num2 == num)
534 {
535 num2 = digestResponse.Parameters["qop"].IndexOf("auth", num + "auth-int".Length, StringComparison.Ordinal);
536 if (num2 == -1)
537 {
538 qop = "auth-int";
539 }
540 }
541 }
542 }
544 string a1 = credential.UserName + ":" + value + ":" + credential.Password;
545 if (algorithm.EndsWith("sess", StringComparison.OrdinalIgnoreCase))
546 {
547 a1 = ComputeHash(a1, algorithm) + ":" + nonce + ":" + cnonce;
548 }
549 string a2 = request.Method.Method + ":" + request.RequestUri.PathAndQuery;
550 if (qop == "auth-int")
551 {
552 string text = ((request.Content != null) ? (await request.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false)) : string.Empty);
553 string data = text;
554 a2 = a2 + ":" + ComputeHash(data, algorithm);
555 }
556 string value4 = ((!isQopSpecified) ? ComputeHash(ComputeHash(a1, algorithm) + ":" + nonce + ":" + ComputeHash(a2, algorithm), algorithm) : ComputeHash(ComputeHash(a1, algorithm) + ":" + nonce + ":00000001:" + cnonce + ":" + qop + ":" + ComputeHash(a2, algorithm), algorithm));
557 sb.AppendKeyValue("response", value4, includeQuotes: true, opaque != null || isAlgorithmSpecified || isQopSpecified);
558 if (opaque != null)
559 {
560 sb.AppendKeyValue("opaque", opaque, includeQuotes: true, isAlgorithmSpecified || isQopSpecified);
561 }
563 {
564 sb.AppendKeyValue("algorithm", algorithm, includeQuotes: false, isQopSpecified);
565 }
566 if (isQopSpecified)
567 {
568 sb.AppendKeyValue("qop", qop, includeQuotes: false);
569 sb.AppendKeyValue("nc", "00000001", includeQuotes: false);
570 sb.AppendKeyValue("cnonce", cnonce, includeQuotes: true, includeComma: false);
571 }
573 }
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
static string ComputeHash(string data, string algorithm)
static bool ContainsNonAscii(string input)
static string Encode5987(string input)
static readonly System.Net.NetEventSource Log
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static string GetStringAndRelease(StringBuilder sb)
static StringBuilder Acquire(int capacity=16)

References System.Text.StringBuilderCache.Acquire(), System.Net.Http.AuthenticationHelper.ComputeHash(), System.Collections.Generic.Dictionary< TKey, TValue >.ContainsKey(), System.Net.Http.Headers.HeaderUtilities.ContainsNonAscii(), System.Net.Http.Headers.HeaderUtilities.Encode5987(), System.Net.NetEventSource.Error(), System.Net.Http.AuthenticationHelper.GetRandomAlphaNumericString(), System.Text.StringBuilderCache.GetStringAndRelease(), System.Net.NetEventSource.Log, System.text, System.Collections.Generic.Dictionary< TKey, TValue >.TryGetValue(), and System.value.

Referenced by System.Net.Http.AuthenticationHelper.TrySetDigestAuthToken().