SamlAttribute.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // SamlAttribute.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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.
  27. //
  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;
  34. namespace System.IdentityModel.Tokens
  35. {
  36. public class SamlAttribute
  37. {
  38. bool is_readonly;
  39. string name, ns;
  40. List<string> attribute_values;
  41. public SamlAttribute ()
  42. {
  43. attribute_values = new List<string> ();
  44. }
  45. public SamlAttribute (Claim claim)
  46. {
  47. if (claim == null)
  48. throw new ArgumentNullException ("claim");
  49. if (claim.ClaimType == null)
  50. throw new ArgumentException ("Claim type is null.");
  51. int idx = claim.ClaimType.LastIndexOf ('/');
  52. if (idx <= 0 || idx == claim.ClaimType.Length - 1)
  53. throw new ArgumentException ("Claim type does not contain '/' or it is at improper position.");
  54. name = claim.ClaimType.Substring (idx + 1);
  55. ns = claim.ClaimType.Substring (0, idx);
  56. if (claim.Resource != null && !(claim.Resource is string))
  57. throw new ArgumentException ("Claim resource is not a string.");
  58. attribute_values = new List<string> ();
  59. attribute_values.Add ((string) claim.Resource);
  60. if (claim.Right != Rights.PossessProperty)
  61. throw new ArgumentException ("Claim right is not PossessProperty");
  62. }
  63. public SamlAttribute (string attributeNamespace,
  64. string attributeName,
  65. IEnumerable<string> attributeValues)
  66. {
  67. ns = attributeNamespace;
  68. name = attributeName;
  69. attribute_values = new List<string> (attributeValues);
  70. }
  71. public IList<string> AttributeValues {
  72. get { return attribute_values; }
  73. }
  74. public string Name {
  75. get { return name; }
  76. set {
  77. CheckReadOnly ();
  78. name = value;
  79. }
  80. }
  81. public string Namespace {
  82. get { return ns; }
  83. set {
  84. CheckReadOnly ();
  85. ns = value;
  86. }
  87. }
  88. public bool IsReadOnly {
  89. get { return is_readonly; }
  90. }
  91. private void CheckReadOnly ()
  92. {
  93. if (is_readonly)
  94. throw new InvalidOperationException ("This SAML assertion is read-only.");
  95. }
  96. public void MakeReadOnly ()
  97. {
  98. is_readonly = true;
  99. }
  100. [MonoTODO]
  101. public virtual void ReadXml (XmlDictionaryReader reader,
  102. SamlSerializer samlSerializer,
  103. SecurityTokenSerializer keyInfoTokenSerializer,
  104. SecurityTokenResolver outOfBandTokenResolver)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. public virtual void WriteXml (XmlDictionaryWriter writer,
  109. SamlSerializer samlSerializer,
  110. SecurityTokenSerializer keyInfoTokenSerializer)
  111. {
  112. writer.WriteStartElement ("saml", "Attribute", SamlConstants.Namespace);
  113. writer.WriteAttributeString ("AttributeName", Name);
  114. writer.WriteAttributeString ("AttributeNamespace", Namespace);
  115. foreach (string s in AttributeValues)
  116. writer.WriteElementString ("saml", "AttributeValue", SamlConstants.Namespace, s);
  117. writer.WriteEndElement ();
  118. }
  119. [MonoTODO]
  120. public virtual ReadOnlyCollection<Claim> ExtractClaims ()
  121. {
  122. throw new NotImplementedException ();
  123. }
  124. }
  125. }