Ejemplo n.º 1
0
        /// <summary>
        /// Acquires credentials via the registered callbacks.
        /// <para/>
        /// Returns `<see cref="Credential"/>` from the authentication object, authority or storage if successful; otherwise `<see langword="null"/>`.
        /// </summary>
        /// <param name="targetUri">
        /// The uniform resource indicator used to uniquely identify the credentials.
        /// </param>
        public async Task <Credential> AcquireCredentials(TargetUri targetUri)
        {
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }

            if (_ntlmSupport != NtlmSupport.Never)
            {
                // Get the WWW-Authenticate headers (if any).
                if (_httpAuthenticateOptions == null)
                {
                    _httpAuthenticateOptions = await WwwAuthenticateHelper.GetHeaderValues(Context, targetUri);
                }

                // If the headers contain NTLM as an option, then fall back to NTLM.
                if (_httpAuthenticateOptions.Any(x => WwwAuthenticateHelper.IsNtlm(x)))
                {
                    Trace.WriteLine($"'{targetUri}' supports NTLM, sending NTLM credentials instead");

                    return(NtlmCredentials);
                }
            }

            Credential credentials = null;

            if (_ntlmSupport != NtlmSupport.Always && _acquireCredentials != null)
            {
                Trace.WriteLine($"prompting user for credentials for '{targetUri}'.");

                credentials = _acquireCredentials(targetUri);

                if (_acquireResult != null)
                {
                    AcquireCredentialResult result = (credentials == null)
                        ? AcquireCredentialResult.Failed
                        : AcquireCredentialResult.Suceeded;

                    _acquireResult(targetUri, result);
                }
            }

            // If credentials have been acquired, write them to the secret store.
            if (credentials != null)
            {
                await _credentialStore.WriteCredentials(targetUri, credentials);
            }

            return(credentials);
        }
        public void ReadSolution_SolutionFileExists_CredentialsExist_ReturnsConfig()
        {
            // Arrange
            var validConfig = @"{
  ""ServerUri"": ""https://*****:*****@"c:\mysolutionfile.foo");
            SetFileContents(@"c:\.sonarlint\mysolutionfile.sqconfig", validConfig);

            Credential cred = new Credential("user1");

            configurableStore.WriteCredentials(new TargetUri("https://xxx:123"), cred);

            // Act
            var actual = testSubject.ReadSolutionBinding();

            // Assert
            actual.Should().NotBeNull();

            actual.Credentials.Should().NotBeNull();
            var actualCreds = actual.Credentials as BasicAuthCredentials;

            actualCreds.Should().NotBeNull();
            actualCreds.UserName.Should().Be("user1");

            actual.ServerUri.Should().Be("https://xxx:123");
            actual.Organization.Should().NotBeNull();
            actual.Organization.Key.Should().Be("OrgKey");
            actual.Organization.Name.Should().Be("OrgName");
            actual.ProjectKey.Should().Be("key111");
        }
Ejemplo n.º 3
0
        private void ICredentialStoreTest(ICredentialStore credentialStore, string url, string username, string password)
        {
            try
            {
                TargetUri  uri        = new TargetUri(url);
                Credential writeCreds = new Credential(username, password);
                Credential readCreds  = null;

                credentialStore.WriteCredentials(uri, writeCreds);

                if ((readCreds = credentialStore.ReadCredentials(uri)) != null)
                {
                    Assert.AreEqual(writeCreds.Password, readCreds.Password, "Passwords did not match between written and read credentials");
                    Assert.AreEqual(writeCreds.Username, readCreds.Username, "Usernames did not match between written and read credentials");
                }
                else
                {
                    Assert.Fail("Failed to read credentials");
                }

                credentialStore.DeleteCredentials(uri);

                Assert.IsNull(readCreds = credentialStore.ReadCredentials(uri), "Deleted credentials were read back");
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Writes credentials for a target URI to the credential store
        /// </summary>
        /// <param name="targetUri">The URI of the target for which credentials are being stored</param>
        /// <param name="credentials">The credentials to be stored</param>
        public async Task <bool> WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            ValidateTargetUri(targetUri);
            BaseSecureStore.ValidateCredential(credentials);

            string targetName = GetTargetName(targetUri);

            return(WriteCredential(targetName, credentials) &&
                   await _credentialCache.WriteCredentials(targetUri, credentials));
        }
        /// <summary>
        /// Writes credentials for a target URI to the credential store
        /// </summary>
        /// <param name="targetUri">The URI of the target for which credentials are being stored</param>
        /// <param name="credentials">The credentials to be stored</param>
        public void WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            ValidateTargetUri(targetUri);
            BaseSecureStore.ValidateCredential(credentials);

            string targetName = GetTargetName(targetUri);

            WriteCredential(targetName, credentials);

            _credentialCache.WriteCredentials(targetUri, credentials);
        }
        public void WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            var credsToStore = credentials;

            if (credentials != null && string.IsNullOrEmpty(credentials.Password))
            {
                credsToStore = new Credential(UserNameForTokenCredential, credentials.Username);
            }

            LogWin32Exception("write", () => store.WriteCredentials(targetUri, credsToStore));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Writes credentials for a target URI to the credential store
        /// </summary>
        /// <param name="targetUri">The URI of the target for which credentials are being stored</param>
        /// <param name="credentials">The credentials to be stored</param>
        public void WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            ValidateTargetUri(targetUri);
            Credential.Validate(credentials);

            Trace.WriteLine("CredentialStore::WriteCredentials");

            string targetName = this.GetTargetName(targetUri);

            this.WriteCredential(targetName, credentials);

            _credentialCache.WriteCredentials(targetUri, credentials);
        }
Ejemplo n.º 8
0
        public async Task <bool> WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }
            if (credentials is null)
            {
                throw new ArgumentNullException(nameof(credentials));
            }

            string targetName = GetTargetName(targetUri);

            return(WriteCredential(targetName, credentials) &&
                   await _credentialCache.WriteCredentials(targetUri, credentials));
        }
        /// <summary>
        /// Acquires credentials via the registered callbacks.
        /// </summary>
        /// <param name="targetUri">
        /// The uniform resource indicator used to uniquely identify the credentials.
        /// </param>
        /// <returns>
        /// If successful a <see cref="Credential"/> object from the authentication object, authority
        /// or storage; otherwise <see langword="null"/>.
        /// </returns>
        public async Task <Credential> AcquireCredentials(TargetUri targetUri)
        {
            BaseSecureStore.ValidateTargetUri(targetUri);

            if (_ntlmSupport != NtlmSupport.Never)
            {
                // get the WWW-Authenticate headers (if any)
                if (_httpAuthenticateOptions == null)
                {
                    _httpAuthenticateOptions = await WwwAuthenticateHelper.GetHeaderValues(targetUri);
                }

                // if the headers contain NTML as an option, then fall back to NTLM
                if (_httpAuthenticateOptions.Any(x => WwwAuthenticateHelper.IsNtlm(x)))
                {
                    Git.Trace.WriteLine($"'{targetUri}' supports NTLM, sending NTLM credentials instead");

                    return(NtlmCredentials);
                }
            }

            Credential credentials = null;

            if (_ntlmSupport != NtlmSupport.Always && _acquireCredentials != null)
            {
                Git.Trace.WriteLine($"prompting user for credentials for '{targetUri}'.");

                credentials = _acquireCredentials(targetUri);

                if (_acquireResult != null)
                {
                    AcquireCredentialResult result = (credentials == null)
                        ? AcquireCredentialResult.Failed
                        : AcquireCredentialResult.Suceeded;

                    _acquireResult(targetUri, result);
                }
            }

            // If credentials have been acquired, write them to the secret store.
            if (credentials != null)
            {
                _credentialStore.WriteCredentials(targetUri, credentials);
            }

            return(credentials);
        }
        public async Task <bool> WriteCredentials(TargetUri targetUri, Credential credentials)
        {
#if DEBUG
            Trace.WriteLine($"targetUri: '{targetUri}', userName: '******' PAT: '{credentials?.Password}',  : Key: '{GetKeyVaultKey(targetUri)}'");
#endif
            if (targetUri is null || string.IsNullOrEmpty(targetUri.Host))
            {
                throw new ArgumentNullException(nameof(targetUri));
            }

            if (credentials is null)
            {
                throw new ArgumentNullException(nameof(credentials));
            }
            return(await WriteKeyVaultCredentials(targetUri, credentials) &&
                   await _credentialCache.WriteCredentials(targetUri, credentials));
        }
        private void ICredentialStoreTest(ICredentialStore credentialStore, string url, string username, string password)
        {
            try
            {
                Uri uri = new Uri(url, UriKind.Absolute);
                Credential writeCreds = new Credential(username, password);
                Credential readCreds = null;

                credentialStore.WriteCredentials(uri, writeCreds);

                if (credentialStore.ReadCredentials(uri, out readCreds))
                {
                    Assert.AreEqual(writeCreds.Password, readCreds.Password, "Passwords did not match between written and read credentials");
                    Assert.AreEqual(writeCreds.Username, readCreds.Username, "Usernames did not match between written and read credentials");
                }
                else
                {
                    Assert.Fail("Failed to read credentials");
                }

                credentialStore.DeleteCredentials(uri);

                Assert.IsFalse(credentialStore.ReadCredentials(uri, out readCreds), "Deleted credentials were read back");
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }
        }