public DefaultPoliciesAuthorizationPolicyProvider(IConfiguration configuration, ScopePatternOptions scopePatternOptions, ILogger logger) { _configuration = configuration; _scopePatternOptions = scopePatternOptions; _policyPatternCacheSet = new ConcurrentDictionary <string, ConcurrentDictionary <string, bool> >(); _logger = logger; }
public void TestEvaluateScope(string policyScope, string[] scopeClaims, bool expected, bool isOidc, bool[] cachedValues, int[] expectedLogMessageFrequency) { var scopePatternOptions = new ScopePatternOptions { IsOidc = isOidc }; var claimType = $"{(isOidc ? scopePatternOptions.UserScopePrefix : "")}scope"; var mockClaimsPrincipal = new Mock <ClaimsPrincipal>(); mockClaimsPrincipal.SetupGet(cp => cp.Claims).Returns( scopeClaims.Select(c => new Claim(claimType, c))); var cache = new ConcurrentDictionary <string, bool>(); if (cachedValues != null) { for (int i = 0; i < cachedValues.Length; i++) { cache.TryAdd(scopeClaims[i], cachedValues[i]); } } var mockLogger = new Mock <TestLogger>(); mockLogger.SetupGet(m => m.InternalLogger).Returns(new Mock <ILogger>().Object); mockLogger.SetupGet(m => m.TestOutputHelper).Returns(_output); var handler = new ClaimPatternAuthorizationHandler(policyScope, scopePatternOptions, cache, mockLogger.Object); var actual = handler.EvaluateClaimsPrincipal(mockClaimsPrincipal.Object, handler); Assert.Equal(expected, actual); for (int i = 0; i < logMessages.Length; i++) { mockLogger.Verify(c => c.XLog(LogLevel.Trace, It.IsRegex(logMessages[i])), Times.Exactly(expectedLogMessageFrequency[i])); } }
/// <summary> /// Creates a new instance of <see cref="ClaimPatternAuthorizationHandler"/>. /// </summary> /// <param name="claimType">The claim type that must be absent if no values are provided.</param> /// <param name="AllowedValues">The optional list of claim values, which, if present, /// the claim must NOT match.</param> public ClaimPatternAuthorizationHandler( string requirementScope, ScopePatternOptions options, ConcurrentDictionary <string, bool> policyPatternCache, ILogger logger) { RequirementScope = requirementScope; PolicyPatternCache = policyPatternCache; if (options != null) { IsOidc = options.IsOidc; if (IsOidc) { UserScopePrefix = options.UserScopePrefix?.ToLower(); } ExclusionPrefix = options.ExclusionPrefix; } Logger = logger; }