Modified HTTP auth for WebSocketServer class

This commit is contained in:
sta
2014-11-22 14:46:42 +09:00
parent b521456dd6
commit 533ef2090a
4 changed files with 68 additions and 93 deletions

View File

@@ -122,7 +122,7 @@ namespace WebSocketSharp.Net
var req = context.Request;
var realm = listener.Realm;
var user = createUser (
var user = HttpUtility.CreateUser (
req.Headers["Authorization"], schm, realm, req.HttpMethod, listener.UserCredentialsFinder);
if (user != null && user.Identity.IsAuthenticated) {
@@ -165,46 +165,6 @@ namespace WebSocketSharp.Net
null);
}
private static IPrincipal createUser (
string response,
AuthenticationSchemes scheme,
string realm,
string method,
Func<IIdentity, NetworkCredential> credentialsFinder)
{
if (response == null ||
!response.StartsWith (scheme.ToString (), StringComparison.OrdinalIgnoreCase))
return null;
var res = AuthenticationResponse.Parse (response);
if (res == null)
return null;
var id = res.ToIdentity ();
if (id == null)
return null;
NetworkCredential cred = null;
try {
cred = credentialsFinder (id);
}
catch {
}
if (cred == null)
return null;
var valid = scheme == AuthenticationSchemes.Basic
? ((HttpBasicIdentity) id).Password == cred.Password
: scheme == AuthenticationSchemes.Digest
? ((HttpDigestIdentity) id).IsValid (cred.Password, realm, method, null)
: false;
return valid
? new GenericPrincipal (id, cred.Roles)
: null;
}
#endregion
#region Internal Methods

View File

@@ -46,6 +46,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Security.Principal;
using System.Text;
namespace WebSocketSharp.Net
@@ -548,6 +549,46 @@ namespace WebSocketSharp.Net
return res;
}
internal static IPrincipal CreateUser (
string response,
AuthenticationSchemes scheme,
string realm,
string method,
Func<IIdentity, NetworkCredential> credentialsFinder)
{
if (response == null ||
!response.StartsWith (scheme.ToString (), StringComparison.OrdinalIgnoreCase))
return null;
var res = AuthenticationResponse.Parse (response);
if (res == null)
return null;
var id = res.ToIdentity ();
if (id == null)
return null;
NetworkCredential cred = null;
try {
cred = credentialsFinder (id);
}
catch {
}
if (cred == null)
return null;
var valid = scheme == AuthenticationSchemes.Basic
? ((HttpBasicIdentity) id).Password == cred.Password
: scheme == AuthenticationSchemes.Digest
? ((HttpDigestIdentity) id).IsValid (cred.Password, realm, method, null)
: false;
return valid
? new GenericPrincipal (id, cred.Roles)
: null;
}
internal static Encoding GetEncoding (string contentType)
{
var parts = contentType.Split (';');

View File

@@ -105,6 +105,12 @@ namespace WebSocketSharp.Net.WebSockets
#region Internal Properties
internal string HttpMethod {
get {
return _request.HttpMethod;
}
}
internal Stream Stream {
get {
return _stream;
@@ -159,7 +165,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public override bool IsAuthenticated {
get {
return _user != null && _user.Identity.IsAuthenticated;
return _user != null;
}
}
@@ -304,7 +310,7 @@ namespace WebSocketSharp.Net.WebSockets
/// Gets the client information (identity, authentication, and security roles).
/// </summary>
/// <value>
/// A <see cref="IPrincipal"/> that represents the client information.
/// A <see cref="IPrincipal"/> instance that represents the client information.
/// </value>
public override IPrincipal User {
get {
@@ -359,38 +365,9 @@ namespace WebSocketSharp.Net.WebSockets
_request = HttpRequest.Read (_stream, 15000);
}
internal void SetUser (
AuthenticationSchemes scheme,
string realm,
Func<IIdentity, NetworkCredential> credentialsFinder)
internal void SetUser (IPrincipal value)
{
var authRes = _request.AuthenticationResponse;
if (authRes == null)
return;
var id = authRes.ToIdentity ();
if (id == null)
return;
NetworkCredential cred = null;
try {
cred = credentialsFinder (id);
}
catch {
}
if (cred == null)
return;
var valid = scheme == AuthenticationSchemes.Basic
? ((HttpBasicIdentity) id).Password == cred.Password
: scheme == AuthenticationSchemes.Digest
? ((HttpDigestIdentity) id).IsValid (
cred.Password, realm, _request.HttpMethod, null)
: false;
if (valid)
_user = new GenericPrincipal (id, cred.Roles);
_user = value;
}
#endregion