ln.identities/Identity.cs

62 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace ln.identities
{
public class Identity
{
public Guid UniqueID { get; private set; }
public String IdentityName { get; set; }
List<SecureAttribute> secureAttributes = new List<SecureAttribute>();
List<RoleAssignment> roleAssignments = new List<RoleAssignment>();
private Identity()
{
}
private Identity(Guid uniqueID,string identityName)
{
UniqueID = uniqueID;
IdentityName = identityName;
}
public Identity(string identityName) : this(Guid.NewGuid(),identityName){}
public SecureAttribute GetSecureAttribute(Guid uniqueID)
{
foreach (SecureAttribute secureAttribute in secureAttributes)
{
if (secureAttribute.UniqueID.Equals(uniqueID))
return secureAttribute;
}
throw new KeyNotFoundException();
}
public SecureAttribute[] GetSecureAttributes() => secureAttributes.ToArray();
public SecureAttribute[] GetSecureAttributes(String secureAttributeTypeName) => secureAttributeTypeName == null ? GetSecureAttributes() : secureAttributes.Where((secureAttribute)=>secureAttribute.GetAttributeTypeName().Equals(secureAttributeTypeName)).ToArray();
public KeyValuePair<Guid, byte[]>[] GetChallenges() => GetChallenges(null);
public KeyValuePair<Guid,byte[]>[] GetChallenges(String secureAttributeTypeName)
{
SecureAttribute[] sas = GetSecureAttributes(secureAttributeTypeName);
KeyValuePair<Guid, byte[]>[] challenges = new KeyValuePair<Guid, byte[]>[sas.Length];
for (int n = 0; n < sas.Length; n++)
{
challenges[n] = new KeyValuePair<Guid, byte[]>(sas[n].UniqueID, sas[n].CreateChallenge());
}
return challenges;
}
public void AddSecureAttribute(SecureAttribute secureAttribute) => secureAttributes.Add(secureAttribute);
public void RemoveSecureAttribute(SecureAttribute secureAttribute) => secureAttributes.Remove(secureAttribute);
public override bool Equals(object obj) => (obj is Identity other) && other.UniqueID.Equals(UniqueID);
public override int GetHashCode() => UniqueID.GetHashCode();
}
}