Example #1
0
        /// <summary>
        /// <para></para>
        /// <para>Tokens acquired are stored in the secure secret store provided during
        /// initialization.</para>
        /// </summary>
        /// <param name="targetUri">The unique identifier for the resource for which access is to
        /// be acquired.</param>
        /// <param name="username">The username of the account for which access is to be acquired.</param>
        /// <param name="password">The password of the account for which access is to be acquired.</param>
        /// <param name="authenticationCode">The two-factor authentication code for use in access acquisition.</param>
        /// <returns>Acquired <see cref="Credential"/> if successful; otherwise <see langword="null"/>.</returns>
        public async Task <Credential> NoninteractiveLogonWithCredentials(TargetUri targetUri, string username, string password, string authenticationCode)
        {
            BaseSecureStore.ValidateTargetUri(targetUri);
            if (String.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentNullException("username", "The `username` parameter is null or invalid.");
            }
            if (String.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("username", "The `password` parameter is null or invalid.");
            }

            Credential credentials = null;

            AuthenticationResult result;

            if (result = await Authority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope))
            {
                Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded.");

                credentials = (Credential)result.Token;
                PersonalAccessTokenStore.WriteCredentials(targetUri, credentials);

                return(credentials);
            }

            Git.Trace.WriteLine($"non-interactive logon for '{targetUri}' failed.");
            return(credentials);
        }
Example #2
0
        /// <summary>
        /// Tokens acquired are stored in the secure secret store provided during initialization.
        /// <para/>
        /// Returns acquired `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`.
        /// </summary>
        /// <param name="targetUri">The unique identifier for the resource for which access is to be acquired.</param>
        /// <param name="username">The username of the account for which access is to be acquired.</param>
        /// <param name="password">The password of the account for which access is to be acquired.</param>
        /// <param name="authenticationCode">The two-factor authentication code for use in access acquisition.</param>
        public async Task <Credential> NoninteractiveLogonWithCredentials(TargetUri targetUri, string username, string password, string authenticationCode)
        {
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }
            if (username is null)
            {
                throw new ArgumentNullException(nameof(username));
            }
            if (password is null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            Credential           credentials         = null;
            var                  normalizedTargetUri = NormalizeUri(targetUri);
            AuthenticationResult result;

            if (result = await Authority.AcquireToken(normalizedTargetUri, username, password, authenticationCode, TokenScope))
            {
                Trace.WriteLine($"token acquisition for '{normalizedTargetUri}' succeeded.");

                credentials = (Credential)result.Token;
                await PersonalAccessTokenStore.WriteCredentials(normalizedTargetUri, credentials);

                return(credentials);
            }

            Trace.WriteLine($"non-interactive logon for '{normalizedTargetUri}' failed.");
            return(credentials);
        }
Example #3
0
        /// <summary>
        /// Tokens acquired are stored in the secure secret store provided during initialization.
        /// <para/>
        /// Returns acquired `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`
        /// </summary>
        /// <param name="targetUri">The unique identifier for the resource for which access is to be acquired.</param>
        public async Task <Credential> InteractiveLogon(TargetUri targetUri)
        {
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }

            Credential credentials = null;

            var normalizedTargetUri = NormalizeUri(targetUri);

            if (AcquireCredentialsCallback(normalizedTargetUri, out string username, out string password))
            {
                AuthenticationResult result;

                if (result = await Authority.AcquireToken(normalizedTargetUri, username, password, null, TokenScope))
                {
                    Trace.WriteLine($"token acquisition for '{normalizedTargetUri}' succeeded");

                    credentials = (Credential)result.Token;
                    await PersonalAccessTokenStore.WriteCredentials(normalizedTargetUri, credentials);

                    // if a result callback was registered, call it
                    AuthenticationResultCallback?.Invoke(normalizedTargetUri, result);

                    return(credentials);
                }
                else if (result == GitHubAuthenticationResultType.TwoFactorApp ||
                         result == GitHubAuthenticationResultType.TwoFactorSms)
                {
                    string authenticationCode;
                    if (AcquireAuthenticationCodeCallback(normalizedTargetUri, result, username, out authenticationCode))
                    {
                        if (result = await Authority.AcquireToken(normalizedTargetUri, username, password, authenticationCode, TokenScope))
                        {
                            Trace.WriteLine($"token acquisition for '{normalizedTargetUri}' succeeded.");

                            credentials = (Credential)result.Token;
                            await PersonalAccessTokenStore.WriteCredentials(normalizedTargetUri, credentials);

                            // if a result callback was registered, call it
                            AuthenticationResultCallback?.Invoke(normalizedTargetUri, result);

                            return(credentials);
                        }
                    }
                }

                AuthenticationResultCallback?.Invoke(normalizedTargetUri, result);
            }

            Trace.WriteLine($"interactive logon for '{normalizedTargetUri}' failed.");
            return(credentials);
        }
Example #4
0
        /// <summary>
        /// <para></para>
        /// <para>Tokens acquired are stored in the secure secret store provided during
        /// initialization.</para>
        /// </summary>
        /// <param name="targetUri">The unique identifier for the resource for which access is to
        /// be acquired.</param>
        /// /// <returns>Acquired <see cref="Credential"/> if successful; otherwise <see langword="null"/>.</returns>
        public async Task <Credential> InteractiveLogon(TargetUri targetUri)
        {
            Credential credentials = null;
            string     username;
            string     password;

            if (AcquireCredentialsCallback(targetUri, out username, out password))
            {
                AuthenticationResult result;

                if (result = await Authority.AcquireToken(targetUri, username, password, null, this.TokenScope))
                {
                    Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded");

                    credentials = (Credential)result.Token;
                    this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials);

                    // if a result callback was registered, call it
                    AuthenticationResultCallback?.Invoke(targetUri, result);

                    return(credentials);
                }
                else if (result == GitHubAuthenticationResultType.TwoFactorApp ||
                         result == GitHubAuthenticationResultType.TwoFactorSms)
                {
                    string authenticationCode;
                    if (AcquireAuthenticationCodeCallback(targetUri, result, username, out authenticationCode))
                    {
                        if (result = await Authority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope))
                        {
                            Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded.");

                            credentials = (Credential)result.Token;
                            this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials);

                            // if a result callback was registered, call it
                            AuthenticationResultCallback?.Invoke(targetUri, result);

                            return(credentials);
                        }
                    }
                }

                // if a result callback was registered, call it
                if (AuthenticationResultCallback != null)
                {
                    AuthenticationResultCallback(targetUri, result);
                }
            }

            Git.Trace.WriteLine($"interactive logon for '{targetUri}' failed.");
            return(credentials);
        }