Skip to content

Commit a916361

Browse files
author
Jon Kjetil Øye
committed
ABAC XacmlPolicy attribute dictionary helper method
#474 As part of the integration with OED/DD (Digitalt dødsbo) as a new external role provider, an easy way to analyse and extract all attributeIds and values from a XacmlPolicy is needed. This will be used to evaluate whether or not the policy contains a subject attribute for an OED/DD role code, and since it's populated to the XacmlPolicy object it will be cached along side policy itself. The logic can later be reused by the resource-registry which will need same logic for analysing the policy for building rolecode register and required validation logic when publishing a resource.
1 parent 008e3a3 commit a916361

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/Altinn.Authorization.ABAC/Xacml/XacmlPolicy.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using Altinn.Authorization.ABAC.Constants;
46
using Altinn.Authorization.ABAC.Utils;
57

68
namespace Altinn.Authorization.ABAC.Xacml
@@ -96,6 +98,8 @@ public class XacmlPolicy
9698

9799
private readonly ICollection<XacmlVariableDefinition> variableDefinitions = new Collection<XacmlVariableDefinition>();
98100

101+
private readonly IDictionary<string, IDictionary<string, Collection<string>>> categoryAttributes = new Dictionary<string, IDictionary<string, Collection<string>>>();
102+
99103
private XacmlTarget target;
100104
private Uri policyId;
101105
private Uri ruleCombiningAlgId;
@@ -308,6 +312,51 @@ public ICollection<XacmlAdviceExpression> AdviceExpressions
308312
}
309313
}
310314

315+
/// <summary>
316+
/// Returns a dictionary of all unique attribute ids and a collection of all their values, which exists across all rules in the policy, for a given match attribute category.
317+
/// </summary>
318+
/// <param name="matchAttributeCategory">The Xacml match attribute category to collect attributes values of</param>
319+
/// <returns>Dictionary of attribute ids and list of values</returns>
320+
public IDictionary<string, Collection<string>> GetAttributeDictionaryByCategory(string matchAttributeCategory)
321+
{
322+
if (categoryAttributes.ContainsKey(matchAttributeCategory))
323+
{
324+
return categoryAttributes[matchAttributeCategory];
325+
}
326+
327+
IDictionary<string, Collection<string>> categoryAttributeDict = new Dictionary<string, Collection<string>>();
328+
categoryAttributes.Add(matchAttributeCategory, categoryAttributeDict);
329+
330+
foreach (XacmlRule rule in Rules)
331+
{
332+
// should we care about permit?
333+
if (rule.Effect.Equals(XacmlEffectType.Permit) && rule.Target != null)
334+
{
335+
foreach (XacmlAnyOf anyOf in rule.Target.AnyOf)
336+
{
337+
foreach (XacmlAllOf allOf in anyOf.AllOf)
338+
{
339+
foreach (XacmlMatch xacmlMatch in allOf.Matches)
340+
{
341+
if (xacmlMatch.AttributeDesignator.Category.Equals(matchAttributeCategory))
342+
{
343+
string attributeId = xacmlMatch.AttributeDesignator.AttributeId.AbsoluteUri;
344+
if (!categoryAttributeDict.ContainsKey(attributeId))
345+
{
346+
categoryAttributeDict.Add(attributeId, new Collection<string>());
347+
}
348+
349+
categoryAttributeDict[attributeId].Add(xacmlMatch.AttributeValue.Value);
350+
}
351+
}
352+
}
353+
}
354+
}
355+
}
356+
357+
return categoryAttributes[matchAttributeCategory];
358+
}
359+
311360
/// <summary>
312361
/// The namespaces used in Policy
313362
/// </summary>

0 commit comments

Comments
 (0)