SamlSubject.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // SamlSubject.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. namespace System.IdentityModel.Tokens
  35. {
  36. public class SamlSubject
  37. {
  38. public static string NameClaimType {
  39. get { return ClaimTypes.Name; }
  40. }
  41. bool is_readonly;
  42. string name_format, name_qualifier, name;
  43. SecurityKey crypto;
  44. SecurityKeyIdentifier key_identifier;
  45. List<string> confirmation_methods = new List<string> ();
  46. string confirmation_data;
  47. public SamlSubject ()
  48. {
  49. }
  50. public SamlSubject (string nameFormat, string nameQualifier, string name)
  51. {
  52. if (name == null || name.Length == 0)
  53. throw new ArgumentException ("non-zero length string must be specified for name of SAML Subject.");
  54. name_format = nameFormat;
  55. name_qualifier = nameQualifier;
  56. this.name = name;
  57. }
  58. public bool IsReadOnly {
  59. get { return is_readonly; }
  60. }
  61. public string NameFormat {
  62. get { return name_format; }
  63. set {
  64. CheckReadOnly ();
  65. name_format = value;
  66. }
  67. }
  68. public string NameQualifier {
  69. get { return name_qualifier; }
  70. set {
  71. CheckReadOnly ();
  72. name_qualifier = value;
  73. }
  74. }
  75. public string Name {
  76. get { return name; }
  77. set {
  78. CheckReadOnly ();
  79. if (value == null || value.Length == 0)
  80. throw new ArgumentException ("non-zero length string must be specified for name of SAML Subject.");
  81. name = value;
  82. }
  83. }
  84. public IList<string> ConfirmationMethods {
  85. get { return confirmation_methods; }
  86. }
  87. public string SubjectConfirmationData {
  88. get { return confirmation_data; }
  89. set {
  90. CheckReadOnly ();
  91. confirmation_data = value;
  92. }
  93. }
  94. public SecurityKey Crypto {
  95. get { return crypto; }
  96. set {
  97. CheckReadOnly ();
  98. crypto = value;
  99. }
  100. }
  101. public SecurityKeyIdentifier KeyIdentifier {
  102. get { return key_identifier; }
  103. set {
  104. CheckReadOnly ();
  105. key_identifier = value;
  106. }
  107. }
  108. private void CheckReadOnly ()
  109. {
  110. if (is_readonly)
  111. throw new InvalidOperationException ("This SAML subject is read-only.");
  112. }
  113. public void MakeReadOnly ()
  114. {
  115. is_readonly = true;
  116. }
  117. [MonoTODO]
  118. public virtual ReadOnlyCollection<Claim> ExtractClaims ()
  119. {
  120. throw new NotImplementedException ();
  121. }
  122. [MonoTODO]
  123. public virtual ClaimSet ExtractSubjectKeyClaimSet (
  124. SamlSecurityTokenAuthenticator samlAuthenticator)
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. public virtual void ReadXml (XmlDictionaryReader reader,
  129. SamlSerializer samlSerializer,
  130. SecurityTokenSerializer keyInfoTokenSerializer,
  131. SecurityTokenResolver outOfBandTokenResolver)
  132. {
  133. if (reader == null)
  134. throw new ArgumentNullException ("reader");
  135. if (samlSerializer == null)
  136. throw new ArgumentNullException ("samlSerializer");
  137. reader.ReadStartElement ("Subject", SamlConstants.Namespace);
  138. NameFormat = reader.GetAttribute ("Format");
  139. NameQualifier = reader.GetAttribute ("NameQualifier");
  140. Name = reader.ReadElementContentAsString ("NameIdentifier", SamlConstants.Namespace);
  141. reader.ReadEndElement ();
  142. if (Name == null || Name.Length == 0)
  143. throw new SecurityTokenException ("non-zero length string must be exist for Name.");
  144. }
  145. public virtual void WriteXml (XmlDictionaryWriter writer,
  146. SamlSerializer samlSerializer,
  147. SecurityTokenSerializer keyInfoTokenSerializer)
  148. {
  149. if (writer == null)
  150. throw new ArgumentNullException ("writer");
  151. if (samlSerializer == null)
  152. throw new ArgumentNullException ("samlSerializer");
  153. if (Name == null || Name.Length == 0)
  154. throw new SecurityTokenException ("non-zero length string must be set to Name of SAML Subject before being written.");
  155. writer.WriteStartElement ("saml", "Subject", SamlConstants.Namespace);
  156. writer.WriteStartElement ("saml", "NameIdentifier", SamlConstants.Namespace);
  157. writer.WriteAttributeString ("Format", NameFormat);
  158. writer.WriteAttributeString ("NameQualifier", NameQualifier);
  159. writer.WriteString (Name);
  160. writer.WriteEndElement ();
  161. writer.WriteEndElement ();
  162. }
  163. }
  164. }