Claim.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Claim.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net.Mail;
  32. using System.Runtime.Serialization;
  33. using System.Security.Cryptography;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Security.Principal;
  36. namespace System.IdentityModel.Claims
  37. {
  38. [DataContract (Namespace="http://schemas.xmlsoap.org/ws/2005/05/identity")]
  39. public class Claim
  40. {
  41. class ClaimComparer : IEqualityComparer<Claim>
  42. {
  43. public bool Equals (Claim c1, Claim c2)
  44. {
  45. if (c1 == null)
  46. return c2 == null;
  47. else if (c2 == null)
  48. return false;
  49. return c1.ClaimType == c2.ClaimType &&
  50. c1.Right == c2.Right &&
  51. c1.Resource != null ? c1.Resource.Equals (c2.Resource) : c2.Resource == null;
  52. }
  53. public int GetHashCode (Claim c)
  54. {
  55. return c.ClaimType.GetHashCode () << 24 +
  56. c.Right.GetHashCode () << 16 +
  57. (c.Resource != null ? c.Resource.GetHashCode () << 8 : 0);
  58. }
  59. }
  60. // static members
  61. static readonly ClaimComparer default_comparer = new ClaimComparer ();
  62. static readonly Claim system = new Claim (ClaimTypes.System, "System", Rights.Identity);
  63. public static IEqualityComparer<Claim> DefaultComparer {
  64. get { return default_comparer; }
  65. }
  66. public static Claim System {
  67. get { return system; }
  68. }
  69. public static Claim CreateDnsClaim (string dns)
  70. {
  71. return new Claim (ClaimTypes.Dns, dns, Rights.PossessProperty);
  72. }
  73. [MonoTODO]
  74. public static Claim CreateDenyOnlyWindowsSidClaim (SecurityIdentifier identifier)
  75. {
  76. throw new NotImplementedException ();
  77. }
  78. public static Claim CreateHashClaim (byte [] hash)
  79. {
  80. return new Claim (ClaimTypes.Hash, hash, Rights.PossessProperty);
  81. }
  82. public static Claim CreateMailAddressClaim (
  83. MailAddress mailAddress)
  84. {
  85. return new Claim (ClaimTypes.Email, mailAddress, Rights.PossessProperty);
  86. }
  87. public static Claim CreateNameClaim (string name)
  88. {
  89. return new Claim (ClaimTypes.Name, name, Rights.PossessProperty);
  90. }
  91. public static Claim CreateRsaClaim (RSA rsa)
  92. {
  93. return new Claim (ClaimTypes.Rsa, rsa, Rights.PossessProperty);
  94. }
  95. public static Claim CreateSpnClaim (string spn)
  96. {
  97. return new Claim (ClaimTypes.Spn, spn, Rights.PossessProperty);
  98. }
  99. public static Claim CreateThumbprintClaim (byte [] thumbprint)
  100. {
  101. return new Claim (ClaimTypes.Thumbprint, thumbprint, Rights.PossessProperty);
  102. }
  103. public static Claim CreateUpnClaim (string upn)
  104. {
  105. return new Claim (ClaimTypes.Upn, upn, Rights.PossessProperty);
  106. }
  107. public static Claim CreateUriClaim (Uri uri)
  108. {
  109. return new Claim (ClaimTypes.Uri, uri, Rights.PossessProperty);
  110. }
  111. public static Claim CreateWindowsSidClaim (
  112. SecurityIdentifier sid)
  113. {
  114. return new Claim (ClaimTypes.Sid, sid, Rights.PossessProperty);
  115. }
  116. public static Claim CreateX500DistinguishedNameClaim (
  117. X500DistinguishedName x500DistinguishedName)
  118. {
  119. return new Claim (ClaimTypes.X500DistinguishedName, x500DistinguishedName, Rights.PossessProperty);
  120. }
  121. // since those public properties do not expose attributes,
  122. // here I use them on the fields instead ...
  123. [DataMember (Name = "ClaimType")]
  124. string claim_type;
  125. [DataMember (Name = "Resource")]
  126. object resource;
  127. [DataMember (Name = "Right")]
  128. string right;
  129. public Claim (string claimType, object resource,
  130. string right)
  131. {
  132. this.claim_type = claimType;
  133. this.resource = resource;
  134. this.right = right;
  135. }
  136. public object Resource {
  137. get { return resource; }
  138. }
  139. public string ClaimType {
  140. get { return claim_type; }
  141. }
  142. public string Right {
  143. get { return right; }
  144. }
  145. public override string ToString ()
  146. {
  147. return String.Concat (Right, ": ", ClaimType);
  148. }
  149. public override bool Equals (object obj)
  150. {
  151. Claim c = obj as Claim;
  152. return c != null && DefaultComparer.Equals (this, c);
  153. }
  154. public override int GetHashCode ()
  155. {
  156. return DefaultComparer.GetHashCode (this);
  157. }
  158. }
  159. }