HttpDigestIdentity.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #region License
  2. /*
  3. * HttpDigestIdentity.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2014-2023 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Collections.Specialized;
  30. using System.Security.Principal;
  31. namespace WebSocketSharp.Net
  32. {
  33. /// <summary>
  34. /// Holds the username and other parameters from an HTTP Digest
  35. /// authentication attempt.
  36. /// </summary>
  37. public class HttpDigestIdentity : GenericIdentity
  38. {
  39. #region Private Fields
  40. private NameValueCollection _parameters;
  41. #endregion
  42. #region Internal Constructors
  43. internal HttpDigestIdentity (NameValueCollection parameters)
  44. : base (parameters["username"], "Digest")
  45. {
  46. _parameters = parameters;
  47. }
  48. #endregion
  49. #region Public Properties
  50. /// <summary>
  51. /// Gets the algorithm parameter from a digest authentication attempt.
  52. /// </summary>
  53. /// <value>
  54. /// A <see cref="string"/> that represents the algorithm parameter.
  55. /// </value>
  56. public string Algorithm {
  57. get {
  58. return _parameters["algorithm"];
  59. }
  60. }
  61. /// <summary>
  62. /// Gets the cnonce parameter from a digest authentication attempt.
  63. /// </summary>
  64. /// <value>
  65. /// A <see cref="string"/> that represents the cnonce parameter.
  66. /// </value>
  67. public string Cnonce {
  68. get {
  69. return _parameters["cnonce"];
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the nc parameter from a digest authentication attempt.
  74. /// </summary>
  75. /// <value>
  76. /// A <see cref="string"/> that represents the nc parameter.
  77. /// </value>
  78. public string Nc {
  79. get {
  80. return _parameters["nc"];
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the nonce parameter from a digest authentication attempt.
  85. /// </summary>
  86. /// <value>
  87. /// A <see cref="string"/> that represents the nonce parameter.
  88. /// </value>
  89. public string Nonce {
  90. get {
  91. return _parameters["nonce"];
  92. }
  93. }
  94. /// <summary>
  95. /// Gets the opaque parameter from a digest authentication attempt.
  96. /// </summary>
  97. /// <value>
  98. /// A <see cref="string"/> that represents the opaque parameter.
  99. /// </value>
  100. public string Opaque {
  101. get {
  102. return _parameters["opaque"];
  103. }
  104. }
  105. /// <summary>
  106. /// Gets the qop parameter from a digest authentication attempt.
  107. /// </summary>
  108. /// <value>
  109. /// A <see cref="string"/> that represents the qop parameter.
  110. /// </value>
  111. public string Qop {
  112. get {
  113. return _parameters["qop"];
  114. }
  115. }
  116. /// <summary>
  117. /// Gets the realm parameter from a digest authentication attempt.
  118. /// </summary>
  119. /// <value>
  120. /// A <see cref="string"/> that represents the realm parameter.
  121. /// </value>
  122. public string Realm {
  123. get {
  124. return _parameters["realm"];
  125. }
  126. }
  127. /// <summary>
  128. /// Gets the response parameter from a digest authentication attempt.
  129. /// </summary>
  130. /// <value>
  131. /// A <see cref="string"/> that represents the response parameter.
  132. /// </value>
  133. public string Response {
  134. get {
  135. return _parameters["response"];
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the uri parameter from a digest authentication attempt.
  140. /// </summary>
  141. /// <value>
  142. /// A <see cref="string"/> that represents the uri parameter.
  143. /// </value>
  144. public string Uri {
  145. get {
  146. return _parameters["uri"];
  147. }
  148. }
  149. #endregion
  150. #region Internal Methods
  151. internal bool IsValid (
  152. string password, string realm, string method, string entity
  153. )
  154. {
  155. var parameters = new NameValueCollection (_parameters);
  156. parameters["password"] = password;
  157. parameters["realm"] = realm;
  158. parameters["method"] = method;
  159. parameters["entity"] = entity;
  160. var expected = AuthenticationResponse.CreateRequestDigest (parameters);
  161. return _parameters["response"] == expected;
  162. }
  163. #endregion
  164. }
  165. }