SamlAuthorizationDecisionStatement.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // SamlAuthorizationDecisionStatement.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.Xml;
  31. using System.IdentityModel.Claims;
  32. using System.IdentityModel.Policy;
  33. using System.IdentityModel.Selectors;
  34. namespace System.IdentityModel.Tokens
  35. {
  36. public class SamlAuthorizationDecisionStatement : SamlSubjectStatement
  37. {
  38. public static string ClaimType {
  39. get { return "http://schemas.microsoft.com/mb/2005/09/ClaimType/SamlAuthorizationDecision"; }
  40. }
  41. public SamlAuthorizationDecisionStatement ()
  42. {
  43. }
  44. public SamlAuthorizationDecisionStatement (
  45. SamlSubject samlSubject, string resource,
  46. SamlAccessDecision accessDecision,
  47. IEnumerable<SamlAction> samlActions)
  48. : base (samlSubject)
  49. {
  50. if (samlActions == null)
  51. throw new ArgumentNullException ("samlActions");
  52. if (resource == null || resource.Length == 0)
  53. throw new SecurityTokenException ("non-zero length string must be set to Resource of SAML AuthorizationDecisionStatement.");
  54. Resource = resource;
  55. AccessDecision = accessDecision;
  56. foreach (SamlAction a in samlActions) {
  57. if (a == null)
  58. throw new ArgumentException ("samlActions contain null item.");
  59. actions.Add (a);
  60. }
  61. }
  62. public SamlAuthorizationDecisionStatement (
  63. SamlSubject samlSubject, string resource,
  64. SamlAccessDecision accessDecision,
  65. IEnumerable<SamlAction> samlActions,
  66. SamlEvidence samlEvidence)
  67. : this (samlSubject, resource, accessDecision, samlActions)
  68. {
  69. evidence = samlEvidence;
  70. }
  71. SamlAccessDecision access_decision;
  72. SamlEvidence evidence;
  73. string resource;
  74. List<SamlAction> actions = new List<SamlAction> ();
  75. public IList<SamlAction> SamlActions {
  76. get { return actions; }
  77. }
  78. public SamlAccessDecision AccessDecision {
  79. get { return access_decision; }
  80. set {
  81. CheckReadOnly ();
  82. access_decision = value;
  83. }
  84. }
  85. public SamlEvidence Evidence {
  86. get { return evidence; }
  87. set {
  88. CheckReadOnly ();
  89. evidence = value;
  90. }
  91. }
  92. public string Resource {
  93. get { return resource; }
  94. set {
  95. CheckReadOnly ();
  96. if (value == null || value.Length == 0)
  97. throw new ArgumentException ("non-zero length string must be set to Resource of SAML AuthorizationDecisionStatement.");
  98. resource = value;
  99. }
  100. }
  101. public override bool IsReadOnly {
  102. get { return base.IsReadOnly; }
  103. }
  104. private void CheckReadOnly ()
  105. {
  106. if (IsReadOnly)
  107. throw new InvalidOperationException ("This SAML assertion is read-only.");
  108. }
  109. public override void MakeReadOnly ()
  110. {
  111. base.MakeReadOnly ();
  112. }
  113. [MonoTODO]
  114. protected override void AddClaimsToList (IList<Claim> claims)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. public override void ReadXml (XmlDictionaryReader reader,
  119. SamlSerializer samlSerializer,
  120. SecurityTokenSerializer keyInfoSerializer,
  121. SecurityTokenResolver resolver)
  122. {
  123. if (reader == null)
  124. throw new ArgumentNullException ("reader");
  125. if (samlSerializer == null)
  126. throw new ArgumentNullException ("samlSerializer");
  127. string decision = reader.GetAttribute ("Decision");
  128. switch (decision) {
  129. case "Permit":
  130. AccessDecision = SamlAccessDecision.Permit;
  131. break;
  132. case "Deny":
  133. AccessDecision = SamlAccessDecision.Deny;
  134. break;
  135. case "Indeterminate":
  136. AccessDecision = SamlAccessDecision.Indeterminate;
  137. break;
  138. default:
  139. throw new SecurityTokenException (String.Format ("AccessDecision value is wrong: {0}", decision));
  140. }
  141. Resource = reader.GetAttribute ("Resource");
  142. reader.ReadStartElement ("AuthorizationDecisionStatement", SamlConstants.Namespace);
  143. reader.MoveToContent ();
  144. SamlSubject = new SamlSubject ();
  145. SamlSubject.ReadXml (reader, samlSerializer, keyInfoSerializer, resolver);
  146. SamlActions.Clear ();
  147. for (reader.MoveToContent ();
  148. reader.LocalName == "Action" &&
  149. reader.NamespaceURI == SamlConstants.Namespace;
  150. reader.MoveToContent ()) {
  151. SamlAction action = new SamlAction ();
  152. action.ReadXml (reader, samlSerializer, keyInfoSerializer, resolver);
  153. SamlActions.Add (action);
  154. }
  155. if (reader.LocalName == "Evidence" &&
  156. reader.NamespaceURI == SamlConstants.Namespace) {
  157. Evidence = new SamlEvidence ();
  158. Evidence.ReadXml (reader, samlSerializer, keyInfoSerializer, resolver);
  159. reader.MoveToContent ();
  160. }
  161. reader.ReadEndElement ();
  162. // verify contents
  163. if (SamlActions.Count == 0)
  164. throw new SecurityTokenException ("SAML AuthorizationDecisionStatement must contain at least one Action.");
  165. if (SamlSubject == null)
  166. throw new SecurityTokenException ("SAML Subject must be set to SAML AuthorizationDecisionStatement before being written.");
  167. if (Resource == null || Resource.Length == 0)
  168. throw new SecurityTokenException ("non-zero string must be set to Resource on SAML AuthorizationDecisionStatement.");
  169. }
  170. public override void WriteXml (XmlDictionaryWriter writer,
  171. SamlSerializer samlSerializer,
  172. SecurityTokenSerializer keyInfoSerializer)
  173. {
  174. if (writer == null)
  175. throw new ArgumentNullException ("writer");
  176. if (samlSerializer == null)
  177. throw new ArgumentNullException ("samlSerializer");
  178. if (SamlActions.Count == 0)
  179. throw new SecurityTokenException ("SAML AuthorizationDecisionStatement must contain at least one Action.");
  180. if (SamlSubject == null)
  181. throw new SecurityTokenException ("SAML Subject must be set to SAML AuthorizationDecisionStatement before being written.");
  182. if (Resource == null || Resource.Length == 0)
  183. throw new SecurityTokenException ("non-zero string must be set to Resource on SAML AuthorizationDecisionStatement.");
  184. writer.WriteStartElement ("saml", "AuthorizationDecisionStatement", SamlConstants.Namespace);
  185. writer.WriteStartAttribute ("Decision");
  186. switch (AccessDecision) {
  187. case SamlAccessDecision.Permit:
  188. writer.WriteString ("Permit");
  189. break;
  190. case SamlAccessDecision.Deny:
  191. writer.WriteString ("Deny");
  192. break;
  193. case SamlAccessDecision.Indeterminate:
  194. writer.WriteString ("Indeterminate");
  195. break;
  196. default:
  197. throw new ArgumentOutOfRangeException ("AccessDecision value is wrong.");
  198. }
  199. writer.WriteEndAttribute ();
  200. writer.WriteAttributeString ("Resource", Resource);
  201. SamlSubject.WriteXml (writer, samlSerializer, keyInfoSerializer);
  202. foreach (SamlAction action in SamlActions)
  203. action.WriteXml (writer, samlSerializer, keyInfoSerializer);
  204. if (Evidence != null)
  205. Evidence.WriteXml (writer, samlSerializer, keyInfoSerializer);
  206. writer.WriteEndElement ();
  207. }
  208. }
  209. }