예제 #1
0
        public void CreateResponseAuth()
        {
            CFHTTPMessage response             = null;
            var           done                 = false;
            var           taskCompletionSource = new TaskCompletionSource <CFHTTPMessage> ();

            // the following code has to be in a diff thread, else, we are blocking the current loop, not cool
            // perform a request so that we fail in the auth, then create the auth object and check the count
            TestRuntime.RunAsync(DateTime.Now.AddSeconds(30), async() => {
                using (var request = CFHTTPMessage.CreateRequest(
                           new Uri(NetworkResources.Httpbin.GetStatusCodeUrl(HttpStatusCode.Unauthorized)), "GET", null)) {
                    request.SetBody(Array.Empty <byte> ());                     // empty body, we are not interested
                    using (var stream = CFStream.CreateForHTTPRequest(request)) {
                        Assert.IsNotNull(stream, "Null stream");
                        // we are only interested in the completed event
                        stream.ClosedEvent += (sender, e) => {
                            taskCompletionSource.SetResult(stream.GetResponseHeader());
                            done = true;
                        };
                        // enable events and run in the current loop
                        stream.EnableEvents(CFRunLoop.Main, CFRunLoop.ModeDefault);
                        stream.Open();
                        response = await taskCompletionSource.Task;
                    }
                }
            }, () => done);
            using (var auth = CFHTTPAuthentication.CreateFromResponse(response)) {
                Assert.NotNull(auth, "Null Auth");
                Assert.IsTrue(auth.IsValid, "Auth is valid");
                Assert.That(TestRuntime.CFGetRetainCount(auth.Handle), Is.EqualTo((nint)1), "RetainCount");
            }
        }
예제 #2
0
        public void ApplyCredentials(CFHTTPAuthentication auth, NetworkCredential credential)
        {
            if (auth == null)
            {
                throw new ArgumentNullException("auth");
            }
            if (credential == null)
            {
                throw new ArgumentNullException("credential");
            }

            if (auth.RequiresAccountDomain)
            {
                ApplyCredentialDictionary(auth, credential);
                return;
            }

            var username = new CFString(credential.UserName);
            var password = new CFString(credential.Password);

            try {
                CFStreamError error;

                var ok = CFHTTPMessageApplyCredentials(
                    Handle, auth.Handle, username.Handle, password.Handle,
                    out error);
                if (!ok)
                {
                    throw GetException((CFStreamErrorHTTPAuthentication)error.code);
                }
            } finally {
                username.Dispose();
                password.Dispose();
            }
        }
        void FindAuthenticationObject(CFHTTPMessage response)
        {
            if (auth != null)
            {
                if (auth.IsValid)
                {
                    return;
                }
                auth.Dispose();
                auth = null;
            }

            if (auth == null)
            {
                auth = CFHTTPAuthentication.CreateFromResponse(response);
                if (auth == null)
                {
                    throw new HttpRequestException("Failed to create CFHTTPAuthentication");
                }
            }

            if (!auth.IsValid)
            {
                throw new HttpRequestException("Failed to validate CFHTTPAuthentication");
            }
        }
예제 #4
0
        public void ApplyCredentials(CFHTTPAuthentication auth, NetworkCredential credential)
        {
            if (auth is null)
            {
                throw new ArgumentNullException(nameof(auth));
            }
            if (credential is null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            if (auth.RequiresAccountDomain)
            {
                ApplyCredentialDictionary(auth, credential);
                return;
            }

            var username = CFString.CreateNative(credential.UserName);

            var password = CFString.CreateNative(credential.Password);

            try {
                var ok = CFHTTPMessageApplyCredentials(Handle, auth.Handle, username, password, out var error);
                if (!ok)
                {
                    throw GetException((CFStreamErrorHTTPAuthentication)error.code);
                }
            } finally {
                CFString.ReleaseNative(username);
                CFString.ReleaseNative(password);
            }
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (auth != null)
                {
                    auth.Dispose();
                    auth = null;
                }
            }

            base.Dispose(disposing);
        }
예제 #6
0
        public void ApplyCredentialDictionary(CFHTTPAuthentication auth, NetworkCredential credential)
        {
            if (auth is null)
            {
                throw new ArgumentNullException(nameof(auth));
            }
            if (credential is null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            var length = credential.Domain is null ? 2 : 3;

            var keys   = new NSString [length];
            var values = new CFString [length];

            keys [0]   = _AuthenticationUsername;
            keys [1]   = _AuthenticationPassword;
            values [0] = credential.UserName !;
            values [1] = credential.Password !;
            if (credential.Domain is not null)
            {
                keys [2]   = _AuthenticationAccountDomain;
                values [2] = credential.Domain;
            }

            var dict = CFDictionary.FromObjectsAndKeys(values, keys);

            try {
                CFStreamError error;
                var           ok = CFHTTPMessageApplyCredentialDictionary(
                    Handle, auth.Handle, dict.Handle, out error);
                if (ok)
                {
                    return;
                }
                throw GetException((CFStreamErrorHTTPAuthentication)error.code);
            } finally {
                dict.Dispose();
                values [0]?.Dispose();
                values [1]?.Dispose();
                values [2]?.Dispose();
            }
        }
예제 #7
0
        public void ApplyCredentialDictionary(CFHTTPAuthentication auth, NetworkCredential credential)
        {
            if (auth == null)
            {
                throw new ArgumentNullException("auth");
            }
            if (credential == null)
            {
                throw new ArgumentNullException("credential");
            }

            var keys = new NSString [3];

            var values = new CFString [3];

            keys [0]   = _AuthenticationUsername;
            keys [1]   = _AuthenticationPassword;
            keys [2]   = _AuthenticationAccountDomain;
            values [0] = (CFString)credential.UserName;
            values [1] = (CFString)credential.Password;
            values [2] = credential.Domain != null ? (CFString)credential.Domain : null;

            var dict = CFDictionary.FromObjectsAndKeys(values, keys);

            try {
                CFStreamError error;
                var           ok = CFHTTPMessageApplyCredentialDictionary(
                    Handle, auth.Handle, dict.Handle, out error);
                if (ok)
                {
                    return;
                }
                throw GetException((CFStreamErrorHTTPAuthentication)error.code);
            } finally {
                dict.Dispose();
                values [0].Dispose();
                values [1].Dispose();
                if (values [2] != null)
                {
                    values [2].Dispose();
                }
            }
        }
예제 #8
0
		public void ApplyCredentialDictionary (CFHTTPAuthentication auth, NetworkCredential credential)
		{
			if (auth == null)
				throw new ArgumentNullException ("auth");
			if (credential == null)
				throw new ArgumentNullException ("credential");

			var keys = new NSString [3];
			var values = new CFString [3];
			keys [0] = _AuthenticationUsername;
			keys [1] = _AuthenticationPassword;
			keys [2] = _AuthenticationAccountDomain;
			values [0] = (CFString)credential.UserName;
			values [1] = (CFString)credential.Password;
			values [2] = credential.Domain != null ? (CFString)credential.Domain : null;

			var dict = CFDictionary.FromObjectsAndKeys (values, keys);

			try {
				CFStreamError error;
				var ok = CFHTTPMessageApplyCredentialDictionary (
					Handle, auth.Handle, dict.Handle, out error);
				if (ok)
					return;
				throw GetException ((CFStreamErrorHTTPAuthentication) error.code);
			} finally {
				dict.Dispose ();
				values [0].Dispose ();
				values [1].Dispose ();
				if (values [2] != null)
					values [2].Dispose ();
			}
		}
예제 #9
0
		public void ApplyCredentials (CFHTTPAuthentication auth, NetworkCredential credential)
		{
			if (auth == null)
				throw new ArgumentNullException ("auth");
			if (credential == null)
				throw new ArgumentNullException ("credential");

			if (auth.RequiresAccountDomain) {
				ApplyCredentialDictionary (auth, credential);
				return;
			}

			var username = new CFString (credential.UserName);
			var password = new CFString (credential.Password);

			try {
				CFStreamError error;

				var ok = CFHTTPMessageApplyCredentials (
					Handle, auth.Handle, username.Handle, password.Handle,
					out error);
				if (!ok)
					throw GetException ((CFStreamErrorHTTPAuthentication) error.code);
			} finally {
				username.Dispose ();
				password.Dispose ();
			}
		}