private async ValueTask <AccessToken> GetTokenImplAsync(TokenRequestContext requestContext, bool async, CancellationToken cancellationToken)
        {
            using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("VisualStudioCodeCredential.GetToken", requestContext);

            try
            {
                GetUserSettings(out var tenant, out var environmentName);

                var cloudInstance     = GetAzureCloudInstance(environmentName);
                var storedCredentials = _vscAdapter.GetCredentials(CredentialsSection, environmentName);

                if (!IsBase64UrlString(storedCredentials))
                {
                    throw new CredentialUnavailableException("Need to re-authenticate user in VSCode Azure Account.");
                }

                var result = await _client.AcquireTokenByRefreshToken(requestContext.Scopes, storedCredentials, cloudInstance, tenant, async, cancellationToken).ConfigureAwait(false);

                return(scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn)));
            }
            catch (OperationCanceledException e)
            {
                scope.Failed(e);
                throw;
            }
            catch (Exception e)
            {
                throw scope.FailAndWrap(e);
            }
        }
        private async ValueTask <AccessToken> GetTokenImplAsync(TokenRequestContext requestContext, bool async, CancellationToken cancellationToken)
        {
            using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("VisualStudioCodeCredential.GetToken", requestContext);

            try
            {
                GetUserSettings(out var tenant, out var environmentName);

                if (string.Equals(tenant, Constants.AdfsTenantId, StringComparison.Ordinal))
                {
                    throw new CredentialUnavailableException("VisualStudioCodeCredential authentication unavailable. ADFS tenant / authorities are not supported.");
                }

                var cloudInstance     = GetAzureCloudInstance(environmentName);
                var storedCredentials = _vscAdapter.GetCredentials(CredentialsSection, environmentName);

                if (!IsRefreshTokenString(storedCredentials))
                {
                    throw new CredentialUnavailableException("Need to re-authenticate user in VSCode Azure Account.");
                }

                var result = await _client.AcquireTokenByRefreshToken(requestContext.Scopes, storedCredentials, cloudInstance, tenant, async, cancellationToken).ConfigureAwait(false);

                return(scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn)));
            }
            catch (MsalUiRequiredException e)
            {
                throw scope.FailWrapAndThrow(new CredentialUnavailableException($"{nameof(VisualStudioCodeCredential)} authentication unavailable. Token acquisition failed. Ensure that you have authenticated in VSCode Azure Account.", e));
            }
            catch (Exception e)
            {
                throw scope.FailWrapAndThrow(e);
            }
        }
Ejemplo n.º 3
0
        private string GetStoredCredentials(string environmentName)
        {
            try
            {
                var storedCredentials = _vscAdapter.GetCredentials(CredentialsSection, environmentName);
                if (!IsRefreshTokenString(storedCredentials))
                {
                    throw new CredentialUnavailableException("Need to re-authenticate user in VSCode Azure Account.");
                }

                return(storedCredentials);
            }
            catch (Exception ex) when(!(ex is OperationCanceledException || ex is CredentialUnavailableException))
            {
                throw new CredentialUnavailableException("Stored credentials not found. Need to authenticate user in VSCode Azure Account. " + Troubleshooting, ex);
            }
        }
Ejemplo n.º 4
0
        private string GetStoredCredentials(string environmentName)
        {
            try
            {
                var storedCredentials = _vscAdapter.GetCredentials(CredentialsSection, environmentName);
                if (!IsRefreshTokenString(storedCredentials))
                {
                    throw new CredentialUnavailableException("Need to re-authenticate user in VSCode Azure Account.");
                }

                return(storedCredentials);
            }
            catch (InvalidOperationException ex)
            {
                throw new CredentialUnavailableException("Stored credentials not found. Need to authenticate user in VSCode Azure Account.", ex);
            }
        }