private async Task RegisterTokenCacheAsync(IPublicClientApplication app)
        {
            Context.Trace.WriteLine(
                "Configuring Microsoft Authentication token cache to instance shared with Microsoft developer tools...");

            if (!PlatformUtils.IsWindows() && !PlatformUtils.IsPosix())
            {
                string osType = PlatformUtils.GetPlatformInformation().OperatingSystemType;
                Context.Trace.WriteLine($"Token cache integration is not supported on {osType}.");
                return;
            }

            // We use the MSAL extension library to provide us consistent cache file access semantics (synchronisation, etc)
            // as other Microsoft developer tools such as the Azure PowerShell CLI.
            MsalCacheHelper helper = null;

            try
            {
                var storageProps = CreateTokenCacheProps(useLinuxFallback: false);
                helper = await MsalCacheHelper.CreateAsync(storageProps);

                // Test that cache access is working correctly
                helper.VerifyPersistence();
            }
            catch (MsalCachePersistenceException ex)
            {
                Context.Streams.Error.WriteLine("warning: cannot persist Microsoft authentication token cache securely!");
                Context.Trace.WriteLine("Cannot persist Microsoft Authentication data securely!");
                Context.Trace.WriteException(ex);

                if (PlatformUtils.IsMacOS())
                {
                    // On macOS sometimes the Keychain returns the "errSecAuthFailed" error - we don't know why
                    // but it appears to be something to do with not being able to access the keychain.
                    // Locking and unlocking (or restarting) often fixes this.
                    Context.Streams.Error.WriteLine(
                        "warning: there is a problem accessing the login Keychain - either manually lock and unlock the " +
                        "login Keychain, or restart the computer to remedy this");
                }
                else if (PlatformUtils.IsLinux())
                {
                    // On Linux the SecretService/keyring might not be available so we must fall-back to a plaintext file.
                    Context.Streams.Error.WriteLine("warning: using plain-text fallback token cache");
                    Context.Trace.WriteLine("Using fall-back plaintext token cache on Linux.");
                    var storageProps = CreateTokenCacheProps(useLinuxFallback: true);
                    helper = await MsalCacheHelper.CreateAsync(storageProps);
                }
            }

            if (helper is null)
            {
                Context.Streams.Error.WriteLine("error: failed to set up Microsoft Authentication token cache!");
                Context.Trace.WriteLine("Failed to integrate with shared token cache!");
            }
            else
            {
                helper.RegisterCache(app.UserTokenCache);
                Context.Trace.WriteLine("Microsoft developer tools token cache configured.");
            }
        }
        public TesterWindow()
        {
            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif

            if (PlatformUtils.IsWindows())
            {
                _environment = new WindowsEnvironment(new WindowsFileSystem());
            }
            else
            {
                IFileSystem fs;
                if (PlatformUtils.IsMacOS())
                {
                    fs = new MacOSFileSystem();
                }
                else
                {
                    fs = new LinuxFileSystem();
                }

                _environment = new PosixEnvironment(fs);
            }
        }
 public WindowViewModel()
 {
     // Default to hiding the system chrome on macOS only for now
     ExtendClientArea = PlatformUtils.IsMacOS();
 }
        protected override async Task <bool> RunInternalAsync(StringBuilder log, IList <string> additionalFiles)
        {
            if (MicrosoftAuthentication.CanUseBroker(_context))
            {
                log.Append("Checking broker initialization state...");
                if (MicrosoftAuthentication.IsBrokerInitialized)
                {
                    log.AppendLine(" Initialized");
                }
                else
                {
                    log.AppendLine("  Not initialized");
                    log.Append("Initializing broker...");
                    MicrosoftAuthentication.InitializeBroker();
                    log.AppendLine("OK");
                }
            }
            else
            {
                log.AppendLine("Broker not supported.");
            }

            var msAuth = new MicrosoftAuthentication(_context);

            log.AppendLine($"Flow type is: {msAuth.GetFlowType()}");

            log.Append("Gathering MSAL token cache data...");
            StorageCreationProperties cacheProps = msAuth.CreateTokenCacheProps(true);

            log.AppendLine(" OK");
            log.AppendLine($"CacheDirectory: {cacheProps.CacheDirectory}");
            log.AppendLine($"CacheFileName: {cacheProps.CacheFileName}");
            log.AppendLine($"CacheFilePath: {cacheProps.CacheFilePath}");

            if (PlatformUtils.IsMacOS())
            {
                log.AppendLine($"MacKeyChainAccountName: {cacheProps.MacKeyChainAccountName}");
                log.AppendLine($"MacKeyChainServiceName: {cacheProps.MacKeyChainServiceName}");
            }
            else if (PlatformUtils.IsLinux())
            {
                log.AppendLine($"KeyringCollection: {cacheProps.KeyringCollection}");
                log.AppendLine($"KeyringSchemaName: {cacheProps.KeyringSchemaName}");
                log.AppendLine($"KeyringSecretLabel: {cacheProps.KeyringSecretLabel}");
                log.AppendLine($"KeyringAttribute1: ({cacheProps.KeyringAttribute1.Key},{cacheProps.KeyringAttribute1.Value})");
                log.AppendLine($"KeyringAttribute2: ({cacheProps.KeyringAttribute2.Key},{cacheProps.KeyringAttribute2.Value})");
            }

            log.Append("Creating cache helper...");
            var cacheHelper = await MsalCacheHelper.CreateAsync(cacheProps);

            log.AppendLine(" OK");
            try
            {
                log.Append("Verifying MSAL token cache persistence...");
                cacheHelper.VerifyPersistence();
                log.AppendLine(" OK");
            }
            catch (Exception)
            {
                log.AppendLine(" Failed");
                throw;
            }

            return(true);
        }