public void Should_acquire_gssapi_security_credential_with_username_only()
        {
            RequireEnvironment.Check().EnvironmentVariable("GSSAPI_TESTS_ENABLED");

            var credential = GssapiSecurityCredential.Acquire(_username);

            credential.Should().NotBeNull();
        }
        public void Should_acquire_gssapi_security_credential_with_username_and_password()
        {
            RequireEnvironment.Check().EnvironmentVariable("GSSAPI_TESTS_ENABLED");

            var securePassword = SecureStringHelper.ToSecureString(_password);
            var credential     = GssapiSecurityCredential.Acquire(_username, securePassword);

            credential.Should().NotBeNull();
        }
        public void Should_fail_to_acquire_gssapi_security_credential_with_username_and_bad_password()
        {
            RequireEnvironment.Check().EnvironmentVariable("GSSAPI_TESTS_ENABLED");

            var securePassword = SecureStringHelper.ToSecureString("BADPASSWORD");

            var exception = Record.Exception(() => GssapiSecurityCredential.Acquire(_username, securePassword));

            exception.Should().BeOfType <LibgssapiException>();
        }
        public static ISecurityContext InitializeSecurityContext(string serviceName, string hostname, string realm, string authorizationId, SecureString password)
        {
            var operatingSystemPlatform = OperatingSystemHelper.CurrentOperatingSystem;

            switch (operatingSystemPlatform)
            {
            case OperatingSystemPlatform.Windows:
            {
                SspiSecurityCredential credential = null;
                try
                {
                    var servicePrincipalName = $"{serviceName}/{hostname}";
                    if (!string.IsNullOrEmpty(realm))
                    {
                        servicePrincipalName += $"@{realm}";
                    }
                    credential = SspiSecurityCredential.Acquire(SspiPackage.Kerberos, authorizationId, password);
                    return(new SspiSecurityContext(servicePrincipalName, credential));
                }
                catch (Win32Exception)
                {
                    credential?.Dispose();
                    throw;
                }
            }

            case OperatingSystemPlatform.Linux:
            {
                GssapiServicePrincipalName servicePrincipalName = null;
                GssapiSecurityCredential   credential           = null;
                try
                {
                    servicePrincipalName = GssapiServicePrincipalName.Create(serviceName, hostname, realm);
                    credential           = GssapiSecurityCredential.Acquire(authorizationId, password);
                    return(new GssapiSecurityContext(servicePrincipalName, credential));
                }
                catch (LibgssapiException)
                {
                    servicePrincipalName?.Dispose();
                    credential?.Dispose();
                    throw;
                }
            }

            default:
                throw new NotSupportedException($"GSSAPI is not supported on {operatingSystemPlatform}.");
            }
        }