/// <summary> /// Get a list of all accounts stored into a virtual database /// </summary> /// <param name="encrypted">Identifies if you want encrypted records. They're useless by the way</param> /// <returns>List of accounts</returns> public List <Account> GetAccounts(bool encrypted = false) { using (var context = new PassContext()) { var acc = context.Accounts.Where(d => d.DbID == _Database.Id).ToList(); if (acc == null) { throw new ArgumentNullException("No accounts found"); } //if (acc.Count() == 0) // return null; if (encrypted) { return(acc); } else { List <Account> unencrypted = new List <Account>(); foreach (var a in acc) { unencrypted.Add(Decrypt(a)); } return(unencrypted); } } }
/// <summary> /// Updates the name of the database /// </summary> /// <param name="newname">The new name for the database</param> /// <returns>bool</returns> public bool UpdateName(string newname) { if (string.IsNullOrEmpty(newname)) { return(false); } var current = GetDB(_Database.Name); current.Name = newname; try { using (var context = new PassContext()) { context.Databases.Update(current); context.SaveChanges(); _Database = GetDB(current.Name); } return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
/// <summary> /// Remove an account from the database /// </summary> /// <param name="a"></param> /// <returns>True or false</returns> public bool Delete(Account a) { try { using (var context = new PassContext()) { var acc = context.Accounts.First(e => e.Id == a.Id); context.Accounts.Remove(acc); context.SaveChanges(); } return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
/// <summary> /// Add an account to the database /// </summary> /// <param name="a">The account object</param> /// <returns>True or false</returns> public bool Add(Account a) { try { using (var context = new PassContext()) { a.DbID = _Database.Id; context.Accounts.Add(Encrypt(a)); context.SaveChanges(); } return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
//Get the database object by name private static DB GetDB(string name) { using (var context = new PassContext()) { if (context.Databases.Any(d => d.Name == name)) { return(context.Databases.First(d => d.Name == name)); } else { return(null); } } }
/// <summary> /// Delete a virtual database from your database /// </summary> /// <param name="name">The name of the database</param> /// <returns>True if it succeed, false if it failed</returns> public static bool DeleteDB(string name) { try { using (var context = new PassContext()) { var db = context.Databases.First(e => e.Name == name); context.Remove(db); var accounts = context.Accounts.Where(a => a.DbID == db.Id); context.Accounts.RemoveRange(accounts); context.SaveChanges(); } return(true); } catch { return(false); } }
/// <summary> /// Adds TwoFactorAuthentication to current database /// </summary> /// <returns>True or false</returns> public bool Add2FA() { if (!_IsLoggedIn) { return(false); } _Database.TwoFactorSecret = GenerateSecret(); using (var context = new PassContext()) { context.Databases.Update(_Database); context.SaveChanges(); return(true); } }
//Add a range of accounts private static bool AddRange(Account[] ac) { try { using (var context = new PassContext()) { foreach (var a in ac) { context.Accounts.Add(a); } context.SaveChanges(); return(true); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
/// <summary> /// Updates the password of a virtual database /// </summary> /// <param name="oldpass">The current password</param> /// <param name="newpass">The new password</param> /// <returns>False if the current pass is wrong or it failed, true if it succeed</returns> public bool UpdatePassword(string oldpass, string newpass) { try { var accounts = GetAccounts(); if (oldpass == _Password) { using (var context = new PassContext()) { //try //{ _Database.Passhash = Easy.Hashing.Hash(newpass); _Password = newpass; _Encryption = new Easy.Encryption(Aes.Create(), _Password, _Salt, 10000); context.Databases.Update(_Database); context.SaveChanges(); //try //{ foreach (var account in accounts) { if (!Update(account)) { throw new ArgumentException("Updating an entry failed"); } } return(true); //} //catch (Exception e) { Console.WriteLine(e.Message); return false; } //} //catch(Exception e) { Console.WriteLine(e.Message); return false; } } } else { return(false); } } catch (Exception e) { throw e; } }
/// <summary> /// Restores an virtual database into your database /// </summary> /// <param name="path">The path of your database</param> /// <param name="name">The name you want to use for the database</param> /// <returns>True of it succeed, false if it failed</returns> public static bool Restore(string path, string name) { if (!File.Exists(path)) { return(false); } try { string json = File.ReadAllText(path); var obj = JsonConvert.DeserializeObject <DBObject>(json); //Import database using (var context = new PassContext()) { string newname = (string.IsNullOrEmpty(name)) ? "_" + obj.Database.Name : name; context.Databases.Add(new DB { Name = newname, Passhash = obj.Database.Passhash, TwoFactorSecret = obj.Database.TwoFactorSecret, Salt = obj.Database.Salt }); context.SaveChanges(); var db = GetDB(newname); if (db != null) { List <Account> toadd = new List <Account>(); foreach (var a in obj.Accounts) { a.DbID = db.Id; toadd.Add(a); } return(AddRange(toadd.ToArray())); } else { Console.WriteLine("ERROR: DB NOT FOUND"); return(false); } } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
/// <summary> /// Updates an account in the database /// </summary> /// <param name="a">The account object</param> /// <returns>True or false</returns> public bool Update(Account a) { try { using (var context = new PassContext()) { var acc = context.Accounts.First(b => b.Id == a.Id); a = Encrypt(a); acc.Username = a.Username; acc.Password = a.Password; acc.Description = a.Description; acc.Title = a.Title; acc.Type = a.Type; acc.TwoFactorSecret = a.TwoFactorSecret; context.Accounts.Update(acc); context.SaveChanges(); } return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
/// <summary> /// Remove TwoFactorAuthentication from current database /// </summary> /// <returns>True or false</returns> public bool Remove2FA() { if (!_IsLoggedIn) { return(false); } if (string.IsNullOrEmpty(_Database.TwoFactorSecret)) { return(false); } _Database.TwoFactorSecret = string.Empty; using (var context = new PassContext()) { context.Databases.Update(_Database); context.SaveChanges(); return(true); } }
/// <summary> /// Opens and verifys an virtual database /// </summary> /// <param name="database">The name of the virtual database you want to open</param> /// <param name="password">The password used for encryption</param> /// <returns>LoginResult</returns> public LoginResult Login(string database, string password) { if (_Tries > 10) { return(LoginResult.TooMuchTries); } var dbs = GetDB(database); if (dbs == null) { return(LoginResult.Database_Not_Exist); } using (var context = new PassContext()) { var db = context.Databases.First(d => d.Id == dbs.Id); if (Easy.Hashing.Verify(db.Passhash, password)) { _Tries = 0; _Database = db; _Password = password; _Salt = db.Salt; _Encryption = new Easy.Encryption(Aes.Create(), _Password, _Salt, 10000); if (!string.IsNullOrEmpty(db.TwoFactorSecret)) { _Needs2FA = true; return(LoginResult.Needs2FA); } _IsLoggedIn = true; return(LoginResult.Success); } else { _Tries++; return(LoginResult.PasswordWrong); } } }
/// <summary> /// Create a new virtual database into your database /// </summary> /// <param name="name">The name. This must be unique</param> /// <param name="password">The password used for authentication and encryption</param> /// <returns>false if name is in use or it failed, true if it succeed</returns> public static bool CreateDB(string name, string password) { try { using (var context = new PassContext()) { if (context.Databases.Any(e => e.Name == name)) { return(false); } DB d = new DB { Name = name, Passhash = Easy.Hashing.Hash(password), Salt = $"A{name}$*(!@#$){name}a" }; context.Databases.Add(d); context.SaveChanges(); } return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }