Skip to content

Commit

Permalink
Add attributes variables to KeyEntryId during publish/links
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Feb 8, 2024
1 parent a86be25 commit 305ae3e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions KeyManager.Library.UI/FavoriteExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static class FavoriteExtension
ks.Properties = fav.Properties;
ks.DefaultKeyEntries = fav.DefaultKeyEntries;

ks.Attributes[KeyStore.KeyStore.ATTRIBUTE_NAME] = fav.Name;

return ks;
}

Expand Down
1 change: 1 addition & 0 deletions KeyManager.Library.UI/Favorites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public Favorite CreateFromKeyStore(KeyStore.KeyStore store, bool save)
SaveToFile();
}
log.Info(string.Format("New Favorite `{0}` saved.", favorite.Name));
store.Attributes[KeyStore.KeyStore.ATTRIBUTE_NAME] = favorite.Name;
return favorite;
}
}
Expand Down
30 changes: 29 additions & 1 deletion KeyManager.Library/KeyStore/KeyEntryId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Leosac.KeyManager.Library.KeyStore
{
public class KeyEntryId : ObservableObject, IEquatable<KeyEntryId>
public class KeyEntryId : ObservableObject, IEquatable<KeyEntryId>, ICloneable
{
public KeyEntryId()
{
Expand Down Expand Up @@ -86,5 +86,33 @@ public override string ToString()
{
return string.Format("Key Entry Id (Identifier: `{0}`, Label: `{1}`)", Id, Label);
}

public object Clone()
{
return Clone(null);
}

public KeyEntryId Clone(IDictionary<string, string>? attributes)
{
var clone = new KeyEntryId();
clone.Id = Id;
clone.Label = Label;
if (attributes != null)
{
foreach (var key in attributes.Keys)
{
if (!string.IsNullOrEmpty(clone.Id))
{
clone.Id = clone.Id.Replace("%{" + key + "}", attributes[key]);
}
if (!string.IsNullOrEmpty(clone.Label))
{
clone.Label = clone.Label.Replace("%{" + key + "}", attributes[key]);
}
}
}
clone.Handle = Handle;
return clone;
}
}
}
7 changes: 5 additions & 2 deletions KeyManager.Library/KeyStore/KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public abstract class KeyStore
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);

public const string ATTRIBUTE_NAME = "name";

protected readonly JsonSerializerSettings _jsonSettings;

protected KeyStore()
Expand Down Expand Up @@ -253,6 +255,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
var entry = await Get(id, keClass);
if (entry != null)
{
entry.Identifier = entry.Identifier.Clone(Attributes);
if (entry.Link != null && entry.Link.KeyIdentifier.IsConfigured() && !string.IsNullOrEmpty(entry.Link.KeyStoreFavorite))
{
var cryptogram = new KeyEntryCryptogram
Expand All @@ -272,7 +275,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
KeyStore = ks,
KeyEntry = entry
};
cryptogram.Value = await ks.ResolveKeyEntryLink(entry.Link.KeyIdentifier, keClass, ComputeDivInput(divContext, entry.Link.DivInput), entry.Link.WrappingKeyId, entry.Link.WrappingKeySelector);
cryptogram.Value = await ks.ResolveKeyEntryLink(entry.Link.KeyIdentifier.Clone(Attributes), keClass, ComputeDivInput(divContext, entry.Link.DivInput), entry.Link.WrappingKeyId, entry.Link.WrappingKeySelector);
await ks.Close();
}
}
Expand All @@ -294,7 +297,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
KeyEntry = entry,
KeyContainer = kv
};
kv.Key.SetAggregatedValueAsString(await ks.ResolveKeyLink(kv.Key.Link.KeyIdentifier, keClass, kv.Key.Link.ContainerSelector, ComputeDivInput(divContext, kv.Key.Link.DivInput)));
kv.Key.SetAggregatedValueAsString(await ks.ResolveKeyLink(kv.Key.Link.KeyIdentifier.Clone(Attributes), keClass, kv.Key.Link.ContainerSelector, ComputeDivInput(divContext, kv.Key.Link.DivInput)));
await ks.Close();
}
}
Expand Down

0 comments on commit 305ae3e

Please sign in to comment.