Replaced the authenticate method with authenticate (HttpListenerContext, HttpListener) method

This commit is contained in:
sta 2015-03-16 14:37:24 +09:00
parent f627efd8df
commit 913c47b70c

View File

@ -108,31 +108,32 @@ namespace WebSocketSharp.Net
#region Private Methods #region Private Methods
private static bool authenticate ( private static bool authenticate (HttpListenerContext context, HttpListener listener)
HttpListenerContext context,
AuthenticationSchemes scheme,
string realm,
Func<IIdentity, NetworkCredential> credentialsFinder)
{ {
if (!(scheme == AuthenticationSchemes.Basic || scheme == AuthenticationSchemes.Digest)) { var schm = listener.SelectAuthenticationScheme (context);
if (schm == AuthenticationSchemes.Anonymous)
return true;
if (schm != AuthenticationSchemes.Basic && schm != AuthenticationSchemes.Digest) {
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"], scheme, realm, req.HttpMethod, credentialsFinder); req.Headers["Authorization"], schm, realm, req.HttpMethod, listener.UserCredentialsFinder);
if (user != null && user.Identity.IsAuthenticated) { if (user != null && user.Identity.IsAuthenticated) {
context.User = user; context.User = user;
return true; return true;
} }
if (scheme == AuthenticationSchemes.Basic) if (schm == AuthenticationSchemes.Basic)
context.Response.CloseWithAuthChallenge ( context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateBasicChallenge (realm).ToBasicString ()); AuthenticationChallenge.CreateBasicChallenge (realm).ToBasicString ());
if (scheme == AuthenticationSchemes.Digest) if (schm == AuthenticationSchemes.Digest)
context.Response.CloseWithAuthChallenge ( context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateDigestChallenge (realm).ToDigestString ()); AuthenticationChallenge.CreateDigestChallenge (realm).ToDigestString ());
@ -182,9 +183,7 @@ namespace WebSocketSharp.Net
internal void Complete (HttpListenerContext context, bool syncCompleted) internal void Complete (HttpListenerContext context, bool syncCompleted)
{ {
var lsnr = context.Listener; var lsnr = context.Listener;
var schm = lsnr.SelectAuthenticationScheme (context); if (!authenticate (context, lsnr)) {
if (schm != AuthenticationSchemes.Anonymous &&
!authenticate (context, schm, lsnr.Realm, lsnr.UserCredentialsFinder)) {
lsnr.BeginGetContext (this); lsnr.BeginGetContext (this);
return; return;
} }