internal AuthenticationResponse(
     AuthenticationChallenge challenge,
     NetworkCredential credentials,
     uint nonceCount)
     : this(challenge.Scheme, challenge.Params, credentials, nonceCount)
 {
 }
 public AuthenticationResponse(WsCredential credential, AuthenticationChallenge challenge)
 {
     _userName = credential.UserName;
       _password = credential.Password;
       _scheme = challenge.Scheme;
       _realm = challenge.Realm;
       if (_scheme == "Digest")
     initForDigest(credential, challenge);
 }
Example #3
0
        public System.Net.Sockets.TcpClient ConnectThroughProxy(Uri uri)
        {
            _tcpClient         = new System.Net.Sockets.TcpClient(_proxyUri.DnsSafeHost, _proxyUri.Port);
            _tcpClient.NoDelay = true;

            _stream = _tcpClient.GetStream();

            var req = HttpRequest.CreateConnectRequest(uri);
            var res = sendHttpRequest(req, 90000);

            if (res.IsProxyAuthenticationRequired)
            {
                var chal = res.Headers["Proxy-Authenticate"];
                _logger.Warn(
                    String.Format("Received a proxy authentication requirement for '{0}'.", chal));

                if (chal.IsNullOrEmpty())
                {
                    throw new WebSocketException("No proxy authentication challenge is specified.");
                }

                var authChal = AuthenticationChallenge.Parse(chal);
                if (authChal == null)
                {
                    throw new WebSocketException("An invalid proxy authentication challenge is specified.");
                }

                if (_proxyCredentials != null)
                {
                    if (res.HasConnectionClose)
                    {
                        releaseClientResources();
                        _tcpClient         = new System.Net.Sockets.TcpClient(_proxyUri.DnsSafeHost, _proxyUri.Port);
                        _tcpClient.NoDelay = true;
                        _stream            = _tcpClient.GetStream();
                    }

                    var authRes = new AuthenticationResponse(authChal, _proxyCredentials, 0);
                    req.Headers["Proxy-Authorization"] = authRes.ToString();
                    res = sendHttpRequest(req, 15000);
                }

                if (res.IsProxyAuthenticationRequired)
                {
                    throw new WebSocketException("A proxy authentication is required.");
                }
            }

            if (res.StatusCode[0] != '2')
            {
                throw new WebSocketException(
                          "The proxy has failed a connection to the requested host and port.");
            }
            return(_tcpClient);
        }
 public AuthenticationResponse(WsCredential credential, AuthenticationChallenge challenge)
 {
     _userName = credential.UserName;
     _password = credential.Password;
     _scheme   = challenge.Scheme;
     _realm    = challenge.Realm;
     if (_scheme == "Digest")
     {
         initForDigest(credential, challenge);
     }
 }
        private void initForDigest(WsCredential credential, AuthenticationChallenge challenge)
        {
            _nonce     = challenge.Nonce;
            _method    = "GET";
            _uri       = credential.Domain;
            _algorithm = challenge.Algorithm;
            _opaque    = challenge.Opaque;
            foreach (var qop in challenge.Qop.Split(','))
            {
                if (qop.Trim().ToLower() == "auth")
                {
                    _qop = "auth";
                    _nc  = "00000001";
                    break;
                }
            }

            _cnonce   = createNonceValue();
            _response = createRequestDigest();
        }
        public static AuthenticationChallenge Parse(string challenge)
        {
            var authChallenge = new AuthenticationChallenge();

            if (challenge.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
            {
                authChallenge.Scheme = "Basic";
                authChallenge.Realm  = challenge.Substring(6).GetValueInternal("=").Trim('"');

                return(authChallenge);
            }

            foreach (var p in challenge.SplitHeaderValue(','))
            {
                var param = p.Trim();
                if (param.StartsWith("digest", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Scheme = "Digest";
                    authChallenge.Realm  = param.Substring(7).GetValueInternal("=").Trim('"');

                    continue;
                }

                var value = param.GetValueInternal("=").Trim('"');
                if (param.StartsWith("domain", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Domain = value;
                    continue;
                }

                if (param.StartsWith("nonce", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Nonce = value;
                    continue;
                }

                if (param.StartsWith("opaque", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Opaque = value;
                    continue;
                }

                if (param.StartsWith("stale", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Stale = value;
                    continue;
                }

                if (param.StartsWith("algorithm", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Algorithm = value;
                    continue;
                }

                if (param.StartsWith("qop", StringComparison.OrdinalIgnoreCase))
                {
                    authChallenge.Qop = value;
                }
            }

            return(authChallenge);
        }
        private void initForDigest(WsCredential credential, AuthenticationChallenge challenge)
        {
            _nonce = challenge.Nonce;
              _method = "GET";
              _uri = credential.Domain;
              _algorithm = challenge.Algorithm;
              _opaque = challenge.Opaque;
              foreach (var qop in challenge.Qop.Split(','))
              {
            if (qop.Trim().ToLower() == "auth")
            {
              _qop = "auth";
              _nc = "00000001";
              break;
            }
              }

              _cnonce = createNonceValue();
              _response = createRequestDigest();
        }