Commit 2f634b01 authored by Ivar Derksen's avatar Ivar Derksen Committed by David Venhoek

Feat: replace cred when refreshing non-singleton credential

parent 69e1f404
......@@ -63,6 +63,22 @@ func (al *AttributeList) Info() *CredentialInfo {
return al.info
}
// EqualsExceptMetadata checks whether two AttributeLists have the same attribute values.
// The attribute containing the metadata information is skipped in this check.
func (al *AttributeList) EqualsExceptMetadata(ol *AttributeList) bool {
if len(al.Ints) != len(ol.Ints) {
return false
}
// Check whether value of all attributes, except for metadata attribute, is equal
for i := 1; i < len(al.Ints); i++ {
if al.Ints[i].Cmp(ol.Ints[i]) != 0 {
return false
}
}
return true
}
func (al *AttributeList) Hash() string {
if al.h == "" {
bytes := []byte{}
......
......@@ -96,6 +96,20 @@ func TestIssuanceOptionalSetAttributes(t *testing.T) {
sessionHelper(t, req, "issue", nil)
}
func TestIssuanceSameAttributesNotSingleton(t *testing.T) {
client, _ := parseStorage(t)
defer test.ClearTestStorage(t)
prevLen := len(client.CredentialInfoList())
req := getIssuanceRequest(true)
sessionHelper(t, req, "issue", client)
req = getIssuanceRequest(false)
sessionHelper(t, req, "issue", client)
require.Equal(t, prevLen+1, len(client.CredentialInfoList()))
}
func TestLargeAttribute(t *testing.T) {
client, _ := parseStorage(t)
defer test.ClearTestStorage(t)
......
......@@ -217,9 +217,18 @@ func (client *Client) addCredential(cred *credential, storeAttributes bool) (err
}
// If this is a singleton credential type, ensure we have at most one by removing any previous instance
if !id.Empty() && cred.CredentialType().IsSingleton {
for len(client.attrs(id)) != 0 {
client.remove(id, 0, false)
// If a credential already exists with exactly the same attribute values (except metadata), delete the previous credential
if !id.Empty() {
if cred.CredentialType().IsSingleton {
for len(client.attrs(id)) != 0 {
_ = client.remove(id, 0, false)
}
}
for i := len(client.attrs(id)) - 1; i >= 0; i-- { // Go backwards through array because remove manipulates it
if client.attrs(id)[i].EqualsExceptMetadata(cred.AttributeList()) {
_ = client.remove(id, i, false)
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment