| | | 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.Generic; |
| | | 5 | | using System.Runtime.Serialization; |
| | | 6 | | using CoreWCF.Security; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Claims |
| | | 9 | | { |
| | | 10 | | [DataContract(Namespace = XsiConstants.Namespace)] |
| | | 11 | | public class DefaultClaimSet : ClaimSet |
| | | 12 | | { |
| | | 13 | | [DataMember(Name = "Issuer")] |
| | | 14 | | private ClaimSet _issuer; |
| | | 15 | | [DataMember(Name = "Claims")] |
| | | 16 | | private IList<Claim> _claims; |
| | | 17 | | |
| | 12 | 18 | | public DefaultClaimSet(params Claim[] claims) |
| | | 19 | | { |
| | 12 | 20 | | Initialize(this, claims); |
| | 12 | 21 | | } |
| | | 22 | | |
| | 3 | 23 | | public DefaultClaimSet(IList<Claim> claims) |
| | | 24 | | { |
| | 3 | 25 | | Initialize(this, claims); |
| | 3 | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | public DefaultClaimSet(ClaimSet issuer, params Claim[] claims) |
| | | 29 | | { |
| | 0 | 30 | | Initialize(issuer, claims); |
| | 0 | 31 | | } |
| | | 32 | | |
| | 14 | 33 | | public DefaultClaimSet(ClaimSet issuer, IList<Claim> claims) |
| | | 34 | | { |
| | 14 | 35 | | Initialize(issuer, claims); |
| | 14 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override Claim this[int index] |
| | | 39 | | { |
| | 0 | 40 | | get { return _claims[index]; } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public override int Count |
| | | 44 | | { |
| | 0 | 45 | | get { return _claims.Count; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public override ClaimSet Issuer |
| | | 49 | | { |
| | 4 | 50 | | get { return _issuer; } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public override bool ContainsClaim(Claim claim) |
| | | 54 | | { |
| | 13 | 55 | | if (claim == null) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claim)); |
| | | 58 | | } |
| | | 59 | | |
| | 40 | 60 | | for (int i = 0; i < _claims.Count; ++i) |
| | | 61 | | { |
| | 12 | 62 | | if (claim.Equals(_claims[i])) |
| | | 63 | | { |
| | 5 | 64 | | return true; |
| | | 65 | | } |
| | | 66 | | } |
| | 8 | 67 | | return false; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public override IEnumerable<Claim> FindClaims(string claimType, string right) |
| | | 71 | | { |
| | 12 | 72 | | bool anyClaimType = (claimType == null); |
| | 12 | 73 | | bool anyRight = (right == null); |
| | | 74 | | |
| | 32 | 75 | | for (int i = 0; i < _claims.Count; ++i) |
| | | 76 | | { |
| | 14 | 77 | | Claim claim = _claims[i]; |
| | 14 | 78 | | if ((claim != null) && |
| | 14 | 79 | | (anyClaimType || claimType == claim.ClaimType) && |
| | 14 | 80 | | (anyRight || right == claim.Right)) |
| | | 81 | | { |
| | 14 | 82 | | yield return claim; |
| | | 83 | | } |
| | | 84 | | } |
| | 2 | 85 | | } |
| | | 86 | | |
| | | 87 | | public override IEnumerator<Claim> GetEnumerator() |
| | | 88 | | { |
| | 0 | 89 | | return _claims.GetEnumerator(); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | protected void Initialize(ClaimSet issuer, IList<Claim> claims) |
| | | 93 | | { |
| | 41 | 94 | | _issuer = issuer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuer)); |
| | 41 | 95 | | _claims = claims ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claims)); |
| | 41 | 96 | | } |
| | | 97 | | |
| | | 98 | | public override string ToString() |
| | | 99 | | { |
| | 0 | 100 | | return SecurityUtils.ClaimSetToString(this); |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |