Added AuthenticationBase.cs
This commit is contained in:
parent
5f0b36997b
commit
d85dc01897
104
websocket-sharp/Net/AuthenticationBase.cs
Normal file
104
websocket-sharp/Net/AuthenticationBase.cs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#region License
|
||||||
|
/*
|
||||||
|
* AuthenticationBase.cs
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2014 sta.blockhead
|
||||||
|
*
|
||||||
|
* 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.Text;
|
||||||
|
|
||||||
|
namespace WebSocketSharp.Net
|
||||||
|
{
|
||||||
|
internal abstract class AuthenticationBase
|
||||||
|
{
|
||||||
|
#region Private Fields
|
||||||
|
|
||||||
|
private AuthenticationSchemes _scheme;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Internal Fields
|
||||||
|
|
||||||
|
internal NameValueCollection Parameters;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Protected Constructors
|
||||||
|
|
||||||
|
protected AuthenticationBase (AuthenticationSchemes scheme, NameValueCollection parameters)
|
||||||
|
{
|
||||||
|
_scheme = scheme;
|
||||||
|
Parameters = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Properties
|
||||||
|
|
||||||
|
public AuthenticationSchemes Scheme {
|
||||||
|
get {
|
||||||
|
return _scheme;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Internal Methods
|
||||||
|
|
||||||
|
internal static string CreateNonceValue ()
|
||||||
|
{
|
||||||
|
var src = new byte[16];
|
||||||
|
var rand = new Random ();
|
||||||
|
rand.NextBytes (src);
|
||||||
|
|
||||||
|
var res = new StringBuilder (32);
|
||||||
|
foreach (var b in src)
|
||||||
|
res.Append (b.ToString ("x2"));
|
||||||
|
|
||||||
|
return res.ToString ();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static NameValueCollection ParseParameters (string value)
|
||||||
|
{
|
||||||
|
var res = new NameValueCollection ();
|
||||||
|
foreach (var param in value.SplitHeaderValue (',')) {
|
||||||
|
var i = param.IndexOf ('=');
|
||||||
|
var name = i > 0 ? param.Substring (0, i).Trim () : null;
|
||||||
|
var val = i < 0
|
||||||
|
? param.Trim ().Trim ('"')
|
||||||
|
: i < param.Length - 1
|
||||||
|
? param.Substring (i + 1).Trim ().Trim ('"')
|
||||||
|
: String.Empty;
|
||||||
|
|
||||||
|
res.Add (name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
@ -32,21 +32,13 @@ using System.Text;
|
|||||||
|
|
||||||
namespace WebSocketSharp.Net
|
namespace WebSocketSharp.Net
|
||||||
{
|
{
|
||||||
internal class AuthenticationChallenge
|
internal class AuthenticationChallenge : AuthenticationBase
|
||||||
{
|
{
|
||||||
#region Private Fields
|
|
||||||
|
|
||||||
private NameValueCollection _parameters;
|
|
||||||
private AuthenticationSchemes _scheme;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Constructors
|
#region Private Constructors
|
||||||
|
|
||||||
private AuthenticationChallenge (AuthenticationSchemes scheme, NameValueCollection parameters)
|
private AuthenticationChallenge (AuthenticationSchemes scheme, NameValueCollection parameters)
|
||||||
|
: base (scheme, parameters)
|
||||||
{
|
{
|
||||||
_scheme = scheme;
|
|
||||||
_parameters = parameters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -54,23 +46,13 @@ namespace WebSocketSharp.Net
|
|||||||
#region Internal Constructors
|
#region Internal Constructors
|
||||||
|
|
||||||
internal AuthenticationChallenge (AuthenticationSchemes scheme, string realm)
|
internal AuthenticationChallenge (AuthenticationSchemes scheme, string realm)
|
||||||
: this (scheme, new NameValueCollection ())
|
: base (scheme, new NameValueCollection ())
|
||||||
{
|
{
|
||||||
_parameters["realm"] = realm;
|
Parameters["realm"] = realm;
|
||||||
if (scheme == AuthenticationSchemes.Digest) {
|
if (scheme == AuthenticationSchemes.Digest) {
|
||||||
_parameters["nonce"] = AuthenticationResponse.CreateNonceValue ();
|
Parameters["nonce"] = CreateNonceValue ();
|
||||||
_parameters["algorithm"] = "MD5";
|
Parameters["algorithm"] = "MD5";
|
||||||
_parameters["qop"] = "auth";
|
Parameters["qop"] = "auth";
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Internal Properties
|
|
||||||
|
|
||||||
internal NameValueCollection Parameters {
|
|
||||||
get {
|
|
||||||
return _parameters;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,49 +62,43 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
public string Algorithm {
|
public string Algorithm {
|
||||||
get {
|
get {
|
||||||
return _parameters["algorithm"];
|
return Parameters["algorithm"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Domain {
|
public string Domain {
|
||||||
get {
|
get {
|
||||||
return _parameters["domain"];
|
return Parameters["domain"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Nonce {
|
public string Nonce {
|
||||||
get {
|
get {
|
||||||
return _parameters["nonce"];
|
return Parameters["nonce"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Opaque {
|
public string Opaque {
|
||||||
get {
|
get {
|
||||||
return _parameters["opaque"];
|
return Parameters["opaque"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Qop {
|
public string Qop {
|
||||||
get {
|
get {
|
||||||
return _parameters["qop"];
|
return Parameters["qop"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Realm {
|
public string Realm {
|
||||||
get {
|
get {
|
||||||
return _parameters["realm"];
|
return Parameters["realm"];
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public AuthenticationSchemes Scheme {
|
|
||||||
get {
|
|
||||||
return _scheme;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Stale {
|
public string Stale {
|
||||||
get {
|
get {
|
||||||
return _parameters["stale"];
|
return Parameters["stale"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,29 +122,36 @@ namespace WebSocketSharp.Net
|
|||||||
if (chal.Length != 2)
|
if (chal.Length != 2)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var scheme = chal[0].ToLower ();
|
var schm = chal[0].ToLower ();
|
||||||
return scheme == "basic"
|
return schm == "basic"
|
||||||
? new AuthenticationChallenge (
|
? new AuthenticationChallenge (
|
||||||
AuthenticationSchemes.Basic, AuthenticationResponse.ParseParameters (chal[1]))
|
AuthenticationSchemes.Basic, ParseParameters (chal[1]))
|
||||||
: scheme == "digest"
|
: schm == "digest"
|
||||||
? new AuthenticationChallenge (
|
? new AuthenticationChallenge (
|
||||||
AuthenticationSchemes.Digest, AuthenticationResponse.ParseParameters (chal[1]))
|
AuthenticationSchemes.Digest, ParseParameters (chal[1]))
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal string ToBasicString ()
|
internal string ToBasicString ()
|
||||||
{
|
{
|
||||||
return String.Format ("Basic realm=\"{0}\"", _parameters["realm"]);
|
return String.Format ("Basic realm=\"{0}\"", Parameters["realm"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal string ToDigestString ()
|
internal string ToDigestString ()
|
||||||
{
|
{
|
||||||
return String.Format (
|
var output = new StringBuilder (64);
|
||||||
"Digest realm=\"{0}\", nonce=\"{1}\", algorithm={2}, qop=\"{3}\"",
|
output.AppendFormat ("Digest realm=\"{0}\"", Parameters["realm"]);
|
||||||
_parameters["realm"],
|
output.AppendFormat (", nonce=\"{0}\"", Parameters["nonce"]);
|
||||||
_parameters["nonce"],
|
|
||||||
_parameters["algorithm"],
|
var algo = Parameters["algorithm"];
|
||||||
_parameters["qop"]);
|
if (algo != null)
|
||||||
|
output.AppendFormat (", algorithm={0}", algo);
|
||||||
|
|
||||||
|
var qop = Parameters["qop"];
|
||||||
|
if (qop != null)
|
||||||
|
output.AppendFormat (", qop=\"{0}\"", qop);
|
||||||
|
|
||||||
|
return output.ToString ();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -177,9 +160,10 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
public override string ToString ()
|
public override string ToString ()
|
||||||
{
|
{
|
||||||
return _scheme == AuthenticationSchemes.Basic
|
var schm = Scheme;
|
||||||
|
return schm == AuthenticationSchemes.Basic
|
||||||
? ToBasicString ()
|
? ToBasicString ()
|
||||||
: _scheme == AuthenticationSchemes.Digest
|
: schm == AuthenticationSchemes.Digest
|
||||||
? ToDigestString ()
|
? ToDigestString ()
|
||||||
: String.Empty;
|
: String.Empty;
|
||||||
}
|
}
|
||||||
|
@ -38,22 +38,19 @@ using System.Text;
|
|||||||
|
|
||||||
namespace WebSocketSharp.Net
|
namespace WebSocketSharp.Net
|
||||||
{
|
{
|
||||||
internal class AuthenticationResponse
|
internal class AuthenticationResponse : AuthenticationBase
|
||||||
{
|
{
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
private uint _nonceCount;
|
private uint _nonceCount;
|
||||||
private NameValueCollection _parameters;
|
|
||||||
private AuthenticationSchemes _scheme;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private Constructors
|
#region Private Constructors
|
||||||
|
|
||||||
private AuthenticationResponse (AuthenticationSchemes scheme, NameValueCollection parameters)
|
private AuthenticationResponse (AuthenticationSchemes scheme, NameValueCollection parameters)
|
||||||
|
: base (scheme, parameters)
|
||||||
{
|
{
|
||||||
_scheme = scheme;
|
|
||||||
_parameters = parameters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -76,11 +73,11 @@ namespace WebSocketSharp.Net
|
|||||||
NameValueCollection parameters,
|
NameValueCollection parameters,
|
||||||
NetworkCredential credentials,
|
NetworkCredential credentials,
|
||||||
uint nonceCount)
|
uint nonceCount)
|
||||||
: this (scheme, parameters)
|
: base (scheme, parameters)
|
||||||
{
|
{
|
||||||
_parameters["username"] = credentials.UserName;
|
Parameters["username"] = credentials.UserName;
|
||||||
_parameters["password"] = credentials.Password;
|
Parameters["password"] = credentials.Password;
|
||||||
_parameters["uri"] = credentials.Domain;
|
Parameters["uri"] = credentials.Domain;
|
||||||
_nonceCount = nonceCount;
|
_nonceCount = nonceCount;
|
||||||
if (scheme == AuthenticationSchemes.Digest)
|
if (scheme == AuthenticationSchemes.Digest)
|
||||||
initAsDigest ();
|
initAsDigest ();
|
||||||
@ -98,85 +95,73 @@ namespace WebSocketSharp.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal NameValueCollection Parameters {
|
|
||||||
get {
|
|
||||||
return _parameters;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Properties
|
#region Public Properties
|
||||||
|
|
||||||
public string Algorithm {
|
public string Algorithm {
|
||||||
get {
|
get {
|
||||||
return _parameters["algorithm"];
|
return Parameters["algorithm"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Cnonce {
|
public string Cnonce {
|
||||||
get {
|
get {
|
||||||
return _parameters["cnonce"];
|
return Parameters["cnonce"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Nc {
|
public string Nc {
|
||||||
get {
|
get {
|
||||||
return _parameters["nc"];
|
return Parameters["nc"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Nonce {
|
public string Nonce {
|
||||||
get {
|
get {
|
||||||
return _parameters["nonce"];
|
return Parameters["nonce"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Opaque {
|
public string Opaque {
|
||||||
get {
|
get {
|
||||||
return _parameters["opaque"];
|
return Parameters["opaque"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Password {
|
public string Password {
|
||||||
get {
|
get {
|
||||||
return _parameters["password"];
|
return Parameters["password"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Qop {
|
public string Qop {
|
||||||
get {
|
get {
|
||||||
return _parameters["qop"];
|
return Parameters["qop"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Realm {
|
public string Realm {
|
||||||
get {
|
get {
|
||||||
return _parameters["realm"];
|
return Parameters["realm"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Response {
|
public string Response {
|
||||||
get {
|
get {
|
||||||
return _parameters["response"];
|
return Parameters["response"];
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public AuthenticationSchemes Scheme {
|
|
||||||
get {
|
|
||||||
return _scheme;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Uri {
|
public string Uri {
|
||||||
get {
|
get {
|
||||||
return _parameters["uri"];
|
return Parameters["uri"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string UserName {
|
public string UserName {
|
||||||
get {
|
get {
|
||||||
return _parameters["username"];
|
return Parameters["username"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,7 +188,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private static string createA2 (string method, string uri, string entity)
|
private static string createA2 (string method, string uri, string entity)
|
||||||
{
|
{
|
||||||
return String.Format ("{0}:{1}:{2}", method, uri, entity);
|
return String.Format ("{0}:{1}:{2}", method, uri, hash (entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string hash (string value)
|
private static string hash (string value)
|
||||||
@ -221,55 +206,42 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private void initAsDigest ()
|
private void initAsDigest ()
|
||||||
{
|
{
|
||||||
var qops = _parameters["qop"];
|
var qops = Parameters["qop"];
|
||||||
if (qops != null) {
|
if (qops != null) {
|
||||||
if (qops.Split (',').Contains (qop => qop.Trim ().ToLower () == "auth")) {
|
if (qops.Split (',').Contains (qop => qop.Trim ().ToLower () == "auth")) {
|
||||||
_parameters["qop"] = "auth";
|
Parameters["qop"] = "auth";
|
||||||
_parameters["nc"] = String.Format ("{0:x8}", ++_nonceCount);
|
Parameters["nc"] = String.Format ("{0:x8}", ++_nonceCount);
|
||||||
_parameters["cnonce"] = CreateNonceValue ();
|
Parameters["cnonce"] = CreateNonceValue ();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_parameters["qop"] = null;
|
Parameters["qop"] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_parameters["method"] = "GET";
|
Parameters["method"] = "GET";
|
||||||
_parameters["response"] = CreateRequestDigest (_parameters);
|
Parameters["response"] = CreateRequestDigest (Parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Internal Methods
|
#region Internal Methods
|
||||||
|
|
||||||
internal static string CreateNonceValue ()
|
|
||||||
{
|
|
||||||
var src = new byte[16];
|
|
||||||
var rand = new Random ();
|
|
||||||
rand.NextBytes (src);
|
|
||||||
|
|
||||||
var res = new StringBuilder (32);
|
|
||||||
foreach (var b in src)
|
|
||||||
res.Append (b.ToString ("x2"));
|
|
||||||
|
|
||||||
return res.ToString ();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static string CreateRequestDigest (NameValueCollection parameters)
|
internal static string CreateRequestDigest (NameValueCollection parameters)
|
||||||
{
|
{
|
||||||
var username = parameters["username"];
|
var user = parameters["username"];
|
||||||
var password = parameters["password"];
|
var pass = parameters["password"];
|
||||||
var realm = parameters["realm"];
|
var realm = parameters["realm"];
|
||||||
var nonce = parameters["nonce"];
|
var nonce = parameters["nonce"];
|
||||||
var uri = parameters["uri"];
|
var uri = parameters["uri"];
|
||||||
var algorithm = parameters["algorithm"];
|
var algo = parameters["algorithm"];
|
||||||
var qop = parameters["qop"];
|
var qop = parameters["qop"];
|
||||||
var nc = parameters["nc"];
|
var nc = parameters["nc"];
|
||||||
var cnonce = parameters["cnonce"];
|
var cnonce = parameters["cnonce"];
|
||||||
var method = parameters["method"];
|
var method = parameters["method"];
|
||||||
|
|
||||||
var a1 = algorithm != null && algorithm.ToLower () == "md5-sess"
|
var a1 = algo != null && algo.ToLower () == "md5-sess"
|
||||||
? createA1 (username, password, realm, nonce, cnonce)
|
? createA1 (user, pass, realm, nonce, cnonce)
|
||||||
: createA1 (username, password, realm);
|
: createA1 (user, pass, realm);
|
||||||
|
|
||||||
var a2 = qop != null && qop.ToLower () == "auth-int"
|
var a2 = qop != null && qop.ToLower () == "auth-int"
|
||||||
? createA2 (method, uri, parameters["entity"])
|
? createA2 (method, uri, parameters["entity"])
|
||||||
@ -290,11 +262,11 @@ namespace WebSocketSharp.Net
|
|||||||
if (cred.Length != 2)
|
if (cred.Length != 2)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var scheme = cred[0].ToLower ();
|
var schm = cred[0].ToLower ();
|
||||||
return scheme == "basic"
|
return schm == "basic"
|
||||||
? new AuthenticationResponse (
|
? new AuthenticationResponse (
|
||||||
AuthenticationSchemes.Basic, ParseBasicCredentials (cred[1]))
|
AuthenticationSchemes.Basic, ParseBasicCredentials (cred[1]))
|
||||||
: scheme == "digest"
|
: schm == "digest"
|
||||||
? new AuthenticationResponse (
|
? new AuthenticationResponse (
|
||||||
AuthenticationSchemes.Digest, ParseParameters (cred[1]))
|
AuthenticationSchemes.Digest, ParseParameters (cred[1]))
|
||||||
: null;
|
: null;
|
||||||
@ -327,27 +299,9 @@ namespace WebSocketSharp.Net
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static NameValueCollection ParseParameters (string value)
|
|
||||||
{
|
|
||||||
var res = new NameValueCollection ();
|
|
||||||
foreach (var param in value.SplitHeaderValue (',')) {
|
|
||||||
var i = param.IndexOf ('=');
|
|
||||||
var name = i > 0 ? param.Substring (0, i).Trim () : null;
|
|
||||||
var val = i < 0
|
|
||||||
? param.Trim ().Trim ('"')
|
|
||||||
: i < param.Length - 1
|
|
||||||
? param.Substring (i + 1).Trim ().Trim ('"')
|
|
||||||
: String.Empty;
|
|
||||||
|
|
||||||
res.Add (name, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal string ToBasicString ()
|
internal string ToBasicString ()
|
||||||
{
|
{
|
||||||
var userPass = String.Format ("{0}:{1}", _parameters["username"], _parameters["password"]);
|
var userPass = String.Format ("{0}:{1}", Parameters["username"], Parameters["password"]);
|
||||||
var cred = Convert.ToBase64String (Encoding.UTF8.GetBytes (userPass));
|
var cred = Convert.ToBase64String (Encoding.UTF8.GetBytes (userPass));
|
||||||
|
|
||||||
return "Basic " + cred;
|
return "Basic " + cred;
|
||||||
@ -356,25 +310,25 @@ namespace WebSocketSharp.Net
|
|||||||
internal string ToDigestString ()
|
internal string ToDigestString ()
|
||||||
{
|
{
|
||||||
var res = new StringBuilder (64);
|
var res = new StringBuilder (64);
|
||||||
res.AppendFormat ("username=\"{0}\"", _parameters["username"]);
|
res.AppendFormat ("username=\"{0}\"", Parameters["username"]);
|
||||||
res.AppendFormat (", realm=\"{0}\"", _parameters["realm"]);
|
res.AppendFormat (", realm=\"{0}\"", Parameters["realm"]);
|
||||||
res.AppendFormat (", nonce=\"{0}\"", _parameters["nonce"]);
|
res.AppendFormat (", nonce=\"{0}\"", Parameters["nonce"]);
|
||||||
res.AppendFormat (", uri=\"{0}\"", _parameters["uri"]);
|
res.AppendFormat (", uri=\"{0}\"", Parameters["uri"]);
|
||||||
|
|
||||||
var algorithm = _parameters["algorithm"];
|
var algo = Parameters["algorithm"];
|
||||||
if (algorithm != null)
|
if (algo != null)
|
||||||
res.AppendFormat (", algorithm={0}", algorithm);
|
res.AppendFormat (", algorithm={0}", algo);
|
||||||
|
|
||||||
res.AppendFormat (", response=\"{0}\"", _parameters["response"]);
|
res.AppendFormat (", response=\"{0}\"", Parameters["response"]);
|
||||||
|
|
||||||
var qop = _parameters["qop"];
|
var qop = Parameters["qop"];
|
||||||
if (qop != null) {
|
if (qop != null) {
|
||||||
res.AppendFormat (", qop={0}", qop);
|
res.AppendFormat (", qop={0}", qop);
|
||||||
res.AppendFormat (", nc={0}", _parameters["nc"]);
|
res.AppendFormat (", nc={0}", Parameters["nc"]);
|
||||||
res.AppendFormat (", cnonce=\"{0}\"", _parameters["cnonce"]);
|
res.AppendFormat (", cnonce=\"{0}\"", Parameters["cnonce"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
var opaque = _parameters["opaque"];
|
var opaque = Parameters["opaque"];
|
||||||
if (opaque != null)
|
if (opaque != null)
|
||||||
res.AppendFormat (", opaque=\"{0}\"", opaque);
|
res.AppendFormat (", opaque=\"{0}\"", opaque);
|
||||||
|
|
||||||
@ -387,18 +341,20 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
public IIdentity ToIdentity ()
|
public IIdentity ToIdentity ()
|
||||||
{
|
{
|
||||||
return _scheme == AuthenticationSchemes.Basic
|
var schm = Scheme;
|
||||||
? new HttpBasicIdentity (_parameters["username"], _parameters["password"]) as IIdentity
|
return schm == AuthenticationSchemes.Basic
|
||||||
: _scheme == AuthenticationSchemes.Digest
|
? new HttpBasicIdentity (Parameters["username"], Parameters["password"]) as IIdentity
|
||||||
? new HttpDigestIdentity (_parameters)
|
: schm == AuthenticationSchemes.Digest
|
||||||
|
? new HttpDigestIdentity (Parameters)
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString ()
|
public override string ToString ()
|
||||||
{
|
{
|
||||||
return _scheme == AuthenticationSchemes.Basic
|
var schm = Scheme;
|
||||||
|
return schm == AuthenticationSchemes.Basic
|
||||||
? ToBasicString ()
|
? ToBasicString ()
|
||||||
: _scheme == AuthenticationSchemes.Digest
|
: schm == AuthenticationSchemes.Digest
|
||||||
? ToDigestString ()
|
? ToDigestString ()
|
||||||
: String.Empty;
|
: String.Empty;
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,7 @@
|
|||||||
<Compile Include="WebSocketFrame.cs" />
|
<Compile Include="WebSocketFrame.cs" />
|
||||||
<Compile Include="Net\AuthenticationChallenge.cs" />
|
<Compile Include="Net\AuthenticationChallenge.cs" />
|
||||||
<Compile Include="Net\AuthenticationResponse.cs" />
|
<Compile Include="Net\AuthenticationResponse.cs" />
|
||||||
|
<Compile Include="Net\AuthenticationBase.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user