Skip to content

Commit

Permalink
Merge pull request #30 from Elagoht:feat/store-stats-instead-of-passp…
Browse files Browse the repository at this point in the history
…hrases

Feat/store-stats-instead-of-passphrases
  • Loading branch information
Elagoht authored Jun 17, 2024
2 parents fc9e0a6 + 68c763a commit af4721f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
9 changes: 8 additions & 1 deletion src/BusinessLogic/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ public void FetchAll()
public void Fetch()
{
RoutineAuthControl("fetch", 2);
ReadWritableDatabaseEntry entry = Database.FetchOne(arguments[1]);
/**
* Fetch can read passphrase history as well.
* Don't worry, it's not a security issue.
* Because passphrase history only tracks
* the stats of the passphrase, not the
* passphrase itself.
*/
DatabaseEntry entry = Database.Fetch(arguments[1]);
if (entry == null) Error.EntryNotFound();
entry.TotalAccesses++;
Database.Update(entry.Id, entry, false);
Expand Down
23 changes: 13 additions & 10 deletions src/DataAccess/DataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ public static List<ListableDatabaseEntry> FetchAll() =>
Mapper.ToListable
).ToList();

public static ReadWritableDatabaseEntry FetchOne(string id) =>
Mapper.ToReadWritable(
database.Entries.Find(entry => entry.Id == id)
);
public static DatabaseEntry Fetch(string id) =>
database.Entries.Find(entry => entry.Id == id);

public static List<ListableDatabaseEntry> Query(string keyword) =>
database.Entries.Where(entry =>
Expand All @@ -108,7 +106,7 @@ public static ListableDatabaseEntry Update(string id, ReadWritableDatabaseEntry
if (index == -1) Error.EntryNotFound();
DatabaseEntry existingEntry = database.Entries[index];

// Update changes
// Update fields
existingEntry.Platform = updatedEntry.Platform;
existingEntry.Url = updatedEntry.Url;
existingEntry.Identity = updatedEntry.Identity;
Expand All @@ -119,11 +117,16 @@ public static ListableDatabaseEntry Update(string id, ReadWritableDatabaseEntry
? existingEntry.Updated
: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
// If passphrase is updated, append to history
if (existingEntry.Passphrases.Last().Passphrase != updatedEntry.Passphrase)
existingEntry.Passphrases = Mapper.RegisterNewPassphrase(
existingEntry.Passphrases,
updatedEntry.Passphrase
);
if (existingEntry.Passphrase != updatedEntry.Passphrase)
existingEntry.PassphraseHistory.Add(new()
{
Created = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
Length= updatedEntry.Passphrase.Length,
Strength = Strength.Calculate(updatedEntry.Passphrase)
}
);
// After updating history, finally update the passphrase
existingEntry.Passphrase = updatedEntry.Passphrase;
SaveToFile();

return Mapper.ToListable(existingEntry);
Expand Down
23 changes: 9 additions & 14 deletions src/DataAccess/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,22 @@ public static class Mapper
Created = entry.Created,
Updated = entry.Updated,
TotalAccesses = entry.TotalAccesses,
Passphrases = [new() { Passphrase = entry.Passphrase, Updated = entry.Updated }],
Passphrase = entry.Passphrase,
PassphraseHistory = [
new() {
Created = entry.Created,
Length= entry.Passphrase.Length,
Strength = Strength.Calculate(entry.Passphrase)
}
],
Notes = entry.Notes
};

public static List<TrackablePassphrase> RegisterNewPassphrase(
List<TrackablePassphrase> passphrases,
string passphrase
) => [
.. passphrases,
new() {
Passphrase = passphrase,
Updated = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
}
];

public static ReadWritableDatabaseEntry ToReadWritable(DatabaseEntry entry) => new()
{
Id = entry.Id,
Platform = entry.Platform,
// Get the last set passphrase
Passphrase = entry.Passphrases.Last().Passphrase,
Passphrase = entry.Passphrase,
Url = entry.Url,
Identity = entry.Identity,
Created = entry.Created,
Expand Down
22 changes: 11 additions & 11 deletions src/DataAccess/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public class DatabaseModel
public List<ConstantPair> Constants { get; set; }
}

/// <summary>Model to track passphrase changes over time</summary>
/// <summary>Model to track passphrase statistics changes over time</summary>
public class TrackablePassphrase
{
[JsonPropertyName("passphrase"), Required]
public string Passphrase { get; set; }
[JsonPropertyName("updated"), Required]
public string Updated { get; set; }
[JsonPropertyName("strength"), Required]
public int Strength { get; set; }
[JsonPropertyName("length"), Required]
public int Length { get; set; }
[JsonPropertyName("created"), Required]
public string Created { get; set; }
}

/// <summary>Model to use on JWT related operations</summary>
Expand Down Expand Up @@ -70,7 +72,7 @@ public class ListableDatabaseEntry : CountableDatabaseEntry
public string Updated { get; set; }
}

/// <summary>Single passphrase entry to use on read and write operations</summary>
/// <summary>Details to use on read and write operations</summary>
public class ReadWritableDatabaseEntry : ListableDatabaseEntry
{
[JsonPropertyName("passphrase"), Required]
Expand All @@ -80,11 +82,9 @@ public class ReadWritableDatabaseEntry : ListableDatabaseEntry
}

/// <summary>Maximum detailed model to save on database file</summary>
public class DatabaseEntry : ListableDatabaseEntry
public class DatabaseEntry : ReadWritableDatabaseEntry
{
[JsonPropertyName("passphrases"), Required]
public List<TrackablePassphrase> Passphrases { get; set; }
[JsonPropertyName("notes")] // Optional
public string Notes { get; set; }
[JsonPropertyName("passphraseHistory")]
public List<TrackablePassphrase> PassphraseHistory { get; set; }
}
}

0 comments on commit af4721f

Please sign in to comment.