File: IssuedTokenParametersElement.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,284,488 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (325 lines) | stat: -rw-r--r-- 14,200 bytes parent folder | download | duplicates (9)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------------------------

namespace System.ServiceModel.Configuration
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Configuration;
    using System.IdentityModel.Tokens;
    using System.IO;
    using System.Runtime;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Security.Tokens;
    using System.Text;
    using System.Xml;

    public sealed partial class IssuedTokenParametersElement : ServiceModelConfigurationElement
    {
        Collection<IssuedTokenParametersElement> optionalIssuedTokenParameters = null;

        public IssuedTokenParametersElement()
        {
        }

        [ConfigurationProperty(ConfigurationStrings.DefaultMessageSecurityVersion)]
        [TypeConverter(typeof(MessageSecurityVersionConverter))]
        public MessageSecurityVersion DefaultMessageSecurityVersion
        {
            get { return (MessageSecurityVersion)base[ConfigurationStrings.DefaultMessageSecurityVersion]; }
            set { base[ConfigurationStrings.DefaultMessageSecurityVersion] = value; }
        }

        [ConfigurationProperty(ConfigurationStrings.AdditionalRequestParameters)]
        public XmlElementElementCollection AdditionalRequestParameters
        {
            get { return (XmlElementElementCollection)base[ConfigurationStrings.AdditionalRequestParameters]; }
        }

        [ConfigurationProperty(ConfigurationStrings.ClaimTypeRequirements)]
        public ClaimTypeElementCollection ClaimTypeRequirements
        {
            get { return (ClaimTypeElementCollection)base[ConfigurationStrings.ClaimTypeRequirements]; }
        }

        [ConfigurationProperty(ConfigurationStrings.Issuer)]
        public IssuedTokenParametersEndpointAddressElement Issuer
        {
            get { return (IssuedTokenParametersEndpointAddressElement)base[ConfigurationStrings.Issuer]; }
        }

        [ConfigurationProperty(ConfigurationStrings.IssuerMetadata)]
        public EndpointAddressElementBase IssuerMetadata
        {
            get { return (EndpointAddressElementBase)base[ConfigurationStrings.IssuerMetadata]; }
        }

        [ConfigurationProperty(ConfigurationStrings.KeySize, DefaultValue = 0)]
        [IntegerValidator(MinValue = 0)]
        public int KeySize
        {
            get { return (int)base[ConfigurationStrings.KeySize]; }
            set { base[ConfigurationStrings.KeySize] = value; }
        }

        [ConfigurationProperty(ConfigurationStrings.KeyType, DefaultValue = IssuedSecurityTokenParameters.defaultKeyType)]
        [ServiceModelEnumValidator(typeof(System.IdentityModel.Tokens.SecurityKeyTypeHelper))]
        public SecurityKeyType KeyType
        {
            get { return (SecurityKeyType)base[ConfigurationStrings.KeyType]; }
            set { base[ConfigurationStrings.KeyType] = value; }
        }

        internal Collection<IssuedTokenParametersElement> OptionalIssuedTokenParameters
        {
            get
            {
                // OptionalIssuedTokenParameters built on assumption that configuration is writable.
                // This should be protected at the callers site.  If assumption is invalid, then
                // configuration system is in an indeterminate state.  Need to stop in a manner that
                // user code can not capture.
                if (this.IsReadOnly())
                {
                    Fx.Assert("IssuedTokenParametersElement.OptionalIssuedTokenParameters should only be called by Admin APIs");
                    DiagnosticUtility.FailFast("IssuedTokenParametersElement.OptionalIssuedTokenParameters should only be called by Admin APIs");
                }

                // No need to worry about a race condition here-- this method is not meant to be called by multi-threaded
                // apps. It is only supposed to be called by svcutil and single threaded equivalents.
                if (this.optionalIssuedTokenParameters == null)
                {
                    this.optionalIssuedTokenParameters = new Collection<IssuedTokenParametersElement>();
                }
                return this.optionalIssuedTokenParameters;
            }
        }


        [ConfigurationProperty(ConfigurationStrings.TokenType, DefaultValue = "")]
        [StringValidator(MinLength = 0)]
        public string TokenType
        {
            get { return (string)base[ConfigurationStrings.TokenType]; }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    value = String.Empty;
                }

                base[ConfigurationStrings.TokenType] = value;
            }
        }

        [ConfigurationProperty(ConfigurationStrings.UseStrTransform, DefaultValue = false)]
        public bool UseStrTransform
        {
            get { return (bool)base[ConfigurationStrings.UseStrTransform]; }
            set { base[ConfigurationStrings.UseStrTransform] = value; }
        }

        internal void ApplyConfiguration(IssuedSecurityTokenParameters parameters)
        {
            if (parameters == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("parameters"));

            if (this.AdditionalRequestParameters != null)
            {
                foreach (XmlElementElement e in this.AdditionalRequestParameters)
                {
                    parameters.AdditionalRequestParameters.Add(e.XmlElement);
                }
            }

            if (this.ClaimTypeRequirements != null)
            {
                foreach (ClaimTypeElement c in this.ClaimTypeRequirements)
                {
                    parameters.ClaimTypeRequirements.Add(new ClaimTypeRequirement(c.ClaimType, c.IsOptional));
                }
            }

            parameters.KeySize = this.KeySize;
            parameters.KeyType = this.KeyType;
            parameters.DefaultMessageSecurityVersion = this.DefaultMessageSecurityVersion;
            parameters.UseStrTransform = this.UseStrTransform;

            if (!string.IsNullOrEmpty(this.TokenType))
            {
                parameters.TokenType = this.TokenType;
            }
            if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOrigin)
            {
                this.Issuer.Validate();

                parameters.IssuerAddress = ConfigLoader.LoadEndpointAddress(this.Issuer);

                if (!string.IsNullOrEmpty(this.Issuer.Binding))
                {
                    parameters.IssuerBinding = ConfigLoader.LookupBinding(this.Issuer.Binding, this.Issuer.BindingConfiguration, this.EvaluationContext);
                }
            }

            if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.IssuerMetadata].ValueOrigin)
            {
                parameters.IssuerMetadataAddress = ConfigLoader.LoadEndpointAddress(this.IssuerMetadata);
            }
        }

        internal void Copy(IssuedTokenParametersElement source)
        {
            if (this.IsReadOnly())
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
            }
            if (null == source)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("source");
            }

            foreach (XmlElementElement xmlElement in source.AdditionalRequestParameters)
            {
                XmlElementElement newElement = new XmlElementElement();
                newElement.Copy(xmlElement);
                this.AdditionalRequestParameters.Add(newElement);
            }

            foreach (ClaimTypeElement c in source.ClaimTypeRequirements)
            {
                this.ClaimTypeRequirements.Add(new ClaimTypeElement(c.ClaimType, c.IsOptional));
            }

            this.KeySize = source.KeySize;
            this.KeyType = source.KeyType;
            this.TokenType = source.TokenType;
            this.DefaultMessageSecurityVersion = source.DefaultMessageSecurityVersion;
            this.UseStrTransform = source.UseStrTransform;

            if (PropertyValueOrigin.Default != source.ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOrigin)
            {
                this.Issuer.Copy(source.Issuer);
            }
            if (PropertyValueOrigin.Default != source.ElementInformation.Properties[ConfigurationStrings.IssuerMetadata].ValueOrigin)
            {
                this.IssuerMetadata.Copy(source.IssuerMetadata);
            }
        }

        internal IssuedSecurityTokenParameters Create(bool createTemplateOnly, SecurityKeyType templateKeyType)
        {
            IssuedSecurityTokenParameters result = new IssuedSecurityTokenParameters();
            if (!createTemplateOnly)
            {
                this.ApplyConfiguration(result);
            }
            else
            {
                result.KeyType = templateKeyType;
            }
            return result;
        }

        internal void InitializeFrom(IssuedSecurityTokenParameters source, bool initializeNestedBindings)
        {
            if (null == source)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("source");

            SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeyType, source.KeyType);
            if (source.KeySize > 0)
            {
                SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeySize, source.KeySize);
            }
            SetPropertyValueIfNotDefaultValue(ConfigurationStrings.TokenType, source.TokenType);
            SetPropertyValueIfNotDefaultValue(ConfigurationStrings.UseStrTransform, source.UseStrTransform);

            if (source.IssuerAddress != null)
                this.Issuer.InitializeFrom(source.IssuerAddress);

            if (source.DefaultMessageSecurityVersion != null)
                SetPropertyValueIfNotDefaultValue(ConfigurationStrings.DefaultMessageSecurityVersion, source.DefaultMessageSecurityVersion);

            if (source.IssuerBinding != null && initializeNestedBindings)
            {
                this.Issuer.BindingConfiguration = this.Issuer.Address.ToString();
                string bindingSectionName;
                BindingsSection.TryAdd(this.Issuer.BindingConfiguration,
                    source.IssuerBinding,
                    out bindingSectionName);
                this.Issuer.Binding = bindingSectionName;
            }

            if (source.IssuerMetadataAddress != null)
            {
                this.IssuerMetadata.InitializeFrom(source.IssuerMetadataAddress);
            }

            foreach (XmlElement element in source.AdditionalRequestParameters)
            {
                this.AdditionalRequestParameters.Add(new XmlElementElement(element));
            }

            foreach (ClaimTypeRequirement c in source.ClaimTypeRequirements)
            {
                this.ClaimTypeRequirements.Add(new ClaimTypeElement(c.ClaimType, c.IsOptional));
            }

            foreach (IssuedSecurityTokenParameters.AlternativeIssuerEndpoint alternativeIssuer in source.AlternativeIssuerEndpoints)
            {
                IssuedTokenParametersElement element = new IssuedTokenParametersElement();
                element.Issuer.InitializeFrom(alternativeIssuer.IssuerAddress);
                if (initializeNestedBindings)
                {
                    element.Issuer.BindingConfiguration = element.Issuer.Address.ToString();
                    string bindingSectionName;
                    BindingsSection.TryAdd(element.Issuer.BindingConfiguration,
                        alternativeIssuer.IssuerBinding,
                        out bindingSectionName);
                    element.Issuer.Binding = bindingSectionName;
                }
                this.OptionalIssuedTokenParameters.Add(element);
            }
        }

        protected override bool SerializeToXmlElement(XmlWriter writer, String elementName)
        {
            bool writeMe = base.SerializeToXmlElement(writer, elementName);
            bool writeComment = this.OptionalIssuedTokenParameters.Count > 0;
            if (writeComment && writer != null)
            {
                MemoryStream memoryStream = new MemoryStream();
                using (XmlTextWriter commentWriter = new XmlTextWriter(memoryStream, Encoding.UTF8))
                {
                    commentWriter.Formatting = Formatting.Indented;
                    commentWriter.WriteStartElement(ConfigurationStrings.AlternativeIssuedTokenParameters);
                    foreach (IssuedTokenParametersElement element in this.OptionalIssuedTokenParameters)
                    {
                        element.SerializeToXmlElement(commentWriter, ConfigurationStrings.IssuedTokenParameters);
                    }
                    commentWriter.WriteEndElement();
                    commentWriter.Flush();
                    string commentString = new UTF8Encoding().GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                    writer.WriteComment(commentString.Substring(1, commentString.Length - 1));
                    commentWriter.Close();
                }
            }
            return writeMe || writeComment;
        }

        protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
        {
            if (sourceElement is IssuedTokenParametersElement)
            {
                IssuedTokenParametersElement source = (IssuedTokenParametersElement)sourceElement;
                this.optionalIssuedTokenParameters = source.optionalIssuedTokenParameters;
            }

            base.Unmerge(sourceElement, parentElement, saveMode);
        }
    }
}