Exemplo n.º 1
0
        private async Task <bool> RequiresTwoFactorAuthenticationAsync(ICredential credentials, Uri targetUri)
        {
            if (IsBitbucketServer(targetUri))
            {
                // BBS does not support 2FA out of the box so neither does GCM
                return(false);
            }

            RestApiResult <UserInfo> result = await _bitbucketApi.GetUserInformationAsync(credentials.UserName, credentials.Password, false);

            switch (result.StatusCode)
            {
            // 2FA may not be required
            case HttpStatusCode.OK:
                return(result.Response.IsTwoFactorAuthenticationEnabled);

            // 2FA is required
            case HttpStatusCode.Forbidden:
                return(true);

            // Incorrect credentials
            case HttpStatusCode.Unauthorized:
                throw new Exception("Invalid credentials");

            default:
                throw new Exception($"Unknown server response: {result.StatusCode}");
            }
        }
        private async Task <bool> RequiresTwoFactorAuthenticationAsync(ICredential credentials)
        {
            _context.Trace.WriteLineSecrets($"Check if 2FA is required for credentials ({credentials.Account}/{{0}})...", new object[] { credentials.Password });

            RestApiResult <UserInfo> result = await _bitbucketApi.GetUserInformationAsync(
                credentials.Account, credentials.Password, isBearerToken : false);

            switch (result.StatusCode)
            {
            // 2FA may not be required
            case HttpStatusCode.OK:
                return(result.Response.IsTwoFactorAuthenticationEnabled);

            // 2FA is required
            case HttpStatusCode.Forbidden:
                return(true);

            // Incorrect credentials
            case HttpStatusCode.Unauthorized:
                throw new Exception("Invalid credentials");

            default:
                throw new Exception($"Unknown server response: {result.StatusCode}");
            }
        }
        private async Task <bool> RequiresTwoFactorAuthenticationAsync(ICredential credentials, AuthenticationModes authModes)
        {
            if (!SupportsOAuth(authModes))
            {
                return(false);
            }

            RestApiResult <UserInfo> result = await _bitbucketApi.GetUserInformationAsync(
                credentials.Account, credentials.Password, false);

            switch (result.StatusCode)
            {
            // 2FA may not be required
            case HttpStatusCode.OK:
                return(result.Response.IsTwoFactorAuthenticationEnabled);

            // 2FA is required
            case HttpStatusCode.Forbidden:
                return(true);

            // Incorrect credentials
            case HttpStatusCode.Unauthorized:
                throw new Exception("Invalid credentials");

            default:
                throw new Exception($"Unknown server response: {result.StatusCode}");
            }
        }
Exemplo n.º 4
0
        private async Task <string> ResolveOAuthUserNameAsync(string accessToken)
        {
            RestApiResult <UserInfo> result = await _bitbucketApi.GetUserInformationAsync(null, accessToken, true);

            if (result.Succeeded)
            {
                return(result.Response.UserName);
            }

            throw new Exception($"Failed to resolve username. HTTP: {result.StatusCode}");
        }
        private async Task <string> ResolveBasicAuthUserNameAsync(string username, string password)
        {
            RestApiResult <UserInfo> result = await _bitbucketApi.GetUserInformationAsync(username, password, isBearerToken : false);

            if (result.Succeeded)
            {
                return(result.Response.UserName);
            }

            throw new Exception($"Failed to resolve username. HTTP: {result.StatusCode}");
        }