2014-01-01 20:43:18 +08:00
|
|
|
#region License
|
|
|
|
/*
|
|
|
|
* HttpDigestIdentity.cs
|
|
|
|
*
|
|
|
|
* The MIT License
|
|
|
|
*
|
2017-04-23 17:29:17 +08:00
|
|
|
* Copyright (c) 2014-2017 sta.blockhead
|
2014-01-01 20:43:18 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
|
|
|
namespace WebSocketSharp.Net
|
|
|
|
{
|
|
|
|
/// <summary>
|
2017-04-20 14:17:59 +08:00
|
|
|
/// Holds the username and other parameters from
|
|
|
|
/// an HTTP Digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
public class HttpDigestIdentity : GenericIdentity
|
|
|
|
{
|
|
|
|
#region Private Fields
|
|
|
|
|
2014-06-26 14:38:06 +08:00
|
|
|
private NameValueCollection _parameters;
|
2014-01-01 20:43:18 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Internal Constructors
|
|
|
|
|
2014-06-26 14:38:06 +08:00
|
|
|
internal HttpDigestIdentity (NameValueCollection parameters)
|
2017-04-21 16:39:22 +08:00
|
|
|
: base (parameters["username"], "Digest")
|
2014-01-01 20:43:18 +08:00
|
|
|
{
|
2014-06-26 14:38:06 +08:00
|
|
|
_parameters = parameters;
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public Properties
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:24:48 +08:00
|
|
|
/// Gets the algorithm parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the algorithm parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Algorithm {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["algorithm"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:27:03 +08:00
|
|
|
/// Gets the cnonce parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the cnonce parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Cnonce {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["cnonce"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:31:05 +08:00
|
|
|
/// Gets the nc parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the nc parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Nc {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["nc"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:32:53 +08:00
|
|
|
/// Gets the nonce parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the nonce parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Nonce {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["nonce"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:34:37 +08:00
|
|
|
/// Gets the opaque parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the opaque parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Opaque {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["opaque"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:36:32 +08:00
|
|
|
/// Gets the qop parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the qop parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Qop {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["qop"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:38:11 +08:00
|
|
|
/// Gets the realm parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the realm parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Realm {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["realm"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:40:56 +08:00
|
|
|
/// Gets the response parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the response parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Response {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["response"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-20 14:42:45 +08:00
|
|
|
/// Gets the uri parameter from a digest authentication attempt.
|
2014-01-01 20:43:18 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="string"/> that represents the uri parameter.
|
|
|
|
/// </value>
|
|
|
|
public string Uri {
|
|
|
|
get {
|
2017-04-21 16:39:22 +08:00
|
|
|
return _parameters["uri"];
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Internal Methods
|
|
|
|
|
2017-04-22 17:24:23 +08:00
|
|
|
internal bool IsValid (
|
|
|
|
string password, string realm, string method, string entity
|
|
|
|
)
|
2014-01-01 20:43:18 +08:00
|
|
|
{
|
2017-04-22 17:24:23 +08:00
|
|
|
var copied = new NameValueCollection (_parameters);
|
|
|
|
copied["password"] = password;
|
|
|
|
copied["realm"] = realm;
|
|
|
|
copied["method"] = method;
|
|
|
|
copied["entity"] = entity;
|
|
|
|
|
|
|
|
var expected = AuthenticationResponse.CreateRequestDigest (copied);
|
|
|
|
return _parameters["response"] == expected;
|
2014-01-01 20:43:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|