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 #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; if (scheme == AuthenticationSchemes.None) {
var schm = listener.SelectAuthenticationScheme (context);
if (schm == AuthenticationSchemes.Anonymous)
return true;
if (schm == AuthenticationSchemes.None) {
context.Response.Close (HttpStatusCode.Forbidden); context.Response.Close (HttpStatusCode.Forbidden);
return false; return false;
} }
var req = context.Request; var req = context.Request;
var realm = listener.Realm;
var user = HttpUtility.CreateUser ( 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) { if (user != null && user.Identity.IsAuthenticated) {
context.User = user; context.User = user;
req.IsAuthenticated = true;
return true; return true;
} }
if (schm == AuthenticationSchemes.Basic) if (scheme == AuthenticationSchemes.Basic)
context.Response.CloseWithAuthChallenge ( context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateBasicChallenge (realm).ToBasicString ()); AuthenticationChallenge.CreateBasicChallenge (realm).ToBasicString ());
else if (schm == AuthenticationSchemes.Digest) else if (scheme == AuthenticationSchemes.Digest)
context.Response.CloseWithAuthChallenge ( context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateDigestChallenge (realm).ToDigestString ()); AuthenticationChallenge.CreateDigestChallenge (realm).ToDigestString ());
else else
@ -186,8 +182,11 @@ namespace WebSocketSharp.Net
internal void Complete (HttpListenerContext context, bool syncCompleted) internal void Complete (HttpListenerContext context, bool syncCompleted)
{ {
if (!authenticate (context)) { var listener = context.Listener;
context.Listener.BeginGetContext (this); var schm = listener.SelectAuthenticationScheme (context);
if (schm != AuthenticationSchemes.Anonymous &&
!authenticate (context, schm, listener.Realm, listener.UserCredentialsFinder)) {
listener.BeginGetContext (this);
return; return;
} }

View File

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

View File

@ -114,7 +114,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override bool IsAuthenticated { public override bool IsAuthenticated {
get { 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). /// Gets the client information (identity, authentication, and security roles).
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="IPrincipal"/> that represents the client information. /// A <see cref="IPrincipal"/> instance that represents the client information.
/// </value> /// </value>
public override IPrincipal User { public override IPrincipal User {
get { get {

View File

@ -182,7 +182,7 @@ namespace WebSocketSharp.Net.WebSockets
/// Gets the client information (identity, authentication, and security roles). /// Gets the client information (identity, authentication, and security roles).
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="IPrincipal"/> that represents the client information. /// A <see cref="IPrincipal"/> instance that represents the client information.
/// </value> /// </value>
public abstract IPrincipal User { get; } public abstract IPrincipal User { get; }