SamlAuthenticationStatement.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // SamlAuthenticationStatement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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.Globalization;
  31. using System.Xml;
  32. using System.IdentityModel.Claims;
  33. using System.IdentityModel.Selectors;
  34. namespace System.IdentityModel.Tokens
  35. {
  36. public class SamlAuthenticationStatement : SamlSubjectStatement
  37. {
  38. public static string ClaimType {
  39. get { return "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/authentication"; }
  40. }
  41. bool is_readonly;
  42. string auth_method = "urn:oasis:names:tc:SAML:1.0:am:unspecified";
  43. string dns, ip;
  44. new IList<SamlAuthorityBinding> bindings;
  45. DateTime instant;
  46. public SamlAuthenticationStatement ()
  47. {
  48. bindings = new List<SamlAuthorityBinding> ();
  49. }
  50. public SamlAuthenticationStatement (SamlSubject samlSubject,
  51. string authenticationMethod,
  52. DateTime authenticationInstant,
  53. string dnsAddress, string ipAddress,
  54. IEnumerable<SamlAuthorityBinding> authorityBindings)
  55. : base (samlSubject)
  56. {
  57. AuthenticationMethod = authenticationMethod;
  58. instant = authenticationInstant;
  59. dns = dnsAddress;
  60. ip = ipAddress;
  61. if (authorityBindings != null)
  62. bindings = new List<SamlAuthorityBinding> (authorityBindings);
  63. else
  64. bindings = new List<SamlAuthorityBinding> ();
  65. }
  66. public DateTime AuthenticationInstant {
  67. get { return instant; }
  68. set {
  69. CheckReadOnly ();
  70. instant = value;
  71. }
  72. }
  73. public string AuthenticationMethod {
  74. get { return auth_method; }
  75. set {
  76. CheckReadOnly ();
  77. if (value == null || value.Length == 0)
  78. throw new ArgumentException ("Authentication method must be non-zero length string.");
  79. auth_method = value;
  80. }
  81. }
  82. public IList<SamlAuthorityBinding> AuthorityBindings {
  83. get { return bindings; }
  84. }
  85. public string DnsAddress {
  86. get { return dns; }
  87. set {
  88. CheckReadOnly ();
  89. dns = value;
  90. }
  91. }
  92. public string IPAddress {
  93. get { return ip; }
  94. set {
  95. CheckReadOnly ();
  96. ip= value;
  97. }
  98. }
  99. public override bool IsReadOnly {
  100. get { return is_readonly; }
  101. }
  102. private void CheckReadOnly ()
  103. {
  104. if (is_readonly)
  105. throw new InvalidOperationException ("This SAML assertion is read-only.");
  106. }
  107. public override void MakeReadOnly ()
  108. {
  109. is_readonly = true;
  110. }
  111. [MonoTODO]
  112. public override void ReadXml (XmlDictionaryReader reader,
  113. SamlSerializer samlSerializer,
  114. SecurityTokenSerializer keyInfoTokenSerializer,
  115. SecurityTokenResolver outOfBandTokenResolver)
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. public override void WriteXml (XmlDictionaryWriter writer,
  120. SamlSerializer samlSerializer,
  121. SecurityTokenSerializer keyInfoTokenSerializer)
  122. {
  123. if (writer == null)
  124. throw new ArgumentNullException ("writer");
  125. if (samlSerializer == null)
  126. throw new ArgumentNullException ("samlSerializer");
  127. if (SamlSubject == null)
  128. throw new SecurityTokenException ("SAML Subject must be set to AuthenticationStatement before it is written.");
  129. writer.WriteStartElement ("saml", "AuthenticationStatement", SamlConstants.Namespace);
  130. writer.WriteAttributeString ("AuthenticationMethod", AuthenticationMethod);
  131. writer.WriteAttributeString ("AuthenticationInstant",
  132. AuthenticationInstant.ToString (SamlConstants.DateFormat, CultureInfo.InvariantCulture));
  133. SamlSubject.WriteXml (writer, samlSerializer, keyInfoTokenSerializer);
  134. if (DnsAddress != null || IPAddress != null) {
  135. writer.WriteStartElement ("saml", "SubjectLocality", SamlConstants.Namespace);
  136. if (IPAddress != null)
  137. writer.WriteAttributeString ("IPAddress", IPAddress);
  138. if (DnsAddress != null)
  139. writer.WriteAttributeString ("DNSAddress", DnsAddress);
  140. writer.WriteEndElement ();
  141. }
  142. writer.WriteEndElement ();
  143. }
  144. [MonoTODO]
  145. protected override void AddClaimsToList (IList<Claim> claims)
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. }
  150. }