示例#1
0
        public async Task PwsFileV3_ItemData_UnkownFields_ArePersisted()
        {
            var d1 = new ItemData();

            d1.CreateUuid();
            d1.Title    = "future";
            d1.Password = "******";
            var uv = new byte[] { 55, 42, 78, 30, 16, 93 };

            d1.SetUnknownField(Item.FieldType.UnknownTesting, uv);

            var ms  = new MemoryStream();
            var pws = new PwsFileV3(ms, CreatePasskey(), FileMode.Create);
            await pws.OpenAsync();

            await pws.WriteRecordAsync(d1);

            pws.Dispose();

            ms.Position = 0;
            var read = (PwsFileV3)await PwsFile.OpenAsync(ms, CreatePasskey());

            var item = await read.ReadRecordAsync();

            Assert.AreEqual(d1, item);
            Assert.IsNull(await read.ReadRecordAsync());
            read.Dispose();
        }
示例#2
0
        public async Task PwsFileV3_ItemData_SaveItem_StoresItemsCorrectly()
        {
            var smallItem = CreateSmallItem();
            var fullItem  = CreateFullItem();


            var ms  = new MemoryStream();
            var pws = new PwsFileV3(ms, CreatePasskey(), FileMode.Create);
            await pws.OpenAsync();


            await pws.WriteRecordAsync(smallItem);

            await pws.WriteRecordAsync(fullItem);

            pws.Dispose();

            ms.Position = 0;
            var read = (PwsFileV3)await PwsFile.OpenAsync(ms, CreatePasskey());

            var readSmallItem = await read.ReadRecordAsync();

            var readFullItem = await read.ReadRecordAsync();

            Assert.AreEqual(smallItem, readSmallItem);
            Assert.AreEqual(fullItem, readFullItem);
            //33
            File.WriteAllBytes(@"q:\Projekte_NoBackup\pwsafe\ConsoleApplication1\testcs.dat", ms.ToArray());
        }
示例#3
0
        private static async Task HandleListCommand(string alias, string file, string password, string group, IConsole console)
        {
            string filepath = await ConsoleHelper.GetPwsFilePath(file, alias);

            if (!File.Exists(filepath))
            {
                System.Console.Error.WriteLine($"Can't locate a valid file, please check your command parameters or configuration in ~/pwsafe.json");
                return;
            }

            SecureString secureString = ConsoleHelper.GetSecureString(password);

            try
            {
                using FileStream stream = File.OpenRead(filepath);
                using MemoryStream ms   = new MemoryStream();
                stream.CopyTo(ms);
                ms.Position = 0;
                PwsFileV3 pwsFile = (PwsFileV3)await PwsFile.OpenAsync(ms, secureString);

                List <ItemData> items = await PwsFileHelper.ReadAllItemsFromPwsFileV3Async(pwsFile);

                Group rootGroup = Group.GroupItems(items);

                if (string.IsNullOrWhiteSpace(group))
                {
                    PrintGroup(rootGroup);
                }
                else
                {
                    Group?subGroup = rootGroup.FindSubGroup(item => item.Path == group);

                    if (subGroup is null)
                    {
                        System.Console.Error.WriteLine("No such group, {0}", group);
                    }
                    else
                    {
                        System.Console.WriteLine("Items ({0}):", subGroup.Items.Count);
                        subGroup.Items.ForEach((item) =>
                        {
                            System.Console.WriteLine(item.Title);
                        });
                        System.Console.WriteLine("Groups ({0}):", subGroup.Groups.Count);
                        subGroup.Groups.ForEach((item) =>
                        {
                            System.Console.WriteLine(item.Name);
                        });
                    }
                }

                pwsFile.Dispose();
            }
            catch (Exception e)
            {
                System.Console.Error.WriteLine(e.Message);
                throw;
            }
        }
示例#4
0
        public async Task PwsFileV3_Open_WrongPassword_ThrowsException()
        {
            var ms  = new MemoryStream();
            var pws = new PwsFileV3(ms, CreatePasskey(), FileMode.Create);
            await pws.OpenAsync();

            pws.Dispose();

            ms.Position = 0;
            await PwsFile.OpenAsync(ms, CreatePasskey("x"));
        }
示例#5
0
        private static async Task HandleRootCommand(string ALIAS, string FILE, string TITLE, string PASSWORD, IConsole console)
        {
            string filepath = await ConsoleHelper.GetPwsFilePath(FILE, ALIAS);

            if (!File.Exists(filepath))
            {
                System.Console.Error.WriteLine($"Can't locate a valid file, please check your command parameters or configuration in ~/pwsafe.json");
                return;
            }

            if (string.IsNullOrWhiteSpace(TITLE))
            {
                TITLE = ConsoleHelper.ReadString("Title:");
                if (string.IsNullOrWhiteSpace(TITLE))
                {
                    System.Console.Error.WriteLine($"Title is required");
                    return;
                }
            }

            SecureString secureString = ConsoleHelper.GetSecureString(PASSWORD);

            try
            {
                using FileStream stream = File.OpenRead(filepath);
                using MemoryStream ms   = new MemoryStream();
                stream.CopyTo(ms);
                ms.Position = 0;
                PwsFileV3 pwsFile = (PwsFileV3)await PwsFile.OpenAsync(ms, secureString);

                List <ItemData> items = await PwsFileHelper.ReadAllItemsFromPwsFileV3Async(pwsFile);

                ItemData?item = items.Where(item => item.Title == TITLE).FirstOrDefault();
                if (item is null)
                {
                    System.Console.Error.WriteLine($"Can't find the password of {TITLE}");
                    return;
                }

                TextCopy.ClipboardService.SetText(item.Password);
                System.Console.WriteLine("Successfully copy password to clipboard");
                pwsFile.Dispose();
            }
            catch (Exception e)
            {
                System.Console.Error.WriteLine("Unexpected error: {0}", e.Message);
                throw;
            }
        }
示例#6
0
        public async Task PwsFileV3_Open_FileCreated_OvertakesHeaderData()
        {
            var ms  = new MemoryStream();
            var pws = new PwsFileV3(ms, CreatePasskey(), FileMode.Create);

            var hdr1 = new PwsFileHeader
            {
                PrefString    = "aPrefString",
                WhenLastSaved = new DateTime(2005, 2, 20, 1, 2, 3), // overwritten
                LastSavedBy   = "aUser",
                LastSavedOn   = "aMachine",
                WhatLastSaved = "PasswordSafe test framework",
                DbName        = "aName",
                DbDescription = "Test the header's persistency"
            };

            pws.Header = hdr1;
            // write to ms
            await pws.OpenAsync();

            pws.Dispose();

            ms.Position = 0;

            var read = (PwsFileV3)await PwsFile.OpenAsync(ms, CreatePasskey());

            Assert.AreEqual(hdr1.PrefString, read.Header.PrefString);
            Assert.AreNotEqual(hdr1.WhenLastSaved, read.Header.WhenLastSaved);
            Assert.AreEqual(hdr1.LastSavedBy, read.Header.LastSavedBy);
            Assert.AreEqual(hdr1.LastSavedOn, read.Header.LastSavedOn);
            Assert.AreEqual(hdr1.WhatLastSaved, read.Header.WhatLastSaved);
            Assert.AreEqual(hdr1.DbName, read.Header.DbName);
            Assert.AreEqual(hdr1.DbDescription, read.Header.DbDescription);

            while (await read.ReadRecordAsync() != null)
            {
                // read till end, so Dispose won't throw (end record is included in hmac)
            }

            read.Dispose();
        }
示例#7
0
        public async Task PwsFileV3_PwPolicy_IsPersisted()
        {
            var ms  = new MemoryStream();
            var pws = new PwsFileV3(ms, CreatePasskey(), FileMode.Create);

            pws.PasswordPolicies["Test"] = new PwPolicy
            {
                Flags           = PwPolicyFlags.UseEasyVision | PwPolicyFlags.UseSymbols,
                Length          = 10,
                DigitMinLength  = 1,
                LowerMinLength  = 2,
                SymbolMinLength = 3,
                UpperMinLength  = 4,
                Symbols         = "{}"
            };
            await pws.OpenAsync();

            pws.Dispose();

            ms.Position = 0;
            var read = (PwsFileV3)await PwsFile.OpenAsync(ms, CreatePasskey());

            Assert.AreEqual(1, read.PasswordPolicies.Count);
            var pwp = read.PasswordPolicies["Test"];

            Assert.AreEqual(PwPolicyFlags.UseEasyVision | PwPolicyFlags.UseSymbols, pwp.Flags);
            Assert.AreEqual(10, pwp.Length);
            Assert.AreEqual(1, pwp.DigitMinLength);
            Assert.AreEqual(2, pwp.LowerMinLength);
            Assert.AreEqual(3, pwp.SymbolMinLength);
            Assert.AreEqual(4, pwp.UpperMinLength);
            Assert.AreEqual("{}", pwp.Symbols);

            while (await read.ReadRecordAsync() != null)
            {
                // read till end, so Dispose won't throw (end record is included in hmac)
            }
            read.Dispose();
        }