2
0

SecurityKeyIdentifier.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // SecurityKeyIdentifier.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2007 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;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Xml;
  33. using System.IdentityModel.Policy;
  34. namespace System.IdentityModel.Tokens
  35. {
  36. public class SecurityKeyIdentifier
  37. : IEnumerable<SecurityKeyIdentifierClause>, IEnumerable
  38. {
  39. public SecurityKeyIdentifier ()
  40. {
  41. this.list = new List<SecurityKeyIdentifierClause> ();
  42. }
  43. public SecurityKeyIdentifier (params SecurityKeyIdentifierClause [] identifiers)
  44. {
  45. this.list = new List<SecurityKeyIdentifierClause> (identifiers);
  46. }
  47. List<SecurityKeyIdentifierClause> list;
  48. bool is_readonly;
  49. public bool CanCreateKey {
  50. get {
  51. foreach (SecurityKeyIdentifierClause kic in this)
  52. if (kic.CanCreateKey)
  53. return true;
  54. return false;
  55. }
  56. }
  57. public int Count {
  58. get { return list.Count; }
  59. }
  60. public bool IsReadOnly {
  61. get { return is_readonly; }
  62. }
  63. public SecurityKeyIdentifierClause this [int i] {
  64. get { return list [i]; }
  65. }
  66. public void Add (SecurityKeyIdentifierClause item)
  67. {
  68. if (is_readonly)
  69. throw new InvalidOperationException ("This SecurityKeyIdentifier is read-only.");
  70. list.Add (item);
  71. }
  72. public SecurityKey CreateKey ()
  73. {
  74. foreach (SecurityKeyIdentifierClause kic in this)
  75. if (kic.CanCreateKey)
  76. return kic.CreateKey ();
  77. throw new ArgumentException ("This key identifier cannot create a key");
  78. }
  79. public TClause Find<TClause> ()
  80. where TClause : SecurityKeyIdentifierClause
  81. {
  82. TClause kic;
  83. if (!TryFind<TClause> (out kic))
  84. throw new ArgumentException (String.Format ("{0} was not found in this SecurityKeyIdentifier", typeof (TClause)));
  85. return kic;
  86. }
  87. IEnumerator IEnumerable.GetEnumerator ()
  88. {
  89. return GetEnumerator ();
  90. }
  91. public IEnumerator<SecurityKeyIdentifierClause> GetEnumerator ()
  92. {
  93. return list.GetEnumerator ();
  94. }
  95. public void MakeReadOnly ()
  96. {
  97. is_readonly = true;
  98. }
  99. public override string ToString ()
  100. {
  101. if (Count == 0)
  102. return "(no key identifier clause)";
  103. StringBuilder sb = new StringBuilder ();
  104. sb.AppendFormat ("Total keys: {0}, ", Count);
  105. foreach (SecurityKeyIdentifierClause kic in this)
  106. sb.Append ('{').Append (kic.ToString ()).Append ('}');
  107. return sb.ToString ();
  108. }
  109. public bool TryFind<TClause> (out TClause result)
  110. where TClause : SecurityKeyIdentifierClause
  111. {
  112. result = default (TClause);
  113. foreach (SecurityKeyIdentifierClause kic in this)
  114. if (typeof (TClause).IsAssignableFrom (kic.GetType ())) {
  115. result = (TClause) kic;
  116. return true;
  117. }
  118. return false;
  119. }
  120. }
  121. }