2009-05-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / EndpointIdentity.cs
1 //
2 // EndpointIdentity.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.IdentityModel.Claims;
31 using System.Security.Cryptography;
32 using System.Security.Cryptography.X509Certificates;
33 using System.Xml;
34
35
36 namespace System.ServiceModel
37 {
38         public abstract class EndpointIdentity
39         {
40                 Claim claim;
41                 IEqualityComparer<Claim> comparer;
42
43                 protected EndpointIdentity ()
44                 {
45                 }
46
47                 public Claim IdentityClaim {
48                         get { return claim; }
49                 }
50
51                 public static EndpointIdentity CreateDnsIdentity (string dnsName)
52                 {
53                         return new DnsEndpointIdentity (dnsName);
54                 }
55
56                 public static EndpointIdentity CreateIdentity (Claim identity)
57                 {
58                         if (identity == null)
59                                 throw new ArgumentNullException ();
60
61                         if (identity.ClaimType == ClaimTypes.Dns)
62                                 return CreateDnsIdentity ((string) identity.Resource);
63                         else if (identity.ClaimType == ClaimTypes.Rsa) {
64                                 if (identity.Resource is string)
65                                         return CreateRsaIdentity ((string) identity.Resource);
66                                 else if (identity.Resource is X509Certificate2)
67                                         return CreateRsaIdentity ((X509Certificate2) identity.Resource);
68                         }
69                         else if (identity.ClaimType == ClaimTypes.Thumbprint)
70                                 return CreateX509CertificateIdentity ((X509Certificate2) identity.Resource);
71                         else if (identity.ClaimType == ClaimTypes.Spn)
72                                 return CreateSpnIdentity ((string) identity.Resource);
73                         else if (identity.ClaimType == ClaimTypes.Upn)
74                                 return CreateSpnIdentity ((string) identity.Resource);
75
76                         throw new NotSupportedException (String.Format ("Claim type '{0}' cannot be used to create an endpoint identity.", identity.ClaimType));
77                 }
78
79                 public static EndpointIdentity CreateRsaIdentity (string publicKey)
80                 {
81                         return new RsaEndpointIdentity (publicKey);
82                 }
83
84                 public static EndpointIdentity CreateRsaIdentity (
85                         X509Certificate2 certificate)
86                 {
87                         return new RsaEndpointIdentity (certificate);
88                 }
89
90                 public static EndpointIdentity CreateSpnIdentity (string spnName)
91                 {
92                         return new SpnEndpointIdentity (spnName);
93                 }
94
95                 public static EndpointIdentity CreateUpnIdentity (string upnName)
96                 {
97                         return new UpnEndpointIdentity (upnName);
98                 }
99
100                 public static EndpointIdentity CreateX509CertificateIdentity (
101                         X509Certificate2 certificate)
102                 {
103                         return new X509CertificateEndpointIdentity (certificate);
104                 }
105
106                 public static EndpointIdentity CreateX509CertificateIdentity (
107                         X509Certificate2 primaryCertificate,
108                         X509Certificate2Collection supportingCertificates)
109                 {
110                         return new X509CertificateEndpointIdentity (primaryCertificate, supportingCertificates);
111                 }
112
113                 public override bool Equals (object other)
114                 {
115                         EndpointIdentity e = other as EndpointIdentity;
116                         return e != null && comparer.Equals (claim, e.claim);
117                 }
118
119                 public override int GetHashCode ()
120                 {
121                         return comparer.GetHashCode (claim);
122                 }
123
124                 public override string ToString ()
125                 {
126                         return String.Concat ("identity(", claim, ")");
127                 }
128
129                 protected void Initialize (Claim claim)
130                 {
131                         Initialize (claim, Claim.DefaultComparer);
132                 }
133
134                 protected void Initialize (Claim claim, IEqualityComparer<Claim> comparer)
135                 {
136                         if (claim == null)
137                                 throw new ArgumentNullException ("claim");
138                         if (comparer == null)
139                                 throw new ArgumentNullException ("comparer");
140                         this.claim = claim;
141                         this.comparer = comparer;
142                 }
143         }
144 }