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