| | | 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.Net.Mail; |
| | | 6 | | using System.Security.Claims; |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | using System.Security.Cryptography.X509Certificates; |
| | | 9 | | using System.Security.Principal; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Security.Claims |
| | | 12 | | { |
| | | 13 | | internal static class ClaimsConversionHelper |
| | | 14 | | { |
| | | 15 | | public static ClaimsIdentity CreateClaimsIdentityFromClaimSet(CoreWCF.IdentityModel.Claims.ClaimSet claimset, st |
| | | 16 | | { |
| | 0 | 17 | | if (claimset == null) |
| | | 18 | | { |
| | 0 | 19 | | throw new ArgumentNullException(nameof(claimset)); |
| | | 20 | | } |
| | | 21 | | |
| | 0 | 22 | | string issuer = null; |
| | 0 | 23 | | if (claimset.Issuer == null) |
| | | 24 | | { |
| | 0 | 25 | | issuer = ClaimsIdentity.DefaultIssuer; |
| | | 26 | | } |
| | | 27 | | else |
| | | 28 | | { |
| | 0 | 29 | | foreach (CoreWCF.IdentityModel.Claims.Claim claim in claimset.Issuer.FindClaims(CoreWCF.IdentityModel.Cl |
| | | 30 | | { |
| | 0 | 31 | | if ((claim != null) && (claim.Resource is string)) |
| | | 32 | | { |
| | 0 | 33 | | issuer = claim.Resource as string; |
| | 0 | 34 | | break; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | ClaimsIdentity claimsIdentity = new ClaimsIdentity(authenticationType); |
| | | 40 | | |
| | 0 | 41 | | for (int i = 0; i < claimset.Count; ++i) |
| | | 42 | | { |
| | | 43 | | // |
| | | 44 | | // Only capture possesses property claims |
| | | 45 | | // |
| | 0 | 46 | | if (String.Equals(claimset[i].Right, CoreWCF.IdentityModel.Claims.Rights.PossessProperty, StringComparis |
| | | 47 | | { |
| | 0 | 48 | | claimsIdentity.AddClaim(CreateClaimFromWcfClaim(claimset[i], issuer)); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return claimsIdentity; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public static ClaimsIdentity CreateClaimsIdentityFromClaimSet(CoreWCF.IdentityModel.Claims.ClaimSet claimset) |
| | | 56 | | { |
| | 0 | 57 | | return CreateClaimsIdentityFromClaimSet(claimset, null); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public static System.Security.Claims.Claim CreateClaimFromWcfClaim(CoreWCF.IdentityModel.Claims.Claim wcfClaim) |
| | | 61 | | { |
| | 0 | 62 | | return CreateClaimFromWcfClaim(wcfClaim, null); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public static System.Security.Claims.Claim CreateClaimFromWcfClaim(CoreWCF.IdentityModel.Claims.Claim wcfClaim, |
| | | 66 | | { |
| | 0 | 67 | | string claimType = null; |
| | 0 | 68 | | string value = null; |
| | 0 | 69 | | string valueType = ClaimValueTypes.String; |
| | 0 | 70 | | string originalIssuer = issuer; |
| | 0 | 71 | | string samlNameIdentifierFormat = null; |
| | 0 | 72 | | string samlNameIdentifierNameQualifier = null; |
| | | 73 | | |
| | 0 | 74 | | if (wcfClaim == null) |
| | | 75 | | { |
| | 0 | 76 | | throw new ArgumentNullException(nameof(wcfClaim)); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | if (wcfClaim.Resource == null) |
| | | 80 | | { |
| | 0 | 81 | | throw new InvalidOperationException(); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | if (string.IsNullOrEmpty(issuer)) |
| | | 85 | | { |
| | 0 | 86 | | issuer = ClaimsIdentity.DefaultIssuer; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | if (wcfClaim.Resource is string) |
| | | 90 | | { |
| | 0 | 91 | | AssignClaimFromStringResourceSysClaim(wcfClaim, out claimType, out value); |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | 0 | 95 | | AssignClaimFromSysClaim(wcfClaim, out claimType, out value, out valueType, out samlNameIdentifierFormat, |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | if (value == null) |
| | | 99 | | { |
| | 0 | 100 | | throw new InvalidOperationException(); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | System.Security.Claims.Claim newClaim = new System.Security.Claims.Claim(claimType, value, valueType, issuer |
| | 0 | 104 | | newClaim.Properties[ClaimProperties.SamlNameIdentifierFormat] = samlNameIdentifierFormat; |
| | 0 | 105 | | newClaim.Properties[ClaimProperties.SamlNameIdentifierNameQualifier] = samlNameIdentifierNameQualifier; |
| | 0 | 106 | | return newClaim; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private static void AssignClaimFromStringResourceSysClaim(CoreWCF.IdentityModel.Claims.Claim claim, out string c |
| | | 110 | | { |
| | 0 | 111 | | claimType = claim.ClaimType; |
| | 0 | 112 | | claimValue = (string)claim.Resource; |
| | | 113 | | |
| | 0 | 114 | | if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Sid)) |
| | | 115 | | { |
| | 0 | 116 | | if (claim.Right == CoreWCF.IdentityModel.Claims.Rights.Identity) |
| | | 117 | | { |
| | 0 | 118 | | claimType = ClaimTypes.PrimarySid; |
| | | 119 | | } |
| | | 120 | | else |
| | | 121 | | { |
| | 0 | 122 | | claimType = ClaimTypes.GroupSid; |
| | | 123 | | } |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | private static void AssignClaimFromSysClaim(CoreWCF.IdentityModel.Claims.Claim claim, out string _type, out stri |
| | | 128 | | { |
| | 0 | 129 | | samlNameIdentifierFormat = null; |
| | 0 | 130 | | samlNameIdentifierNameQualifier = null; |
| | 0 | 131 | | _type = null; |
| | 0 | 132 | | _value = null; |
| | 0 | 133 | | _valueType = null; |
| | | 134 | | |
| | 0 | 135 | | if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Sid) && claim.Resource is SecurityIdentifier) |
| | | 136 | | { |
| | 0 | 137 | | if (claim.Right == CoreWCF.IdentityModel.Claims.Rights.Identity) |
| | | 138 | | { |
| | 0 | 139 | | _type = ClaimTypes.PrimarySid; |
| | | 140 | | } |
| | | 141 | | else |
| | | 142 | | { |
| | 0 | 143 | | _type = ClaimTypes.GroupSid; |
| | | 144 | | } |
| | 0 | 145 | | _value = ((SecurityIdentifier)claim.Resource).Value; |
| | | 146 | | } |
| | 0 | 147 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Email) && claim.Resource is MailAddress) |
| | | 148 | | { |
| | 0 | 149 | | _type = claim.ClaimType; |
| | 0 | 150 | | _value = ((MailAddress)claim.Resource).Address; |
| | | 151 | | } |
| | 0 | 152 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Thumbprint) && claim.Resource is byte[]) |
| | | 153 | | { |
| | 0 | 154 | | _type = claim.ClaimType; |
| | 0 | 155 | | _value = Convert.ToBase64String(((byte[])claim.Resource)); |
| | 0 | 156 | | _valueType = ClaimValueTypes.Base64Binary; |
| | | 157 | | } |
| | 0 | 158 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Hash) && claim.Resource is byte[]) |
| | | 159 | | { |
| | 0 | 160 | | _type = claim.ClaimType; |
| | 0 | 161 | | _value = Convert.ToBase64String(((byte[])claim.Resource)); |
| | 0 | 162 | | _valueType = ClaimValueTypes.Base64Binary; |
| | | 163 | | } |
| | 0 | 164 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.X500DistinguishedName) && claim.Resource |
| | | 165 | | { |
| | 0 | 166 | | _type = claim.ClaimType; |
| | 0 | 167 | | _value = ((X500DistinguishedName)claim.Resource).Name; |
| | 0 | 168 | | _valueType = ClaimValueTypes.X500Name; |
| | | 169 | | } |
| | 0 | 170 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Uri) && claim.Resource is Uri) |
| | | 171 | | { |
| | 0 | 172 | | _type = claim.ClaimType; |
| | 0 | 173 | | _value = ((Uri)claim.Resource).ToString(); |
| | | 174 | | } |
| | 0 | 175 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.Rsa) && claim.Resource is RSA) |
| | | 176 | | { |
| | 0 | 177 | | _type = claim.ClaimType; |
| | 0 | 178 | | _value = ((RSA)claim.Resource).ToXmlString(false); |
| | 0 | 179 | | _valueType = ClaimValueTypes.RsaKeyValue; |
| | | 180 | | } |
| | 0 | 181 | | else if (StringComparer.Ordinal.Equals(claim.ClaimType, ClaimTypes.DenyOnlySid) && claim.Resource is Securit |
| | | 182 | | { |
| | 0 | 183 | | _type = claim.ClaimType; |
| | 0 | 184 | | _value = ((SecurityIdentifier)claim.Resource).Value; |
| | | 185 | | } |
| | 0 | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |
| | | 189 | | |