| | | 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.Globalization; |
| | | 7 | | using System.Net.Mail; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using System.Security.Cryptography; |
| | | 10 | | using System.Security.Cryptography.X509Certificates; |
| | | 11 | | using System.Security.Principal; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.IdentityModel.Claims |
| | | 15 | | { |
| | | 16 | | public class Claim |
| | | 17 | | { |
| | | 18 | | private static Claim s_system; |
| | | 19 | | |
| | | 20 | | [DataMember(Name = "ClaimType")] |
| | | 21 | | private readonly string _claimType; |
| | | 22 | | [DataMember(Name = "Resource")] |
| | | 23 | | private readonly object _resource; |
| | | 24 | | [DataMember(Name = "Right")] |
| | | 25 | | private readonly string _right; |
| | | 26 | | private IEqualityComparer<Claim> _comparer; |
| | | 27 | | |
| | 311 | 28 | | private Claim(string claimType, object resource, string right, IEqualityComparer<Claim> comparer) |
| | | 29 | | { |
| | 311 | 30 | | if (claimType == null) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claimType)); |
| | | 33 | | } |
| | | 34 | | |
| | 311 | 35 | | if (claimType.Length <= 0) |
| | | 36 | | { |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(claimType), SR.ArgumentCannotBeEmpty |
| | | 38 | | } |
| | | 39 | | |
| | 311 | 40 | | if (right == null) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(right)); |
| | | 43 | | } |
| | | 44 | | |
| | 311 | 45 | | if (right.Length <= 0) |
| | | 46 | | { |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(right), SR.ArgumentCannotBeEmptyStri |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // String interning is only supported in .Net standard 1.7. Api tool says this is slow? |
| | | 51 | | //this.claimType = StringUtil.OptimizeString(claimType); |
| | 311 | 52 | | _claimType = claimType; |
| | 311 | 53 | | _resource = resource; |
| | | 54 | | //this.right = StringUtil.OptimizeString(right); |
| | 311 | 55 | | _right = right; |
| | 311 | 56 | | _comparer = comparer; |
| | 311 | 57 | | } |
| | | 58 | | |
| | 300 | 59 | | public Claim(string claimType, object resource, string right) : this(claimType, resource, right, null) |
| | | 60 | | { |
| | 300 | 61 | | } |
| | | 62 | | |
| | | 63 | | public static IEqualityComparer<Claim> DefaultComparer |
| | | 64 | | { |
| | | 65 | | get |
| | | 66 | | { |
| | 0 | 67 | | return EqualityComparer<Claim>.Default; |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public static Claim System |
| | | 72 | | { |
| | | 73 | | get |
| | | 74 | | { |
| | 3 | 75 | | if (s_system == null) |
| | | 76 | | { |
| | 3 | 77 | | s_system = new Claim(ClaimTypes.System, XsiConstants.System, Rights.Identity); |
| | | 78 | | } |
| | | 79 | | |
| | 3 | 80 | | return s_system; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public object Resource |
| | | 85 | | { |
| | 22 | 86 | | get { return _resource; } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public string ClaimType |
| | | 90 | | { |
| | 72 | 91 | | get { return _claimType; } |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public string Right |
| | | 95 | | { |
| | 32 | 96 | | get { return _right; } |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | // Turn key claims |
| | | 100 | | public static Claim CreateDnsClaim(string dns) |
| | | 101 | | { |
| | 7 | 102 | | if (dns == null) |
| | | 103 | | { |
| | 0 | 104 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dns)); |
| | | 105 | | } |
| | | 106 | | |
| | 7 | 107 | | return new Claim(ClaimTypes.Dns, dns, Rights.PossessProperty, ClaimComparer.Dns); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | //public static Claim CreateDenyOnlyWindowsSidClaim(SecurityIdentifier sid) |
| | | 111 | | //{ |
| | | 112 | | // if (sid == null) |
| | | 113 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(sid)); |
| | | 114 | | |
| | | 115 | | // return new Claim(ClaimTypes.DenyOnlySid, sid, Rights.PossessProperty); |
| | | 116 | | //} |
| | | 117 | | |
| | | 118 | | public static Claim CreateHashClaim(byte[] hash) |
| | | 119 | | { |
| | 0 | 120 | | if (hash == null) |
| | 0 | 121 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(hash)); |
| | | 122 | | |
| | 0 | 123 | | return new Claim(ClaimTypes.Hash, SecurityUtils.CloneBuffer(hash), Rights.PossessProperty, ClaimComparer.Has |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | public static Claim CreateMailAddressClaim(MailAddress mailAddress) |
| | | 127 | | { |
| | 0 | 128 | | if (mailAddress == null) |
| | | 129 | | { |
| | 0 | 130 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(mailAddress)); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return new Claim(ClaimTypes.Email, mailAddress, Rights.PossessProperty); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public static Claim CreateNameClaim(string name) |
| | | 137 | | { |
| | 14 | 138 | | if (name == null) |
| | | 139 | | { |
| | 0 | 140 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 141 | | } |
| | | 142 | | |
| | 14 | 143 | | return new Claim(ClaimTypes.Name, name, Rights.PossessProperty); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public static Claim CreateRsaClaim(RSA rsa) |
| | | 147 | | { |
| | 2 | 148 | | if (rsa == null) |
| | | 149 | | { |
| | 0 | 150 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rsa)); |
| | | 151 | | } |
| | | 152 | | |
| | 2 | 153 | | return new Claim(ClaimTypes.Rsa, rsa, Rights.PossessProperty, ClaimComparer.Rsa); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public static Claim CreateSpnClaim(string spn) |
| | | 157 | | { |
| | 12 | 158 | | if (spn == null) |
| | | 159 | | { |
| | 0 | 160 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(spn)); |
| | | 161 | | } |
| | | 162 | | |
| | 12 | 163 | | return new Claim(ClaimTypes.Spn, spn, Rights.PossessProperty); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | public static Claim CreateThumbprintClaim(byte[] thumbprint) |
| | | 167 | | { |
| | 0 | 168 | | if (thumbprint == null) |
| | | 169 | | { |
| | 0 | 170 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(thumbprint)); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return new Claim(ClaimTypes.Thumbprint, SecurityUtils.CloneBuffer(thumbprint), Rights.PossessProperty, Claim |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public static Claim CreateUpnClaim(string upn) |
| | | 177 | | { |
| | 0 | 178 | | if (upn == null) |
| | | 179 | | { |
| | 0 | 180 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(upn)); |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | return new Claim(ClaimTypes.Upn, upn, Rights.PossessProperty, ClaimComparer.Upn); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public static Claim CreateUriClaim(Uri uri) |
| | | 187 | | { |
| | 0 | 188 | | if (uri == null) |
| | | 189 | | { |
| | 0 | 190 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri)); |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | return new Claim(ClaimTypes.Uri, uri, Rights.PossessProperty); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | public static Claim CreateWindowsSidClaim(SecurityIdentifier sid) |
| | | 197 | | { |
| | 0 | 198 | | if (sid == null) |
| | | 199 | | { |
| | 0 | 200 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(sid)); |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | return new Claim(ClaimTypes.Sid, sid, Rights.PossessProperty); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public static Claim CreateX500DistinguishedNameClaim(X500DistinguishedName x500DistinguishedName) |
| | | 207 | | { |
| | 2 | 208 | | if (x500DistinguishedName == null) |
| | | 209 | | { |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(x500DistinguishedName)); |
| | | 211 | | } |
| | | 212 | | |
| | 2 | 213 | | return new Claim(ClaimTypes.X500DistinguishedName, x500DistinguishedName, Rights.PossessProperty, ClaimCompa |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | public override bool Equals(object obj) |
| | | 217 | | { |
| | 12 | 218 | | if (_comparer == null) |
| | | 219 | | { |
| | 6 | 220 | | _comparer = ClaimComparer.GetComparer(_claimType); |
| | | 221 | | } |
| | | 222 | | |
| | 12 | 223 | | return _comparer.Equals(this, obj as Claim); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public override int GetHashCode() |
| | | 227 | | { |
| | 0 | 228 | | if (_comparer == null) |
| | | 229 | | { |
| | 0 | 230 | | _comparer = ClaimComparer.GetComparer(_claimType); |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | return _comparer.GetHashCode(this); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public override string ToString() |
| | | 237 | | { |
| | 19 | 238 | | return string.Format(CultureInfo.CurrentCulture, "{0}: {1}", _right, _claimType); |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | } |