Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.ServiceModel / System / ServiceModel / Configuration / IssuedTokenClientElement.cs
1 //------------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------------------------
4
5 namespace System.ServiceModel.Configuration
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.ComponentModel;
10     using System.Configuration;
11     using System.Runtime;
12     using System.ServiceModel;
13     using System.ServiceModel.Description;
14     using System.ServiceModel.Security;
15
16     public sealed partial class IssuedTokenClientElement : ConfigurationElement
17     {
18         public IssuedTokenClientElement()
19         {
20         }
21
22         [ConfigurationProperty(ConfigurationStrings.LocalIssuer)]
23         public IssuedTokenParametersEndpointAddressElement LocalIssuer
24         {
25             get { return (IssuedTokenParametersEndpointAddressElement)base[ConfigurationStrings.LocalIssuer]; }
26         }
27
28         [ConfigurationProperty(ConfigurationStrings.LocalIssuerChannelBehaviors, DefaultValue = "")]
29         [StringValidator(MinLength = 0)]
30         public string LocalIssuerChannelBehaviors
31         {
32             get { return (string)base[ConfigurationStrings.LocalIssuerChannelBehaviors]; }
33             set
34             {
35                 if (String.IsNullOrEmpty(value))
36                 {
37                     value = String.Empty;
38                 }
39                 base[ConfigurationStrings.LocalIssuerChannelBehaviors] = value;
40             }
41         }
42
43         [ConfigurationProperty(ConfigurationStrings.IssuerChannelBehaviors)]
44         public IssuedTokenClientBehaviorsElementCollection IssuerChannelBehaviors
45         {
46             get { return (IssuedTokenClientBehaviorsElementCollection)base[ConfigurationStrings.IssuerChannelBehaviors]; }
47         }
48
49         [ConfigurationProperty(ConfigurationStrings.CacheIssuedTokens, DefaultValue = SpnegoTokenProvider.defaultClientCacheTokens)]
50         public bool CacheIssuedTokens
51         {
52             get { return (bool)base[ConfigurationStrings.CacheIssuedTokens]; }
53             set { base[ConfigurationStrings.CacheIssuedTokens] = value; }
54         }
55
56         [ConfigurationProperty(ConfigurationStrings.MaxIssuedTokenCachingTime, DefaultValue = SpnegoTokenProvider.defaultClientMaxTokenCachingTimeString)]
57         [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
58         [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
59         public TimeSpan MaxIssuedTokenCachingTime
60         {
61             get { return (TimeSpan)base[ConfigurationStrings.MaxIssuedTokenCachingTime]; }
62             set { base[ConfigurationStrings.MaxIssuedTokenCachingTime] = value; }
63         }
64
65         [ConfigurationProperty(ConfigurationStrings.DefaultKeyEntropyMode, DefaultValue = System.ServiceModel.Security.AcceleratedTokenProvider.defaultKeyEntropyMode)]
66         [ServiceModelEnumValidator(typeof(SecurityKeyEntropyModeHelper))]
67         public SecurityKeyEntropyMode DefaultKeyEntropyMode
68         {
69             get { return (SecurityKeyEntropyMode)base[ConfigurationStrings.DefaultKeyEntropyMode]; }
70             set { base[ConfigurationStrings.DefaultKeyEntropyMode] = value; }
71         }
72
73         [ConfigurationProperty(ConfigurationStrings.IssuedTokenRenewalThresholdPercentage, DefaultValue = SpnegoTokenProvider.defaultServiceTokenValidityThresholdPercentage)]
74         [IntegerValidator(MinValue = 0, MaxValue = 100)]
75         public int IssuedTokenRenewalThresholdPercentage
76         {
77             get { return (int)base[ConfigurationStrings.IssuedTokenRenewalThresholdPercentage]; }
78             set { base[ConfigurationStrings.IssuedTokenRenewalThresholdPercentage] = value; }
79         }
80
81         public void Copy(IssuedTokenClientElement from)
82         {
83             if (this.IsReadOnly())
84             {
85                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
86             }
87             if (null == from)
88             {
89                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("from");
90             }
91
92             this.DefaultKeyEntropyMode = from.DefaultKeyEntropyMode;
93             this.CacheIssuedTokens = from.CacheIssuedTokens;
94             this.MaxIssuedTokenCachingTime = from.MaxIssuedTokenCachingTime;
95             this.IssuedTokenRenewalThresholdPercentage = from.IssuedTokenRenewalThresholdPercentage;
96
97 #pragma warning suppress 56506 //Microsoft; from.ElementInformation.Properties[ConfigurationStrings.LocalIssuerIssuedTokenParameters] can never be null (underlying configuration system guarantees)
98             if (PropertyValueOrigin.Default != from.ElementInformation.Properties[ConfigurationStrings.LocalIssuer].ValueOrigin)
99             {
100                 this.LocalIssuer.Copy(from.LocalIssuer);
101             }
102 #pragma warning suppress 56506 //Microsoft; from.ElementInformation.Properties[ConfigurationStrings.LocalIssuerChannelBehaviors] can never be null (underlying configuration system guarantees)
103             if (PropertyValueOrigin.Default != from.ElementInformation.Properties[ConfigurationStrings.LocalIssuerChannelBehaviors].ValueOrigin)
104             {
105                 this.LocalIssuerChannelBehaviors = from.LocalIssuerChannelBehaviors;
106             }
107 #pragma warning suppress 56506 //Microsoft; from.ElementInformation.Properties[ConfigurationStrings.IssuerChannelBehaviors] can never be null (underlying configuration system guarantees)
108             if (PropertyValueOrigin.Default != from.ElementInformation.Properties[ConfigurationStrings.IssuerChannelBehaviors].ValueOrigin)
109             {
110                 foreach (IssuedTokenClientBehaviorsElement element in from.IssuerChannelBehaviors)
111                 {
112                     this.IssuerChannelBehaviors.Add(element);
113                 }
114             }
115         }
116
117         internal void ApplyConfiguration(IssuedTokenClientCredential issuedToken)
118         {
119             if (issuedToken == null)
120             {
121                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("issuedToken");
122             }
123             issuedToken.CacheIssuedTokens = this.CacheIssuedTokens;
124             issuedToken.DefaultKeyEntropyMode = this.DefaultKeyEntropyMode;
125             issuedToken.MaxIssuedTokenCachingTime = this.MaxIssuedTokenCachingTime;
126             issuedToken.IssuedTokenRenewalThresholdPercentage = this.IssuedTokenRenewalThresholdPercentage;
127             if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.LocalIssuer].ValueOrigin)
128             {
129                 this.LocalIssuer.Validate();
130                 issuedToken.LocalIssuerAddress = ConfigLoader.LoadEndpointAddress(this.LocalIssuer);
131                 if (!string.IsNullOrEmpty(this.LocalIssuer.Binding))
132                 {
133                     issuedToken.LocalIssuerBinding = ConfigLoader.LookupBinding(this.LocalIssuer.Binding, this.LocalIssuer.BindingConfiguration, this.EvaluationContext);
134                 }
135             }
136             if (!string.IsNullOrEmpty(this.LocalIssuerChannelBehaviors))
137             {
138                 ConfigLoader.LoadChannelBehaviors(this.LocalIssuerChannelBehaviors, this.EvaluationContext, issuedToken.LocalIssuerChannelBehaviors);
139             }
140             if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.IssuerChannelBehaviors].ValueOrigin)
141             {
142                 foreach (IssuedTokenClientBehaviorsElement issuerBehaviorElement in this.IssuerChannelBehaviors)
143                 {
144                     if (!string.IsNullOrEmpty(issuerBehaviorElement.BehaviorConfiguration))
145                     {
146                         KeyedByTypeCollection<IEndpointBehavior> issuerBehaviors = new KeyedByTypeCollection<IEndpointBehavior>();
147                         ConfigLoader.LoadChannelBehaviors(issuerBehaviorElement.BehaviorConfiguration, this.EvaluationContext, issuerBehaviors);
148                         issuedToken.IssuerChannelBehaviors.Add(new Uri(issuerBehaviorElement.IssuerAddress), issuerBehaviors);
149                     }
150                 }
151             }
152         }
153     }
154 }