| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Security.Principal; |
| | | 8 | | using CoreWCF.IdentityModel.Claims; |
| | | 9 | | using CoreWCF.Security; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.IdentityModel.Policy |
| | | 12 | | { |
| | | 13 | | internal interface IIdentityInfo |
| | | 14 | | { |
| | | 15 | | IIdentity Identity { get; } |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | internal class UnconditionalPolicy : IAuthorizationPolicy, IDisposable |
| | | 19 | | { |
| | | 20 | | private SecurityUniqueId _id; |
| | | 21 | | private ClaimSet _issuance; |
| | | 22 | | private ReadOnlyCollection<ClaimSet> _issuances; |
| | | 23 | | private IIdentity _primaryIdentity; |
| | | 24 | | private bool _disposed = false; |
| | | 25 | | |
| | | 26 | | public UnconditionalPolicy(ClaimSet issuance) |
| | 0 | 27 | | : this(issuance, SecurityUtils.MaxUtcDateTime) |
| | | 28 | | { |
| | 0 | 29 | | } |
| | | 30 | | |
| | 18 | 31 | | public UnconditionalPolicy(ClaimSet issuance, DateTime expirationTime) |
| | | 32 | | { |
| | 18 | 33 | | if (issuance == null) |
| | | 34 | | { |
| | 0 | 35 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuance)); |
| | | 36 | | } |
| | | 37 | | |
| | 18 | 38 | | Initialize(ClaimSet.System, issuance, null, expirationTime); |
| | 18 | 39 | | } |
| | | 40 | | |
| | 10 | 41 | | public UnconditionalPolicy(ReadOnlyCollection<ClaimSet> issuances, DateTime expirationTime) |
| | | 42 | | { |
| | 10 | 43 | | if (issuances == null) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuances)); |
| | | 46 | | } |
| | | 47 | | |
| | 10 | 48 | | Initialize(ClaimSet.System, null, issuances, expirationTime); |
| | 10 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal UnconditionalPolicy(IIdentity primaryIdentity, ClaimSet issuance) |
| | 0 | 52 | | : this(issuance) |
| | | 53 | | { |
| | 0 | 54 | | _primaryIdentity = primaryIdentity; |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | internal UnconditionalPolicy(IIdentity primaryIdentity, ClaimSet issuance, DateTime expirationTime) |
| | 0 | 58 | | : this(issuance, expirationTime) |
| | | 59 | | { |
| | 0 | 60 | | _primaryIdentity = primaryIdentity; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | internal UnconditionalPolicy(IIdentity primaryIdentity, ReadOnlyCollection<ClaimSet> issuances, DateTime expirat |
| | 10 | 64 | | : this(issuances, expirationTime) |
| | | 65 | | { |
| | 10 | 66 | | _primaryIdentity = primaryIdentity; |
| | 10 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | private UnconditionalPolicy(UnconditionalPolicy from) |
| | | 70 | | { |
| | 0 | 71 | | IsDisposable = from.IsDisposable; |
| | 0 | 72 | | _primaryIdentity = from.IsDisposable ? SecurityUtils.CloneIdentityIfNecessary(from._primaryIdentity) : from. |
| | 0 | 73 | | if (from._issuance != null) |
| | | 74 | | { |
| | 0 | 75 | | _issuance = from.IsDisposable ? SecurityUtils.CloneClaimSetIfNecessary(from._issuance) : from._issuance; |
| | | 76 | | } |
| | | 77 | | else |
| | | 78 | | { |
| | 0 | 79 | | _issuances = from.IsDisposable ? SecurityUtils.CloneClaimSetsIfNecessary(from._issuances) : from._issuan |
| | | 80 | | } |
| | 0 | 81 | | Issuer = from.Issuer; |
| | 0 | 82 | | ExpirationTime = from.ExpirationTime; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | private void Initialize(ClaimSet issuer, ClaimSet issuance, ReadOnlyCollection<ClaimSet> issuances, DateTime exp |
| | | 86 | | { |
| | 28 | 87 | | Issuer = issuer; |
| | 28 | 88 | | _issuance = issuance; |
| | 28 | 89 | | _issuances = issuances; |
| | 28 | 90 | | ExpirationTime = expirationTime; |
| | 28 | 91 | | if (issuance != null) |
| | | 92 | | { |
| | 18 | 93 | | IsDisposable = issuance is WindowsClaimSet; |
| | | 94 | | } |
| | | 95 | | else |
| | | 96 | | { |
| | 40 | 97 | | for (int i = 0; i < issuances.Count; ++i) |
| | | 98 | | { |
| | 10 | 99 | | if (issuances[i] is WindowsClaimSet) |
| | | 100 | | { |
| | 0 | 101 | | IsDisposable = true; |
| | 0 | 102 | | break; |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |
| | 10 | 106 | | } |
| | | 107 | | |
| | | 108 | | public string Id |
| | | 109 | | { |
| | | 110 | | get |
| | | 111 | | { |
| | 0 | 112 | | if (_id == null) |
| | | 113 | | { |
| | 0 | 114 | | _id = SecurityUniqueId.Create(); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return _id.Value; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 28 | 121 | | public ClaimSet Issuer { get; private set; } |
| | | 122 | | |
| | | 123 | | internal IIdentity PrimaryIdentity |
| | | 124 | | { |
| | | 125 | | get |
| | | 126 | | { |
| | 91 | 127 | | ThrowIfDisposed(); |
| | 91 | 128 | | if (_primaryIdentity == null) |
| | | 129 | | { |
| | 17 | 130 | | IIdentity identity = null; |
| | 17 | 131 | | if (_issuance != null) |
| | | 132 | | { |
| | 17 | 133 | | if (_issuance is IIdentityInfo) |
| | | 134 | | { |
| | 17 | 135 | | identity = ((IIdentityInfo)_issuance).Identity; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | else |
| | | 139 | | { |
| | 0 | 140 | | for (int i = 0; i < _issuances.Count; ++i) |
| | | 141 | | { |
| | 0 | 142 | | ClaimSet issuance = _issuances[i]; |
| | 0 | 143 | | if (issuance is IIdentityInfo) |
| | | 144 | | { |
| | 0 | 145 | | identity = ((IIdentityInfo)issuance).Identity; |
| | | 146 | | // Preferably Non-Anonymous |
| | 0 | 147 | | if (identity != null && identity != SecurityUtils.AnonymousIdentity) |
| | | 148 | | { |
| | | 149 | | break; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |
| | 17 | 154 | | _primaryIdentity = identity ?? SecurityUtils.AnonymousIdentity; |
| | | 155 | | } |
| | 91 | 156 | | return _primaryIdentity; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | internal ReadOnlyCollection<ClaimSet> Issuances |
| | | 161 | | { |
| | | 162 | | get |
| | | 163 | | { |
| | 26 | 164 | | ThrowIfDisposed(); |
| | 26 | 165 | | if (_issuances == null) |
| | | 166 | | { |
| | 10 | 167 | | List<ClaimSet> issuances = new List<ClaimSet>(1) |
| | 10 | 168 | | { |
| | 10 | 169 | | _issuance |
| | 10 | 170 | | }; |
| | 10 | 171 | | _issuances = issuances.AsReadOnly(); |
| | | 172 | | } |
| | 26 | 173 | | return _issuances; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 32 | 177 | | public DateTime ExpirationTime { get; private set; } |
| | | 178 | | |
| | 58 | 179 | | internal bool IsDisposable { get; private set; } = false; |
| | | 180 | | |
| | | 181 | | internal UnconditionalPolicy Clone() |
| | | 182 | | { |
| | 0 | 183 | | ThrowIfDisposed(); |
| | 0 | 184 | | return (IsDisposable) ? new UnconditionalPolicy(this) : this; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | public virtual void Dispose() |
| | | 188 | | { |
| | 10 | 189 | | if (IsDisposable && !_disposed) |
| | | 190 | | { |
| | 0 | 191 | | _disposed = true; |
| | 0 | 192 | | SecurityUtils.DisposeIfNecessary(_primaryIdentity as WindowsIdentity); |
| | 0 | 193 | | SecurityUtils.DisposeClaimSetIfNecessary(_issuance); |
| | 0 | 194 | | SecurityUtils.DisposeClaimSetsIfNecessary(_issuances); |
| | | 195 | | } |
| | 10 | 196 | | } |
| | | 197 | | |
| | | 198 | | private void ThrowIfDisposed() |
| | | 199 | | { |
| | 121 | 200 | | if (_disposed) |
| | | 201 | | { |
| | 0 | 202 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().FullName |
| | | 203 | | } |
| | 121 | 204 | | } |
| | | 205 | | |
| | | 206 | | public virtual bool Evaluate(EvaluationContext evaluationContext, ref object state) |
| | | 207 | | { |
| | 4 | 208 | | ThrowIfDisposed(); |
| | 4 | 209 | | if (_issuance != null) |
| | | 210 | | { |
| | 0 | 211 | | evaluationContext.AddClaimSet(this, _issuance); |
| | | 212 | | } |
| | | 213 | | else |
| | | 214 | | { |
| | 16 | 215 | | for (int i = 0; i < _issuances.Count; ++i) |
| | | 216 | | { |
| | 4 | 217 | | if (_issuances[i] != null) |
| | | 218 | | { |
| | 4 | 219 | | evaluationContext.AddClaimSet(this, _issuances[i]); |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | // Preferably Non-Anonymous |
| | 4 | 225 | | if (PrimaryIdentity != null && PrimaryIdentity != SecurityUtils.AnonymousIdentity) |
| | | 226 | | { |
| | | 227 | | IList<IIdentity> identities; |
| | 4 | 228 | | if (!evaluationContext.Properties.TryGetValue(SecurityUtils.Identities, out object obj)) |
| | | 229 | | { |
| | 4 | 230 | | identities = new List<IIdentity>(1); |
| | 4 | 231 | | evaluationContext.Properties.Add(SecurityUtils.Identities, identities); |
| | | 232 | | } |
| | | 233 | | else |
| | | 234 | | { |
| | | 235 | | // null if other overrides the property with something else |
| | 0 | 236 | | identities = obj as IList<IIdentity>; |
| | | 237 | | } |
| | | 238 | | |
| | 4 | 239 | | if (identities != null) |
| | | 240 | | { |
| | 4 | 241 | | identities.Add(PrimaryIdentity); |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | |
| | 4 | 245 | | evaluationContext.RecordExpirationTime(ExpirationTime); |
| | 4 | 246 | | return true; |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | } |