SecurityTokenRequirement.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // SecurityTokenRequirement.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;
  29. using System.Collections.Generic;
  30. using System.IdentityModel.Tokens;
  31. namespace System.IdentityModel.Selectors
  32. {
  33. public class SecurityTokenRequirement
  34. {
  35. // huh, why not const?
  36. public static string KeySizeProperty {
  37. get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeySize"; }
  38. }
  39. public static string KeyTypeProperty {
  40. get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeyType"; }
  41. }
  42. public static string KeyUsageProperty {
  43. get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeyUsage"; }
  44. }
  45. public static string RequireCryptographicTokenProperty {
  46. get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/RequireCryptographicToken"; }
  47. }
  48. public static string TokenTypeProperty {
  49. get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/TokenType"; }
  50. }
  51. // Instance members
  52. public SecurityTokenRequirement ()
  53. {
  54. }
  55. Dictionary<string,object> properties;
  56. public int KeySize {
  57. get {
  58. int ret;
  59. if (TryGetProperty<int> (KeySizeProperty, out ret))
  60. return ret;
  61. return default (int);
  62. }
  63. set { Properties [KeySizeProperty] = value; }
  64. }
  65. public SecurityKeyType KeyType {
  66. get {
  67. SecurityKeyType ret;
  68. if (TryGetProperty<SecurityKeyType> (KeyTypeProperty, out ret))
  69. return ret;
  70. return default (SecurityKeyType);
  71. }
  72. set { Properties [KeyTypeProperty] = value; }
  73. }
  74. public string TokenType {
  75. get {
  76. string ret;
  77. if (TryGetProperty<string> (TokenTypeProperty, out ret))
  78. return ret;
  79. return default (string);
  80. }
  81. set { Properties [TokenTypeProperty] = value; }
  82. }
  83. public SecurityKeyUsage KeyUsage {
  84. get {
  85. SecurityKeyUsage ret;
  86. if (TryGetProperty<SecurityKeyUsage> (KeyUsageProperty, out ret))
  87. return ret;
  88. return SecurityKeyUsage.Signature;// not default!!
  89. }
  90. set { Properties [KeyUsageProperty] = value; }
  91. }
  92. public bool RequireCryptographicToken {
  93. get {
  94. bool ret;
  95. if (TryGetProperty<bool> (RequireCryptographicTokenProperty, out ret))
  96. return ret;
  97. return default (bool);
  98. }
  99. set { Properties [RequireCryptographicTokenProperty] = value; }
  100. }
  101. public IDictionary<string,object> Properties {
  102. get {
  103. if (properties == null) {
  104. properties = new Dictionary<string,object> ();
  105. properties [KeyTypeProperty] = SecurityKeyType.SymmetricKey;
  106. properties [KeySizeProperty] = 0;
  107. properties [RequireCryptographicTokenProperty] = false;
  108. }
  109. return properties;
  110. }
  111. }
  112. public TValue GetProperty<TValue> (string property)
  113. {
  114. TValue ret;
  115. if (TryGetProperty<TValue> (property, out ret))
  116. return ret;
  117. throw new ArgumentException (String.Format ("Property '{0}' was not found.", property));
  118. }
  119. public bool TryGetProperty<TValue> (string property, out TValue value)
  120. {
  121. object tmp;
  122. value = default (TValue);
  123. if (!Properties.TryGetValue (property, out tmp))
  124. return false;
  125. if (tmp == null && !typeof (TValue).IsValueType)
  126. value = default (TValue);
  127. else if (tmp is TValue)
  128. value = (TValue) tmp;
  129. else
  130. throw new ArgumentException (String.Format ("The value of property '{0}' is of type '{1}', while '{2}' is expected.", property, tmp.GetType (), typeof (TValue)));
  131. return true;
  132. }
  133. }
  134. }