| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using CoreWCF.IdentityModel.Tokens; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 9 | | { |
| | | 10 | | public class SecurityTokenRequirement |
| | | 11 | | { |
| | | 12 | | private const string Namespace = "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement |
| | | 13 | | private const string tokenTypeProperty = Namespace + "/TokenType"; |
| | | 14 | | private const string keyUsageProperty = Namespace + "/KeyUsage"; |
| | | 15 | | private const string keyTypeProperty = Namespace + "/KeyType"; |
| | | 16 | | private const string keySizeProperty = Namespace + "/KeySize"; |
| | | 17 | | private const string requireCryptographicTokenProperty = Namespace + "/RequireCryptographicToken"; |
| | | 18 | | private const string peerAuthenticationMode = Namespace + "/PeerAuthenticationMode"; |
| | | 19 | | private const string isOptionalTokenProperty = Namespace + "/IsOptionalTokenProperty"; |
| | | 20 | | private const bool defaultRequireCryptographicToken = false; |
| | | 21 | | private const SecurityKeyUsage defaultKeyUsage = SecurityKeyUsage.Signature; |
| | | 22 | | private const SecurityKeyType defaultKeyType = SecurityKeyType.SymmetricKey; |
| | | 23 | | private const int defaultKeySize = 0; |
| | | 24 | | private const bool defaultIsOptionalToken = false; |
| | | 25 | | private readonly Dictionary<string, object> _properties; |
| | | 26 | | |
| | 99 | 27 | | public SecurityTokenRequirement() |
| | | 28 | | { |
| | 99 | 29 | | _properties = new Dictionary<string, object>(); |
| | 99 | 30 | | Initialize(); |
| | 99 | 31 | | } |
| | | 32 | | |
| | 222 | 33 | | public static string TokenTypeProperty { get { return tokenTypeProperty; } } |
| | 186 | 34 | | public static string KeyUsageProperty { get { return keyUsageProperty; } } |
| | 157 | 35 | | public static string KeyTypeProperty { get { return keyTypeProperty; } } |
| | 99 | 36 | | public static string KeySizeProperty { get { return keySizeProperty; } } |
| | 198 | 37 | | public static string RequireCryptographicTokenProperty { get { return requireCryptographicTokenProperty; } } |
| | 0 | 38 | | public static string PeerAuthenticationMode { get { return peerAuthenticationMode; } } |
| | 99 | 39 | | public static string IsOptionalTokenProperty { get { return isOptionalTokenProperty; } } |
| | | 40 | | |
| | | 41 | | public string TokenType |
| | | 42 | | { |
| | | 43 | | get |
| | | 44 | | { |
| | 123 | 45 | | return (TryGetProperty<string>(TokenTypeProperty, out string result)) ? result : null; |
| | | 46 | | } |
| | | 47 | | set |
| | | 48 | | { |
| | 99 | 49 | | _properties[TokenTypeProperty] = value; |
| | 99 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | internal bool IsOptionalToken |
| | | 54 | | { |
| | | 55 | | get |
| | | 56 | | { |
| | 0 | 57 | | return (TryGetProperty<bool>(IsOptionalTokenProperty, out bool result)) ? result : defaultIsOptionalToke |
| | | 58 | | } |
| | | 59 | | set |
| | | 60 | | { |
| | 99 | 61 | | _properties[IsOptionalTokenProperty] = value; |
| | 99 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public bool RequireCryptographicToken |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 0 | 69 | | return (TryGetProperty<bool>(RequireCryptographicTokenProperty, out bool result)) ? result : defaultRequ |
| | | 70 | | } |
| | | 71 | | set |
| | | 72 | | { |
| | 198 | 73 | | _properties[RequireCryptographicTokenProperty] = (object)value; |
| | 198 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public SecurityKeyUsage KeyUsage |
| | | 78 | | { |
| | | 79 | | get |
| | | 80 | | { |
| | 0 | 81 | | return (TryGetProperty<SecurityKeyUsage>(KeyUsageProperty, out SecurityKeyUsage result)) ? result : defa |
| | | 82 | | } |
| | | 83 | | set |
| | | 84 | | { |
| | 186 | 85 | | SecurityKeyUsageHelper.Validate(value); |
| | 186 | 86 | | _properties[KeyUsageProperty] = (object)value; |
| | 186 | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | internal SecurityKeyType KeyType |
| | | 91 | | { |
| | | 92 | | get |
| | | 93 | | { |
| | 0 | 94 | | return (TryGetProperty<SecurityKeyType>(KeyTypeProperty, out SecurityKeyType result)) ? result : default |
| | | 95 | | } |
| | | 96 | | set |
| | | 97 | | { |
| | 157 | 98 | | SecurityKeyTypeHelper.Validate(value); |
| | 157 | 99 | | _properties[KeyTypeProperty] = (object)value; |
| | 157 | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public int KeySize |
| | | 104 | | { |
| | | 105 | | get |
| | | 106 | | { |
| | 0 | 107 | | return (TryGetProperty<int>(KeySizeProperty, out int result)) ? result : defaultKeySize; |
| | | 108 | | } |
| | | 109 | | set |
| | | 110 | | { |
| | 99 | 111 | | if (value < 0) |
| | | 112 | | { |
| | 0 | 113 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 114 | | } |
| | 99 | 115 | | Properties[KeySizeProperty] = value; |
| | 99 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public IDictionary<string, object> Properties |
| | | 120 | | { |
| | | 121 | | get |
| | | 122 | | { |
| | 1351 | 123 | | return _properties; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private void Initialize() |
| | | 128 | | { |
| | 99 | 129 | | KeyType = defaultKeyType; |
| | 99 | 130 | | KeyUsage = defaultKeyUsage; |
| | 99 | 131 | | RequireCryptographicToken = defaultRequireCryptographicToken; |
| | 99 | 132 | | KeySize = defaultKeySize; |
| | 99 | 133 | | IsOptionalToken = defaultIsOptionalToken; |
| | 99 | 134 | | } |
| | | 135 | | |
| | | 136 | | public TValue GetProperty<TValue>(string propertyName) |
| | | 137 | | { |
| | 69 | 138 | | if (!TryGetProperty<TValue>(propertyName, out TValue result)) |
| | | 139 | | { |
| | 0 | 140 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SecurityTok |
| | | 141 | | } |
| | 69 | 142 | | return result; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public bool TryGetProperty<TValue>(string propertyName, out TValue result) |
| | | 146 | | { |
| | 341 | 147 | | if (!Properties.TryGetValue(propertyName, out object dictionaryValue)) |
| | | 148 | | { |
| | 34 | 149 | | result = default; |
| | 34 | 150 | | return false; |
| | | 151 | | } |
| | 307 | 152 | | if (dictionaryValue != null && !typeof(TValue).IsAssignableFrom(dictionaryValue.GetType())) |
| | | 153 | | { |
| | 0 | 154 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SecurityTok |
| | | 155 | | } |
| | 307 | 156 | | result = (TValue)dictionaryValue; |
| | 307 | 157 | | return true; |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | } |