|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
3 | 4 | using Microsoft.AspNetCore.Authorization; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using Microsoft.JSInterop; |
4 | 7 |
|
5 | 8 | namespace Casbin.AspNetCore.Authorization.Policy |
6 | 9 | { |
7 | | - public class CasbinPolicyCreator : ICasbinPolicyCreator |
| 10 | + public class DefaultCasbinAuthorizationPolicyProvider : ICasbinAuthorizationPolicyProvider |
8 | 11 | { |
9 | | - public CasbinPolicyCreator() |
| 12 | + public DefaultCasbinAuthorizationPolicyProvider(IOptions<CasbinAuthorizationOptions> options) |
10 | 13 | { |
11 | | - _emptyPolicy = new AuthorizationPolicy(_casbinAuthorizationRequirements, Array.Empty<string>()); |
| 14 | + if (options is null) |
| 15 | + { |
| 16 | + throw new NullReferenceException(nameof(options)); |
| 17 | + } |
| 18 | + |
| 19 | + string? defaultAuthenticationSchemes = options.Value.DefaultAuthenticationSchemes; |
| 20 | + ICollection<string> authenticationSchemes = new List<string>(); |
| 21 | + if (defaultAuthenticationSchemes is not null) |
| 22 | + { |
| 23 | + AddAuthenticationSchemes(authenticationSchemes, defaultAuthenticationSchemes); |
| 24 | + } |
| 25 | + _defaultPolicy = new AuthorizationPolicy(_casbinAuthorizationRequirements, authenticationSchemes); |
12 | 26 | } |
13 | 27 |
|
14 | 28 | private readonly IEnumerable<IAuthorizationRequirement> _casbinAuthorizationRequirements = |
15 | | - new []{CasbinAuthorizationRequirement.Requirement}; |
| 29 | + new[] { CasbinAuthorizationRequirement.Requirement }; |
16 | 30 |
|
17 | | - private readonly AuthorizationPolicy _emptyPolicy; |
| 31 | + private readonly AuthorizationPolicy _defaultPolicy; |
18 | 32 |
|
19 | | - public AuthorizationPolicy Create(IEnumerable<ICasbinAuthorizationData> authorizationData) |
| 33 | + public AuthorizationPolicy GetAuthorizationPolicy(IEnumerable<ICasbinAuthorizationData> authorizationData) |
20 | 34 | { |
21 | 35 | if (authorizationData is null) |
22 | 36 | { |
23 | 37 | throw new ArgumentNullException(nameof(authorizationData)); |
24 | 38 | } |
25 | 39 |
|
26 | | - IList<string>? authenticationSchemes = null; |
| 40 | + ICollection<string>? authenticationSchemes = null; |
27 | 41 | foreach (var data in authorizationData) |
28 | 42 | { |
29 | | - string[]? authTypesSplit = data.AuthenticationSchemes?.Split(','); |
30 | | - if (authTypesSplit is null || authTypesSplit.Length > 0 is false) |
| 43 | + if (string.IsNullOrWhiteSpace(data.AuthenticationSchemes)) |
31 | 44 | { |
32 | | - return _emptyPolicy; |
| 45 | + continue; |
33 | 46 | } |
34 | 47 |
|
35 | | - authenticationSchemes ??= new List<string>(); |
| 48 | + authenticationSchemes = _defaultPolicy.AuthenticationSchemes as ICollection<string> ?? |
| 49 | + _defaultPolicy.AuthenticationSchemes.ToList(); |
| 50 | + |
| 51 | + AddAuthenticationSchemes(authenticationSchemes, data.AuthenticationSchemes); |
| 52 | + } |
36 | 53 |
|
37 | | - foreach (var authType in authTypesSplit) |
| 54 | + return authenticationSchemes is not null |
| 55 | + ? new AuthorizationPolicy(_casbinAuthorizationRequirements, authenticationSchemes) |
| 56 | + : _defaultPolicy; |
| 57 | + } |
| 58 | + |
| 59 | + private static void AddAuthenticationSchemes(ICollection<string> authenticationSchemes, |
| 60 | + string authenticationSchemesString) |
| 61 | + { |
| 62 | + string[] authTypesSplit = authenticationSchemesString.Split(','); |
| 63 | + if (authTypesSplit.Length == 0) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + foreach (var authType in authTypesSplit) |
| 69 | + { |
| 70 | + if (string.IsNullOrWhiteSpace(authType) is false) |
38 | 71 | { |
39 | | - if (string.IsNullOrWhiteSpace(authType) is false) |
40 | | - { |
41 | | - authenticationSchemes.Add(authType.Trim()); |
42 | | - } |
| 72 | + authenticationSchemes.Add(authType.Trim()); |
43 | 73 | } |
44 | 74 | } |
45 | | - |
46 | | - return authenticationSchemes is null ? _emptyPolicy : new AuthorizationPolicy(_casbinAuthorizationRequirements, authenticationSchemes); |
47 | 75 | } |
48 | 76 | } |
49 | 77 | } |
0 commit comments