Merge pull request #2397 from alexanderkyte/debugger_appdomain
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Claims / Claim.cs
1 //
2 // Claim.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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
37 namespace System.IdentityModel.Claims
38 {
39         [DataContract (Namespace="http://schemas.xmlsoap.org/ws/2005/05/identity")]
40         public class Claim
41         {
42                 class ClaimComparer : IEqualityComparer<Claim>
43                 {
44                         public bool Equals (Claim c1, Claim c2)
45                         {
46                                 if (c1 == null)
47                                         return c2 == null;
48                                 else if (c2 == null)
49                                         return false;
50                                 return  c1.ClaimType == c2.ClaimType &&
51                                         c1.Right == c2.Right &&
52                                         c1.Resource != null ? c1.Resource.Equals (c2.Resource) : c2.Resource == null;
53                         }
54
55                         public int GetHashCode (Claim c)
56                         {
57                                 return  c.ClaimType.GetHashCode () << 24 +
58                                         c.Right.GetHashCode () << 16 +
59                                         (c.Resource != null ? c.Resource.GetHashCode () << 8 : 0);
60                         }
61                 }
62
63                 // static members
64                 static readonly ClaimComparer default_comparer = new ClaimComparer ();
65                 static readonly Claim system = new Claim (ClaimTypes.System, "System", Rights.Identity);
66
67                 public static IEqualityComparer<Claim> DefaultComparer {
68                         get { return default_comparer; }
69                 }
70
71                 public static Claim System {
72                         get { return system; }
73                 }
74
75                 public static Claim CreateDnsClaim (string dns)
76                 {
77                         return new Claim (ClaimTypes.Dns, dns, Rights.PossessProperty);
78                 }
79
80                 [MonoTODO]
81                 public static Claim CreateDenyOnlyWindowsSidClaim (SecurityIdentifier identifier)
82                 {
83                         throw new NotImplementedException ();
84                 }
85
86                 public static Claim CreateHashClaim (byte [] hash)
87                 {
88                         return new Claim (ClaimTypes.Hash, hash, Rights.PossessProperty);
89                 }
90
91                 public static Claim CreateMailAddressClaim (
92                         MailAddress mailAddress)
93                 {
94                         return new Claim (ClaimTypes.Email, mailAddress, Rights.PossessProperty);
95                 }
96
97                 public static Claim CreateNameClaim (string name)
98                 {
99                         return new Claim (ClaimTypes.Name, name, Rights.PossessProperty);
100                 }
101
102                 public static Claim CreateRsaClaim (RSA rsa)
103                 {
104                         return new Claim (ClaimTypes.Rsa, rsa, Rights.PossessProperty);
105                 }
106
107                 public static Claim CreateSpnClaim (string spn)
108                 {
109                         return new Claim (ClaimTypes.Spn, spn, Rights.PossessProperty);
110                 }
111
112                 public static Claim CreateThumbprintClaim (byte [] thumbprint)
113                 {
114                         return new Claim (ClaimTypes.Thumbprint, thumbprint, Rights.PossessProperty);
115                 }
116
117                 public static Claim CreateUpnClaim (string upn)
118                 {
119                         return new Claim (ClaimTypes.Upn, upn, Rights.PossessProperty);
120                 }
121
122                 public static Claim CreateUriClaim (Uri uri)
123                 {
124                         return new Claim (ClaimTypes.Uri, uri, Rights.PossessProperty);
125                 }
126
127                 public static Claim CreateWindowsSidClaim (
128                         SecurityIdentifier sid)
129                 {
130                         return new Claim (ClaimTypes.Sid, sid, Rights.PossessProperty);
131                 }
132
133                 public static Claim CreateX500DistinguishedNameClaim (
134                         X500DistinguishedName x500DistinguishedName)
135                 {
136                         return new Claim (ClaimTypes.X500DistinguishedName, x500DistinguishedName, Rights.PossessProperty);
137                 }
138
139                 // since those public properties do not expose attributes,
140                 // here I use them on the fields instead ...
141                 [DataMember (Name = "ClaimType")]
142                 string claim_type;
143                 [DataMember (Name = "Resource")]
144                 object resource;
145                 [DataMember (Name = "Right")]
146                 string right;
147
148                 public Claim (string claimType, object resource,
149                         string right)
150                 {
151                         this.claim_type = claimType;
152                         this.resource = resource;
153                         this.right = right;
154                 }
155
156                 public object Resource {
157                         get { return resource; }
158                 }
159
160                 public string ClaimType {
161                         get { return claim_type; }
162                 }
163
164                 public string Right {
165                         get { return right; }
166                 }
167
168                 public override string ToString ()
169                 {
170                         return String.Concat (Right, ": ", ClaimType);
171                 }
172
173                 public override bool Equals (object obj)
174                 {
175                         Claim c = obj as Claim;
176                         return c != null && DefaultComparer.Equals (this, c);
177                 }
178
179                 public override int GetHashCode ()
180                 {
181                         return DefaultComparer.GetHashCode (this);
182                 }
183         }
184 }