2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.IdentityModel / System.IdentityModel.Tokens / SamlAttribute.cs
blob92605125f51083b009f14cd9e9dfd363bb7615a2
1 //
2 // SamlAttribute.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-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.
28 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Xml;
32 using System.IdentityModel.Claims;
33 using System.IdentityModel.Selectors;
35 namespace System.IdentityModel.Tokens
37 public class SamlAttribute
39 bool is_readonly;
40 string name, ns;
41 List<string> attribute_values;
43 public SamlAttribute ()
45 attribute_values = new List<string> ();
48 public SamlAttribute (Claim claim)
50 if (claim == null)
51 throw new ArgumentNullException ("claim");
52 if (claim.ClaimType == null)
53 throw new ArgumentException ("Claim type is null.");
54 int idx = claim.ClaimType.LastIndexOf ('/');
55 if (idx <= 0 || idx == claim.ClaimType.Length - 1)
56 throw new ArgumentException ("Claim type does not contain '/' or it is at improper position.");
57 name = claim.ClaimType.Substring (idx + 1);
58 ns = claim.ClaimType.Substring (0, idx);
60 if (claim.Resource != null && !(claim.Resource is string))
61 throw new ArgumentException ("Claim resource is not a string.");
63 attribute_values = new List<string> ();
64 attribute_values.Add ((string) claim.Resource);
66 if (claim.Right != Rights.PossessProperty)
67 throw new ArgumentException ("Claim right is not PossessProperty");
70 public SamlAttribute (string attributeNamespace,
71 string attributeName,
72 IEnumerable<string> attributeValues)
74 ns = attributeNamespace;
75 name = attributeName;
76 attribute_values = new List<string> (attributeValues);
79 public IList<string> AttributeValues {
80 get { return attribute_values; }
83 public string Name {
84 get { return name; }
85 set {
86 CheckReadOnly ();
87 name = value;
91 public string Namespace {
92 get { return ns; }
93 set {
94 CheckReadOnly ();
95 ns = value;
99 public bool IsReadOnly {
100 get { return is_readonly; }
103 private void CheckReadOnly ()
105 if (is_readonly)
106 throw new InvalidOperationException ("This SAML assertion is read-only.");
109 public void MakeReadOnly ()
111 is_readonly = true;
114 [MonoTODO]
115 public virtual void ReadXml (XmlDictionaryReader reader,
116 SamlSerializer samlSerializer,
117 SecurityTokenSerializer keyInfoTokenSerializer,
118 SecurityTokenResolver outOfBandTokenResolver)
120 throw new NotImplementedException ();
123 public virtual void WriteXml (XmlDictionaryWriter writer,
124 SamlSerializer samlSerializer,
125 SecurityTokenSerializer keyInfoTokenSerializer)
127 writer.WriteStartElement ("saml", "Attribute", SamlConstants.Namespace);
128 writer.WriteAttributeString ("AttributeName", Name);
129 writer.WriteAttributeString ("AttributeNamespace", Namespace);
130 foreach (string s in AttributeValues)
131 writer.WriteElementString ("saml", "AttributeValue", SamlConstants.Namespace, s);
132 writer.WriteEndElement ();
135 [MonoTODO]
136 public virtual ReadOnlyCollection<Claim> ExtractClaims ()
138 throw new NotImplementedException ();