Skip to content

Commit ce39129

Browse files
committed
Refactored AuthenticationChallenge.cs and AuthenticationResponse.cs
1 parent df4b238 commit ce39129

File tree

9 files changed

+301
-251
lines changed

9 files changed

+301
-251
lines changed

websocket-sharp/AuthenticationChallenge.cs

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,40 @@
2929
using System;
3030
using System.Collections.Specialized;
3131
using System.Text;
32+
using WebSocketSharp.Net;
3233

3334
namespace WebSocketSharp
3435
{
3536
internal class AuthenticationChallenge
3637
{
3738
#region Private Fields
3839

39-
private NameValueCollection _parameters;
40-
private string _scheme;
40+
private NameValueCollection _parameters;
41+
private AuthenticationSchemes _scheme;
4142

4243
#endregion
4344

44-
#region Internal Constructors
45+
#region Private Constructors
4546

46-
internal AuthenticationChallenge (string scheme, string parameters)
47+
private AuthenticationChallenge (AuthenticationSchemes scheme, NameValueCollection parameters)
4748
{
4849
_scheme = scheme;
49-
_parameters = parameters.ParseAuthParameters ();
50+
_parameters = parameters;
51+
}
52+
53+
#endregion
54+
55+
#region Internal Constructors
56+
57+
internal AuthenticationChallenge (AuthenticationSchemes scheme, string realm)
58+
: this (scheme, new NameValueCollection ())
59+
{
60+
_parameters["realm"] = realm;
61+
if (scheme == AuthenticationSchemes.Digest) {
62+
_parameters["nonce"] = AuthenticationResponse.CreateNonceValue ();
63+
_parameters["algorithm"] = "MD5";
64+
_parameters["qop"] = "auth";
65+
}
5066
}
5167

5268
#endregion
@@ -65,66 +81,108 @@ internal NameValueCollection Parameters {
6581

6682
public string Algorithm {
6783
get {
68-
return _parameters ["algorithm"];
84+
return _parameters["algorithm"];
6985
}
7086
}
7187

7288
public string Domain {
7389
get {
74-
return _parameters ["domain"];
90+
return _parameters["domain"];
7591
}
7692
}
7793

7894
public string Nonce {
7995
get {
80-
return _parameters ["nonce"];
96+
return _parameters["nonce"];
8197
}
8298
}
8399

84100
public string Opaque {
85101
get {
86-
return _parameters ["opaque"];
102+
return _parameters["opaque"];
87103
}
88104
}
89105

90106
public string Qop {
91107
get {
92-
return _parameters ["qop"];
108+
return _parameters["qop"];
93109
}
94110
}
95111

96112
public string Realm {
97113
get {
98-
return _parameters ["realm"];
114+
return _parameters["realm"];
99115
}
100116
}
101117

102-
public string Scheme {
118+
public AuthenticationSchemes Scheme {
103119
get {
104120
return _scheme;
105121
}
106122
}
107123

108124
public string Stale {
109125
get {
110-
return _parameters ["stale"];
126+
return _parameters["stale"];
111127
}
112128
}
113129

114130
#endregion
115131

116-
#region Public Methods
132+
#region Internal Methods
117133

118-
public static AuthenticationChallenge Parse (string value)
134+
internal static AuthenticationChallenge CreateBasicChallenge (string realm)
119135
{
120-
var challenge = value.Split (new [] { ' ' }, 2);
121-
if (challenge.Length != 2)
136+
return new AuthenticationChallenge (AuthenticationSchemes.Basic, realm);
137+
}
138+
139+
internal static AuthenticationChallenge CreateDigestChallenge (string realm)
140+
{
141+
return new AuthenticationChallenge (AuthenticationSchemes.Digest, realm);
142+
}
143+
144+
internal static AuthenticationChallenge Parse (string value)
145+
{
146+
var chal = value.Split (new[] { ' ' }, 2);
147+
if (chal.Length != 2)
122148
return null;
123149

124-
var scheme = challenge [0].ToLower ();
125-
return scheme == "basic" || scheme == "digest"
126-
? new AuthenticationChallenge (scheme, challenge [1])
127-
: null;
150+
var scheme = chal[0].ToLower ();
151+
return scheme == "basic"
152+
? new AuthenticationChallenge (
153+
AuthenticationSchemes.Basic, AuthenticationResponse.ParseParameters (chal[1]))
154+
: scheme == "digest"
155+
? new AuthenticationChallenge (
156+
AuthenticationSchemes.Digest, AuthenticationResponse.ParseParameters (chal[1]))
157+
: null;
158+
}
159+
160+
internal string ToBasicString ()
161+
{
162+
return String.Format ("Basic realm=\"{0}\"", _parameters["realm"]);
163+
}
164+
165+
internal string ToDigestString ()
166+
{
167+
return String.Format (
168+
"Digest realm=\"{0}\", nonce=\"{1}\", algorithm={2}, qop=\"{3}\"",
169+
_parameters["realm"],
170+
_parameters["nonce"],
171+
_parameters["algorithm"],
172+
_parameters["qop"]);
173+
}
174+
175+
#endregion
176+
177+
#region Public Methods
178+
179+
public override string ToString ()
180+
{
181+
return _scheme == AuthenticationSchemes.Basic
182+
? ToBasicString ()
183+
: _scheme == AuthenticationSchemes.Digest
184+
? ToDigestString ()
185+
: String.Empty;
128186
}
129187

130188
#endregion

0 commit comments

Comments
 (0)