Exemplo n.º 1
0
        public string GenerateString(short len, bool uc, bool lc, bool n, bool sc)
        {
            List <CliFlag> flags = new List <CliFlag>(6);

            flags.Add(BwRawFlag);
            flags.Add(CliFlag.FlagWithValue("length", len));
            if (uc)
            {
                flags.Add(CliFlag.Flag('u'));
            }
            if (lc)
            {
                flags.Add(CliFlag.Flag('l'));
            }
            if (n)
            {
                flags.Add(CliFlag.Flag('n'));
            }
            if (sc)
            {
                flags.Add(CliFlag.Flag('s'));
            }

            var generateResult = BitwardenCliInterface.ExecuteCommand(GenerateCommand, flags.ToArray());

            if (!generateResult.TimedOut && generateResult.ExitCode == SuccessExitCode)
            {
                return(generateResult.StandardOutput);
            }
            else
            {
                return(string.Empty);
            }
        }
Exemplo n.º 2
0
        public bool LockVault()
        {
            BitwardenCliCommandResult result = BitwardenCliInterface.ExecuteCommand(LockCommand, BwRawFlag);

            ConsoleDebugLogger.LogError(result.StandardError);
            return(!result.TimedOut && result.ExitCode == SuccessExitCode);
        }
Exemplo n.º 3
0
        public bool SyncVault(BitwardenCredentials credentials)
        {
            if (!CheckSessionCode(credentials))
            {
                return(false);
            }

            BitwardenCliCommandResult result = BitwardenCliInterface.ExecuteCommand(SyncCommand,
                                                                                    BwRawFlag,
                                                                                    CliFlag.FlagWithValue("session", credentials.SessionCode));

            return(result.ExitCode == SuccessExitCode && !result.TimedOut);
        }
Exemplo n.º 4
0
        public bool Logout()
        {
            BitwardenCliCommandResult result = BitwardenCliInterface.ExecuteCommand(LogoutCommand);

            if (result.TimedOut || result.ExitCode != SuccessExitCode)
            {
                ConsoleDebugLogger.LogError(result.StandardError);
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 5
0
        public VaultData GetVaultData(BitwardenCredentials credentials)
        {
            if (!CheckSessionCode(credentials))
            {
                return(null);
            }

            BitwardenCliCommandResult vaultItemsResult = BitwardenCliInterface.ExecuteCommand(ListCommand,
                                                                                              BwRawFlag,
                                                                                              CliFlag.StringValue("items"),
                                                                                              CliFlag.FlagWithValue("session", credentials.SessionCode));

            if (vaultItemsResult.TimedOut || vaultItemsResult.ExitCode != SuccessExitCode)
            {
                ConsoleDebugLogger.LogError("Failed to get vault items.");
                return(null);
            }

            BitwardenCliCommandResult vaultFoldersResults = BitwardenCliInterface.ExecuteCommand(ListCommand,
                                                                                                 BwRawFlag,
                                                                                                 CliFlag.StringValue("folders"),
                                                                                                 CliFlag.FlagWithValue("session", credentials.SessionCode));

            if (vaultFoldersResults.TimedOut || vaultFoldersResults.ExitCode != SuccessExitCode)
            {
                ConsoleDebugLogger.LogError("Failed to get vault folders.");
                return(null);
            }

            try
            {
                IEnumerable <VaultItem>   items   = JsonConvert.DeserializeObject <IEnumerable <VaultItem> >(vaultItemsResult.StandardOutput);
                IEnumerable <VaultFolder> folders = JsonConvert.DeserializeObject <IEnumerable <VaultFolder> >(vaultFoldersResults.StandardOutput);
                return(new VaultData()
                {
                    VaultFolders = folders, VaultItems = items
                });
            }
            catch (JsonException ex)
            {
                ConsoleDebugLogger.LogError("Failed JsonDeserialization in [GetVaultData]");
                ConsoleDebugLogger.LogError(ex.Message);
                return(null);
            }
        }
Exemplo n.º 6
0
        public bool UnlockVault(BitwardenCredentials credentials)
        {
            CliFlag[] flags =
            {
                CliFlag.StringValue(credentials.Password),
                BwRawFlag
            };

            BitwardenCliCommandResult result = BitwardenCliInterface.ExecuteCommand(UnlockCommand, flags);

            if (!result.TimedOut && result.ExitCode == SuccessExitCode)
            {
                credentials.SessionCode = result.StandardOutput;
                return(true);
            }
            else
            {
                ConsoleDebugLogger.LogError(result.StandardError);
                return(false);
            }
        }
Exemplo n.º 7
0
        public bool IsUserLogged(out string loggedUserEmail)
        {
            loggedUserEmail = string.Empty;

            int originalTimeout = BitwardenCliInterface.ProcessTimeout;

            BitwardenCliInterface.ProcessTimeout = 1000;
            BitwardenCliCommandResult result = BitwardenCliInterface.ExecuteCommand(LoginCommand);

            BitwardenCliInterface.ProcessTimeout = originalTimeout;
            ConsoleDebugLogger.LogError(result.StandardError);
            // We are expecting exit code of 1.
            if (result.ExitCode == 1 && result.StandardOutput.StartsWith(AlreadyLoggedMessage))
            {
                loggedUserEmail = result.StandardOutput.Substring(AlreadyLoggedMessage.Length).TrimEnd('.');
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 8
0
        public bool Login(BitwardenCredentials credentials)
        {
            CliFlag[] flags;
            if (!string.IsNullOrWhiteSpace(credentials.AuthCode))
            {
                // Use 2FA code.
                flags = new CliFlag[4]
                {
                    CliFlag.StringValue(credentials.Email),
                    CliFlag.StringValue(credentials.Password),
                    CliFlag.FlagWithValue("--code", credentials.AuthCode),
                    BwRawFlag
                };
            }
            else
            {
                flags = new CliFlag[3]
                {
                    CliFlag.StringValue(credentials.Email),
                    CliFlag.StringValue(credentials.Password),
                    BwRawFlag
                };
            }

            BitwardenCliCommandResult result = BitwardenCliInterface.ExecuteCommand(LoginCommand, flags);

            if (!result.TimedOut && result.ExitCode == SuccessExitCode)
            {
                credentials.SessionCode = result.StandardOutput;
                return(true);
            }
            else
            {
                ConsoleDebugLogger.LogError(result.StandardError);
                return(false);
            }
        }