5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
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;
37 namespace System.IdentityModel.Claims
39 [DataContract (Namespace="http://schemas.xmlsoap.org/ws/2005/05/identity")]
42 class ClaimComparer : IEqualityComparer<Claim>
44 public bool Equals (Claim c1, Claim c2)
50 return c1.ClaimType == c2.ClaimType &&
51 c1.Right == c2.Right &&
52 c1.Resource != null ? c1.Resource.Equals (c2.Resource) : c2.Resource == null;
55 public int GetHashCode (Claim c)
57 return c.ClaimType.GetHashCode () << 24 +
58 c.Right.GetHashCode () << 16 +
59 (c.Resource != null ? c.Resource.GetHashCode () << 8 : 0);
64 static readonly ClaimComparer default_comparer = new ClaimComparer ();
65 static readonly Claim system = new Claim (ClaimTypes.System, "System", Rights.Identity);
67 public static IEqualityComparer<Claim> DefaultComparer {
68 get { return default_comparer; }
71 public static Claim System {
72 get { return system; }
75 public static Claim CreateDnsClaim (string dns)
77 return new Claim (ClaimTypes.Dns, dns, Rights.PossessProperty);
81 public static Claim CreateDenyOnlyWindowsSidClaim (SecurityIdentifier sid)
83 throw new NotImplementedException ();
86 public static Claim CreateHashClaim (byte [] hash)
88 return new Claim (ClaimTypes.Hash, hash, Rights.PossessProperty);
91 public static Claim CreateMailAddressClaim (
92 MailAddress mailAddress)
94 return new Claim (ClaimTypes.Email, mailAddress, Rights.PossessProperty);
97 public static Claim CreateNameClaim (string name)
99 return new Claim (ClaimTypes.Name, name, Rights.PossessProperty);
102 public static Claim CreateRsaClaim (RSA rsa)
104 return new Claim (ClaimTypes.Rsa, rsa, Rights.PossessProperty);
107 public static Claim CreateSpnClaim (string spn)
109 return new Claim (ClaimTypes.Spn, spn, Rights.PossessProperty);
112 public static Claim CreateThumbprintClaim (byte [] thumbprint)
114 return new Claim (ClaimTypes.Thumbprint, thumbprint, Rights.PossessProperty);
117 public static Claim CreateUpnClaim (string upn)
119 return new Claim (ClaimTypes.Upn, upn, Rights.PossessProperty);
122 public static Claim CreateUriClaim (Uri uri)
124 return new Claim (ClaimTypes.Uri, uri, Rights.PossessProperty);
127 public static Claim CreateWindowsSidClaim (
128 SecurityIdentifier sid)
130 return new Claim (ClaimTypes.Sid, sid, Rights.PossessProperty);
133 public static Claim CreateX500DistinguishedNameClaim (
134 X500DistinguishedName x500DistinguishedName)
136 return new Claim (ClaimTypes.X500DistinguishedName, x500DistinguishedName, Rights.PossessProperty);
139 // since those public properties do not expose attributes,
140 // here I use them on the fields instead ...
141 [DataMember (Name = "ClaimType")]
143 [DataMember (Name = "Resource")]
145 [DataMember (Name = "Right")]
148 public Claim (string claimType, object resource,
151 this.claim_type = claimType;
152 this.resource = resource;
156 public object Resource {
157 get { return resource; }
160 public string ClaimType {
161 get { return claim_type; }
164 public string Right {
165 get { return right; }
168 public override string ToString ()
170 return String.Concat (Right, ": ", ClaimType);
173 public override bool Equals (object obj)
175 Claim c = obj as Claim;
176 return c != null && DefaultComparer.Equals (this, c);
179 public override int GetHashCode ()
181 return DefaultComparer.GetHashCode (this);