Modified HTTP auth for HttpListener class

This commit is contained in:
sta 2014-11-23 11:23:06 +09:00
parent 533ef2090a
commit 5003c10e0c
4 changed files with 18 additions and 24 deletions

View File

@ -108,34 +108,30 @@ namespace WebSocketSharp.Net
#region Private Methods
private static bool authenticate (HttpListenerContext context)
private static bool authenticate (
HttpListenerContext context,
AuthenticationSchemes scheme,
string realm,
Func<IIdentity, NetworkCredential> credentialsFinder)
{
var listener = context.Listener;
var schm = listener.SelectAuthenticationScheme (context);
if (schm == AuthenticationSchemes.Anonymous)
return true;
if (schm == AuthenticationSchemes.None) {
if (scheme == AuthenticationSchemes.None) {
context.Response.Close (HttpStatusCode.Forbidden);
return false;
}
var req = context.Request;
var realm = listener.Realm;
var user = HttpUtility.CreateUser (
req.Headers["Authorization"], schm, realm, req.HttpMethod, listener.UserCredentialsFinder);
req.Headers["Authorization"], scheme, realm, req.HttpMethod, credentialsFinder);
if (user != null && user.Identity.IsAuthenticated) {
context.User = user;
req.IsAuthenticated = true;
return true;
}
if (schm == AuthenticationSchemes.Basic)
if (scheme == AuthenticationSchemes.Basic)
context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateBasicChallenge (realm).ToBasicString ());
else if (schm == AuthenticationSchemes.Digest)
else if (scheme == AuthenticationSchemes.Digest)
context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateDigestChallenge (realm).ToDigestString ());
else
@ -186,8 +182,11 @@ namespace WebSocketSharp.Net
internal void Complete (HttpListenerContext context, bool syncCompleted)
{
if (!authenticate (context)) {
context.Listener.BeginGetContext (this);
var listener = context.Listener;
var schm = listener.SelectAuthenticationScheme (context);
if (schm != AuthenticationSchemes.Anonymous &&
!authenticate (context, schm, listener.Realm, listener.UserCredentialsFinder)) {
listener.BeginGetContext (this);
return;
}

View File

@ -59,7 +59,6 @@ namespace WebSocketSharp.Net
private static readonly byte[] _100continue;
private string[] _acceptTypes;
private bool _authenticated;
private bool _chunked;
private Encoding _contentEncoding;
private long _contentLength;
@ -242,11 +241,7 @@ namespace WebSocketSharp.Net
/// </value>
public bool IsAuthenticated {
get {
return _authenticated;
}
internal set {
_authenticated = value;
return _context.User != null;
}
}

View File

@ -114,7 +114,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public override bool IsAuthenticated {
get {
return _context.Request.IsAuthenticated;
return _context.User != null;
}
}
@ -257,7 +257,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 {

View File

@ -182,7 +182,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 abstract IPrincipal User { get; }