| | | 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.Claims; |
| | | 8 | | using System.Security.Principal; |
| | | 9 | | using CoreWCF.IdentityModel.Policy; |
| | | 10 | | using SysClaimSet = CoreWCF.IdentityModel.Claims.ClaimSet; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Defines an AuthorizationPolicy that carries the IDFx Claims. When IDFx is enabled |
| | | 16 | | /// a new set of Security Token Authenticators are added to the system. These Authenticators |
| | | 17 | | /// will generate the new Claims defined in System.Security.Claims. |
| | | 18 | | /// </summary> |
| | | 19 | | internal class AuthorizationPolicy : IAuthorizationPolicy |
| | | 20 | | { |
| | | 21 | | public const string ClaimsPrincipalKey = "ClaimsPrincipal"; // This key must be different from "Principal". "Pri |
| | | 22 | | public const string IdentitiesKey = "Identities"; |
| | 28 | 23 | | private readonly List<ClaimsIdentity> _identityCollection = new List<ClaimsIdentity>(); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes an instance of <see cref="AuthorizationPolicy"/> |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public AuthorizationPolicy() |
| | | 29 | | { |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes an instance of <see cref="AuthorizationPolicy"/> |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="identity">ClaimsIdentity for the AuthorizationPolicy.</param> |
| | | 36 | | /// <exception cref="ArgumentNullException">One of the input argument is null.</exception> |
| | 0 | 37 | | public AuthorizationPolicy(ClaimsIdentity identity) |
| | | 38 | | { |
| | 0 | 39 | | if (identity == null) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | _identityCollection.Add(identity); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes an instance of <see cref="AuthorizationPolicy"/> |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="identityCollection">Collection of identities.</param> |
| | 28 | 51 | | public AuthorizationPolicy(IEnumerable<ClaimsIdentity> identityCollection) |
| | | 52 | | { |
| | 28 | 53 | | if (identityCollection == null) |
| | | 54 | | { |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identityCollection)); |
| | | 56 | | } |
| | | 57 | | |
| | 28 | 58 | | List<ClaimsIdentity> collection = new List<ClaimsIdentity>(); |
| | 112 | 59 | | foreach (ClaimsIdentity identity in identityCollection) |
| | | 60 | | { |
| | 28 | 61 | | collection.Add(identity); |
| | | 62 | | } |
| | | 63 | | |
| | 28 | 64 | | _identityCollection = collection; |
| | 28 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets a ClaimsIdentity collection. |
| | | 69 | | /// </summary> |
| | | 70 | | public ReadOnlyCollection<ClaimsIdentity> IdentityCollection |
| | | 71 | | { |
| | | 72 | | get |
| | | 73 | | { |
| | 0 | 74 | | return _identityCollection.AsReadOnly(); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | #region IAuthorizationPolicy Members |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Evaluates the current Policy. This is provided for backward compatibility |
| | | 82 | | /// of WCF Claims model. We always return true without affecting the EvaluationContext. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="evaluationContext">The current EvaluationContext.</param> |
| | | 85 | | /// <param name="state">The reference state object.</param> |
| | | 86 | | /// <returns>True if the Policy was successfully applied.</returns> |
| | | 87 | | public bool Evaluate(EvaluationContext evaluationContext, ref object state) |
| | | 88 | | { |
| | 28 | 89 | | if (null == evaluationContext || null == evaluationContext.Properties) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(evaluationContext)); |
| | | 92 | | } |
| | | 93 | | |
| | 28 | 94 | | if (0 == _identityCollection.Count) |
| | | 95 | | { |
| | | 96 | | // |
| | | 97 | | // Nothing to do here. |
| | | 98 | | // |
| | 0 | 99 | | return true; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | // |
| | | 103 | | // Locate or create the ClaimsPrincipal |
| | | 104 | | // |
| | 28 | 105 | | if (!evaluationContext.Properties.TryGetValue(ClaimsPrincipalKey, out object principalObj)) |
| | | 106 | | { |
| | 28 | 107 | | ClaimsPrincipal principalToAdd = CreateClaimsPrincipalFromIdentities(_identityCollection); |
| | | 108 | | |
| | 28 | 109 | | evaluationContext.Properties.Add(ClaimsPrincipalKey, principalToAdd); |
| | | 110 | | } |
| | | 111 | | else |
| | | 112 | | { |
| | 0 | 113 | | if (principalObj is ClaimsPrincipal principal && null != principal.Identities) |
| | | 114 | | { |
| | 0 | 115 | | principal.AddIdentities(_identityCollection); |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // |
| | | 123 | | // Locate or create evaluationContext.Properties[ "Identities" ] with identities |
| | | 124 | | // |
| | 28 | 125 | | if (!evaluationContext.Properties.TryGetValue(IdentitiesKey, out object identitiesObj)) |
| | | 126 | | { |
| | 28 | 127 | | List<ClaimsIdentity> identities = new List<ClaimsIdentity>(); |
| | 112 | 128 | | foreach (ClaimsIdentity ici in _identityCollection) |
| | | 129 | | { |
| | 28 | 130 | | identities.Add(ici); |
| | | 131 | | } |
| | | 132 | | |
| | 28 | 133 | | evaluationContext.Properties.Add(IdentitiesKey, identities); |
| | | 134 | | } |
| | | 135 | | else |
| | | 136 | | { |
| | | 137 | | List<ClaimsIdentity> identities; |
| | 0 | 138 | | identities = identitiesObj as List<ClaimsIdentity>; |
| | | 139 | | |
| | 0 | 140 | | foreach (ClaimsIdentity ici in _identityCollection) |
| | | 141 | | { |
| | 0 | 142 | | identities.Add(ici); |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | 28 | 146 | | return true; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | private static ClaimsPrincipal CreateClaimsPrincipalFromIdentities(IEnumerable<ClaimsIdentity> identities) |
| | | 150 | | { |
| | 28 | 151 | | ClaimsIdentity selectedClaimsIdentity = SelectPrimaryIdentity(identities); |
| | | 152 | | |
| | 28 | 153 | | if (selectedClaimsIdentity == null) |
| | | 154 | | { |
| | | 155 | | //return an anonymous identity |
| | 0 | 156 | | return new ClaimsPrincipal(new ClaimsIdentity()); |
| | | 157 | | } |
| | | 158 | | |
| | 28 | 159 | | ClaimsPrincipal principal = CreateFromIdentity(selectedClaimsIdentity); |
| | | 160 | | |
| | | 161 | | // Add the remaining identities. |
| | 112 | 162 | | foreach (ClaimsIdentity identity in identities) |
| | | 163 | | { |
| | 28 | 164 | | if (identity != selectedClaimsIdentity) |
| | | 165 | | { |
| | 0 | 166 | | principal.AddIdentity(identity); |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | 28 | 170 | | return principal; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Creates the appropriate implementation of an IClaimsPrincipal base on the |
| | | 175 | | /// type of the specified IIdentity (e.g. WindowsClaimsPrincipal for a WindowsIdentity). |
| | | 176 | | /// Note the appropriate IClaimsIdentity is generated based on the specified IIdentity |
| | | 177 | | /// as well. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="identity">An implementation of IIdentity</param> |
| | | 180 | | /// <returns>A claims-based principal.</returns> |
| | | 181 | | private static ClaimsPrincipal CreateFromIdentity(IIdentity identity) |
| | | 182 | | { |
| | 28 | 183 | | if (null == identity) |
| | | 184 | | { |
| | 0 | 185 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 186 | | } |
| | | 187 | | |
| | 28 | 188 | | if (identity is WindowsIdentity wci) |
| | | 189 | | { |
| | 0 | 190 | | return new WindowsPrincipal(wci); |
| | | 191 | | } |
| | | 192 | | |
| | 28 | 193 | | if (identity is WindowsIdentity wi) |
| | | 194 | | { |
| | 0 | 195 | | return new WindowsPrincipal(wi); |
| | | 196 | | } |
| | | 197 | | |
| | 28 | 198 | | if (identity is ClaimsIdentity ici) |
| | | 199 | | { |
| | 28 | 200 | | return new ClaimsPrincipal(ici); |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | return new ClaimsPrincipal(new ClaimsIdentity(identity)); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// This method iterates through the collection of ClaimsIdentities |
| | | 208 | | /// and determines which identity must be used as the primary one. |
| | | 209 | | /// </summary> |
| | | 210 | | /// <remarks> |
| | | 211 | | /// If the identities collection contains a WindowsClaimsIdentity, it is the most preferred. |
| | | 212 | | /// If the identities collection contains an RsaClaimsIdentity, it is the least preferred. |
| | | 213 | | /// </remarks> |
| | | 214 | | private static ClaimsIdentity SelectPrimaryIdentity(IEnumerable<ClaimsIdentity> identities) |
| | | 215 | | { |
| | 28 | 216 | | if (identities == null) |
| | | 217 | | { |
| | 0 | 218 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identities)); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | // |
| | | 222 | | // Loop through the identities to determine the primary identity. |
| | | 223 | | // |
| | 28 | 224 | | ClaimsIdentity selectedClaimsIdentity = null; |
| | | 225 | | |
| | 112 | 226 | | foreach (ClaimsIdentity identity in identities) |
| | | 227 | | { |
| | 28 | 228 | | if (identity is WindowsIdentity) |
| | | 229 | | { |
| | | 230 | | // |
| | | 231 | | // If there is a WindowsIdentity, return that. |
| | | 232 | | // |
| | 0 | 233 | | selectedClaimsIdentity = identity; |
| | 0 | 234 | | break; |
| | | 235 | | } |
| | 28 | 236 | | else if (identity.FindFirst(ClaimTypes.Rsa) != null) |
| | | 237 | | { |
| | | 238 | | //this is a RSA identity |
| | | 239 | | //it is the least preffered identity |
| | 0 | 240 | | if (selectedClaimsIdentity == null) |
| | | 241 | | { |
| | 0 | 242 | | selectedClaimsIdentity = identity; |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | continue; |
| | | 246 | | } |
| | 28 | 247 | | else if (selectedClaimsIdentity == null) |
| | | 248 | | { |
| | | 249 | | // |
| | | 250 | | // If no primary identity has been selected yet, choose the current identity. |
| | | 251 | | // |
| | 28 | 252 | | selectedClaimsIdentity = identity; |
| | | 253 | | } |
| | | 254 | | } |
| | | 255 | | |
| | 28 | 256 | | return selectedClaimsIdentity; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Gets the Issuer Claimset. This will return a DefaultClaimSet with just one claim |
| | | 262 | | /// whose ClaimType is http://schemas.microsoft.com/claims/identityclaim. |
| | | 263 | | /// </summary> |
| | 28 | 264 | | public SysClaimSet Issuer { get; } = SysClaimSet.System; |
| | | 265 | | |
| | | 266 | | #endregion |
| | | 267 | | |
| | | 268 | | #region IAuthorizationComponent Members |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Returns an Id for the ClaimsPrincipal. |
| | | 272 | | /// </summary> |
| | 28 | 273 | | public string Id { get; } = SecurityUniqueId.Create().Value; |
| | | 274 | | |
| | | 275 | | #endregion |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | } |