[runtime] Switch getenv to use heap memory
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Tokens / SamlSubject.cs
1 //
2 // SamlSubject.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 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.Collections.Generic;
29 using System.Collections.ObjectModel;
30 using System.Runtime.Serialization;
31 using System.IdentityModel.Claims;
32 using System.IdentityModel.Selectors;
33 using System.Xml;
34
35 namespace System.IdentityModel.Tokens
36 {
37         public class SamlSubject
38         {
39                 public static string NameClaimType {
40                         get { return ClaimTypes.Name; }
41                 }
42
43                 bool is_readonly;
44                 string name_format, name_qualifier, name;
45                 SecurityKey crypto;
46                 SecurityKeyIdentifier key_identifier;
47                 List<string> confirmation_methods;
48                 string confirmation_data;
49
50                 public SamlSubject ()
51                 {
52                 }
53
54                 public SamlSubject (string nameFormat, string nameQualifier, string name)
55                         : this (nameFormat, nameQualifier, name, new string [0], null, null)
56                 {
57                 }
58
59                 public SamlSubject (string nameFormat, string nameQualifier, string name, IEnumerable<string> confirmations, string confirmationData, SecurityKeyIdentifier securityKeyIdentifier)
60                 {
61                         if (name == null || name.Length == 0)
62                                 throw new ArgumentException ("non-zero length string must be specified for name of SAML Subject.");
63                         name_format = nameFormat;
64                         name_qualifier = nameQualifier;
65                         this.name = name;
66
67                         confirmation_methods = new List<string> (confirmations);
68                         confirmation_data = confirmationData;
69                         key_identifier = securityKeyIdentifier;
70                 }
71
72                 public bool IsReadOnly {
73                         get { return is_readonly; }
74                 }
75
76                 public string NameFormat {
77                         get { return name_format; }
78                         set {
79                                 CheckReadOnly ();
80                                 name_format = value;
81                         }
82                 }
83
84                 public string NameQualifier {
85                         get { return name_qualifier; }
86                         set {
87                                 CheckReadOnly ();
88                                 name_qualifier = value;
89                         }
90                 }
91
92                 public string Name {
93                         get { return name; }
94                         set {
95                                 CheckReadOnly ();
96                                 if (value == null || value.Length == 0)
97                                         throw new ArgumentException ("non-zero length string must be specified for name of SAML Subject.");
98                                 name = value;
99                         }
100                 }
101
102                 public IList<string> ConfirmationMethods {
103                         get { return confirmation_methods; }
104                 }
105
106                 public string SubjectConfirmationData {
107                         get { return confirmation_data; }
108                         set {
109                                 CheckReadOnly ();
110                                 confirmation_data = value;
111                         }
112                 }
113
114                 public SecurityKey Crypto {
115                         get { return crypto; }
116                         set {
117                                 CheckReadOnly ();
118                                 crypto = value;
119                         }
120                 }
121
122                 public SecurityKeyIdentifier KeyIdentifier {
123                         get { return key_identifier; }
124                         set {
125                                 CheckReadOnly ();
126                                 key_identifier = value;
127                         }
128                 }
129
130                 private void CheckReadOnly ()
131                 {
132                         if (is_readonly)
133                                 throw new InvalidOperationException ("This SAML subject is read-only.");
134                 }
135
136                 public void MakeReadOnly ()
137                 {
138                         is_readonly = true;
139                 }
140
141                 [MonoTODO]
142                 public virtual ReadOnlyCollection<Claim> ExtractClaims ()
143                 {
144                         throw new NotImplementedException ();
145                 }
146
147                 [MonoTODO]
148                 public virtual ClaimSet ExtractSubjectKeyClaimSet (
149                         SamlSecurityTokenAuthenticator samlAuthenticator)
150                 {
151                         throw new NotImplementedException ();
152                 }
153
154                 public virtual void ReadXml (XmlDictionaryReader reader,
155                         SamlSerializer samlSerializer,
156                         SecurityTokenSerializer keyInfoTokenSerializer,
157                         SecurityTokenResolver outOfBandTokenResolver)
158                 {
159                         if (reader == null)
160                                 throw new ArgumentNullException ("reader");
161                         if (samlSerializer == null)
162                                 throw new ArgumentNullException ("samlSerializer");
163
164                         reader.ReadStartElement ("Subject", SamlConstants.Namespace);
165                         NameFormat = reader.GetAttribute ("Format");
166                         NameQualifier = reader.GetAttribute ("NameQualifier");
167                         Name = reader.ReadElementContentAsString ("NameIdentifier", SamlConstants.Namespace);
168                         reader.ReadEndElement ();
169
170                         if (Name == null || Name.Length == 0)
171                                 throw new SecurityTokenException ("non-zero length string must be exist for Name.");
172                 }
173
174                 public virtual void WriteXml (XmlDictionaryWriter writer,
175                         SamlSerializer samlSerializer,
176                         SecurityTokenSerializer keyInfoTokenSerializer)
177                 {
178                         if (writer == null)
179                                 throw new ArgumentNullException ("writer");
180                         if (samlSerializer == null)
181                                 throw new ArgumentNullException ("samlSerializer");
182
183                         if (Name == null || Name.Length == 0)
184                                 throw new SecurityTokenException ("non-zero length string must be set to Name of SAML Subject before being written.");
185
186                         writer.WriteStartElement ("saml", "Subject", SamlConstants.Namespace);
187                         writer.WriteStartElement ("saml", "NameIdentifier", SamlConstants.Namespace);
188                         writer.WriteAttributeString ("Format", NameFormat);
189                         writer.WriteAttributeString ("NameQualifier", NameQualifier);
190                         writer.WriteString (Name);
191                         writer.WriteEndElement ();
192                         writer.WriteEndElement ();
193                 }
194         }
195 }