Refactored AuthenticationResponse.cs

This commit is contained in:
sta 2014-06-22 22:24:51 +09:00
parent 7ed991d72e
commit 5ab0abac8f
2 changed files with 13 additions and 26 deletions

View File

@ -4,7 +4,7 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2013 sta.blockhead * Copyright (c) 2013-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -46,8 +46,7 @@ namespace WebSocketSharp
#region Private Constructors #region Private Constructors
private AuthenticationResponse ( private AuthenticationResponse (string authScheme, NameValueCollection authParams)
string authScheme, NameValueCollection authParams)
{ {
_scheme = authScheme; _scheme = authScheme;
_params = authParams; _params = authParams;
@ -184,28 +183,19 @@ namespace WebSocketSharp
#region Private Methods #region Private Methods
private static bool contains (string [] array, string item)
{
foreach (var i in array)
if (i.Trim ().ToLower () == item)
return true;
return false;
}
private void initAsDigest () private void initAsDigest ()
{ {
var qops = _params ["qop"]; var qops = _params ["qop"];
if (qops != null) { if (qops != null) {
var qop = "auth"; if (qops.Split (',').Contains (qop => qop.Trim ().ToLower () == "auth")) {
if (contains (qops.Split (','), qop)) { _params ["qop"] = "auth";
_params ["qop"] = qop;
_params ["nc"] = String.Format ("{0:x8}", ++_nonceCount); _params ["nc"] = String.Format ("{0:x8}", ++_nonceCount);
_params ["cnonce"] = HttpUtility.CreateNonceValue (); _params ["cnonce"] = HttpUtility.CreateNonceValue ();
} }
else else {
_params ["qop"] = null; _params ["qop"] = null;
} }
}
_params ["method"] = "GET"; _params ["method"] = "GET";
_params ["response"] = HttpUtility.CreateRequestDigest (_params); _params ["response"] = HttpUtility.CreateRequestDigest (_params);
@ -218,7 +208,7 @@ namespace WebSocketSharp
public static AuthenticationResponse Parse (string value) public static AuthenticationResponse Parse (string value)
{ {
try { try {
var credentials = value.Split (new char [] { ' ' }, 2); var credentials = value.Split (new [] { ' ' }, 2);
if (credentials.Length != 2) if (credentials.Length != 2)
return null; return null;
@ -227,8 +217,7 @@ namespace WebSocketSharp
? new AuthenticationResponse ( ? new AuthenticationResponse (
scheme, credentials [1].ParseBasicAuthResponseParams ()) scheme, credentials [1].ParseBasicAuthResponseParams ())
: scheme == "digest" : scheme == "digest"
? new AuthenticationResponse ( ? new AuthenticationResponse (scheme, credentials [1].ParseAuthParams ())
scheme, credentials [1].ParseAuthParams ())
: null; : null;
} }
catch { catch {
@ -240,8 +229,7 @@ namespace WebSocketSharp
public IIdentity ToIdentity () public IIdentity ToIdentity ()
{ {
return _scheme == "basic" return _scheme == "basic"
? new HttpBasicIdentity ( ? new HttpBasicIdentity (_params ["username"], _params ["password"]) as IIdentity
_params ["username"], _params ["password"]) as IIdentity
: _scheme == "digest" : _scheme == "digest"
? new HttpDigestIdentity (_params) ? new HttpDigestIdentity (_params)
: null; : null;
@ -250,8 +238,7 @@ namespace WebSocketSharp
public override string ToString () public override string ToString ()
{ {
return _scheme == "basic" return _scheme == "basic"
? HttpUtility.CreateBasicAuthCredentials ( ? HttpUtility.CreateBasicAuthCredentials (_params ["username"], _params ["password"])
_params ["username"], _params ["password"])
: _scheme == "digest" : _scheme == "digest"
? HttpUtility.CreateDigestAuthCredentials (_params) ? HttpUtility.CreateDigestAuthCredentials (_params)
: String.Empty; : String.Empty;

View File

@ -338,10 +338,10 @@ namespace WebSocketSharp
: stream.ToByteArray (); : stream.ToByteArray ();
} }
internal static bool Contains<T> (this IEnumerable<T> source, Func<T, bool> comparer) internal static bool Contains<T> (this IEnumerable<T> source, Func<T, bool> condition)
{ {
foreach (T value in source) foreach (T elm in source)
if (comparer (value)) if (condition (elm))
return true; return true;
return false; return false;