From dffa4e800686c7320896002c9de1b85748f3f58c Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 28 Apr 2016 16:00:12 +0900 Subject: [PATCH] [Modify] Add it --- .../WebSockets/TcpListenerWebSocketContext.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs b/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs index 9def9f3d..7c3938e3 100644 --- a/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs +++ b/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs @@ -355,6 +355,53 @@ namespace WebSocketSharp.Net.WebSockets #region Internal Methods + internal bool Authenticate ( + AuthenticationSchemes scheme, + string realm, + Func credentialsFinder + ) + { + if (scheme == AuthenticationSchemes.Anonymous) + return true; + + if (scheme != AuthenticationSchemes.Basic && scheme != AuthenticationSchemes.Digest) { + Close (HttpStatusCode.Forbidden); + return false; + } + + var chal = new AuthenticationChallenge (scheme, realm).ToString (); + + var retry = -1; + Func auth = null; + auth = + () => { + retry++; + if (retry > 99) { + Close (HttpStatusCode.Forbidden); + return false; + } + + var user = + HttpUtility.CreateUser ( + _request.Headers["Authorization"], + scheme, + realm, + _request.HttpMethod, + credentialsFinder + ); + + if (user == null || !user.Identity.IsAuthenticated) { + SendAuthenticationChallenge (chal); + return auth (); + } + + _user = user; + return true; + }; + + return auth (); + } + internal void Close () { _stream.Close ();