| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using System.Security.Principal; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Claims |
| | | 10 | | { |
| | | 11 | | [DataContract(Namespace = XsiConstants.Namespace)] |
| | | 12 | | public abstract class ClaimSet : IEnumerable<Claim> |
| | | 13 | | { |
| | | 14 | | private static ClaimSet s_system; |
| | | 15 | | private static ClaimSet s_windows; |
| | | 16 | | private static ClaimSet s_anonymous; |
| | | 17 | | |
| | | 18 | | public static ClaimSet System |
| | | 19 | | { |
| | | 20 | | get |
| | | 21 | | { |
| | 88 | 22 | | if (s_system == null) |
| | | 23 | | { |
| | 3 | 24 | | List<Claim> claims = new List<Claim>(2) |
| | 3 | 25 | | { |
| | 3 | 26 | | Claim.System, |
| | 3 | 27 | | new Claim(ClaimTypes.System, XsiConstants.System, Rights.PossessProperty) |
| | 3 | 28 | | }; |
| | 3 | 29 | | s_system = new DefaultClaimSet(claims); |
| | | 30 | | } |
| | 88 | 31 | | return s_system; |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public static ClaimSet Windows |
| | | 36 | | { |
| | | 37 | | get |
| | | 38 | | { |
| | 0 | 39 | | if (s_windows == null) |
| | | 40 | | { |
| | 0 | 41 | | List<Claim> claims = new List<Claim>(2); |
| | 0 | 42 | | SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.NTAuthoritySid, null); |
| | 0 | 43 | | claims.Add(new Claim(ClaimTypes.Sid, sid, Rights.Identity)); |
| | 0 | 44 | | claims.Add(Claim.CreateWindowsSidClaim(sid)); |
| | 0 | 45 | | s_windows = new DefaultClaimSet(claims); |
| | | 46 | | } |
| | 0 | 47 | | return s_windows; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static ClaimSet Anonymous |
| | | 52 | | { |
| | | 53 | | get |
| | | 54 | | { |
| | 0 | 55 | | if (s_anonymous == null) |
| | | 56 | | { |
| | 0 | 57 | | s_anonymous = new DefaultClaimSet(); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | return s_anonymous; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal static bool SupportedRight(string right) |
| | | 65 | | { |
| | 7 | 66 | | return right == null || |
| | 7 | 67 | | Rights.Identity.Equals(right) || |
| | 7 | 68 | | Rights.PossessProperty.Equals(right); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | |
| | | 72 | | public virtual bool ContainsClaim(Claim claim, IEqualityComparer<Claim> comparer) |
| | | 73 | | { |
| | 0 | 74 | | if (claim == null) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claim)); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | if (comparer == null) |
| | | 80 | | { |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(comparer)); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | IEnumerable<Claim> claims = FindClaims(null, null); |
| | 0 | 85 | | if (claims != null) |
| | | 86 | | { |
| | 0 | 87 | | foreach (Claim matchingClaim in claims) |
| | | 88 | | { |
| | 0 | 89 | | if (comparer.Equals(claim, matchingClaim)) |
| | | 90 | | { |
| | 0 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |
| | 0 | 95 | | return false; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | public virtual bool ContainsClaim(Claim claim) |
| | | 99 | | { |
| | 0 | 100 | | if (claim == null) |
| | | 101 | | { |
| | 0 | 102 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claim)); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | IEnumerable<Claim> claims = FindClaims(claim.ClaimType, claim.Right); |
| | 0 | 106 | | if (claims != null) |
| | | 107 | | { |
| | 0 | 108 | | foreach (Claim matchingClaim in claims) |
| | | 109 | | { |
| | 0 | 110 | | if (claim.Equals(matchingClaim)) |
| | | 111 | | { |
| | 0 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | 0 | 116 | | return false; |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public abstract Claim this[int index] { get; } |
| | | 120 | | public abstract int Count { get; } |
| | | 121 | | public abstract ClaimSet Issuer { get; } |
| | | 122 | | // Note: null string represents any. |
| | | 123 | | public abstract IEnumerable<Claim> FindClaims(string claimType, string right); |
| | | 124 | | public abstract IEnumerator<Claim> GetEnumerator(); |
| | 0 | 125 | | IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } |
| | | 126 | | } |
| | | 127 | | } |