summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OpenId/Configuration/OpenIdElement.cs
blob: e45e56e7b76ef83d9e46c1253f691d94e252bace (plain)
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
//-----------------------------------------------------------------------
// <copyright file="OpenIdElement.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Configuration {
	using System;
	using System.Configuration;
	using System.Diagnostics.Contracts;
	using DotNetOpenAuth.OpenId.ChannelElements;
	using DotNetOpenAuth.OpenId.Messages;
	using System.Collections.Generic;

	/// <summary>
	/// Represents the &lt;openid&gt; element in the host's .config file.
	/// </summary>
	[ContractVerification(true)]
	internal class OpenIdElement : ConfigurationSectionGroup {
		/// <summary>
		/// The name of the section under which this library's settings must be found.
		/// </summary>
		private const string SectionName = DotNetOpenAuthSection.SectionName + "/openid";

		/// <summary>
		/// The name of the &lt;extensions&gt; sub-element.
		/// </summary>
		private const string ExtensionFactoriesElementName = "extensionFactories";

		/// <summary>
		/// The name of the &lt;xriResolver&gt; sub-element.
		/// </summary>
		private const string XriResolverElementName = "xriResolver";

		/// <summary>
		/// The name of the @maxAuthenticationTime attribute.
		/// </summary>
		private const string MaxAuthenticationTimePropertyName = "maxAuthenticationTime";

		/// <summary>
		/// The name of the @cacheDiscovery attribute.
		/// </summary>
		private const string CacheDiscoveryPropertyName = "cacheDiscovery";

		/// <summary>
		/// Initializes a new instance of the <see cref="OpenIdElement"/> class.
		/// </summary>
		internal OpenIdElement() {
		}

		private Dictionary<string, object> indexer;

		/// <summary>
		/// Gets the configuration section from the .config file.
		/// </summary>
		public static OpenIdElement Configuration {
			get {
				Contract.Ensures(Contract.Result<OpenIdElement>() != null);
				return (OpenIdElement)ConfigurationManager.GetSection(SectionName) ?? new OpenIdElement();
			}
		}

		/// <summary>
		/// Gets or sets the maximum time a user can take to complete authentication.
		/// </summary>
		/// <remarks>
		/// This time limit allows the library to decide how long to cache certain values
		/// necessary to complete authentication.  The lower the time, the less demand on
		/// the server.  But too short a time can frustrate the user.
		/// </remarks>
		[ConfigurationProperty(MaxAuthenticationTimePropertyName, DefaultValue = "0:05")] // 5 minutes
		[PositiveTimeSpanValidator]
		internal TimeSpan MaxAuthenticationTime {
			get {
				Contract.Ensures(Contract.Result<TimeSpan>() > TimeSpan.Zero);
				TimeSpan result = (TimeSpan)indexer[MaxAuthenticationTimePropertyName];
				Contract.Assume(result > TimeSpan.Zero); // our PositiveTimeSpanValidator should take care of this
				return result;
			}

			set {
				Contract.Requires<ArgumentOutOfRangeException>(value > TimeSpan.Zero);
				indexer[MaxAuthenticationTimePropertyName] = value;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether the results of Identifier discovery
		/// should be cached.
		/// </summary>
		/// <value>
		/// Use <c>true</c> to allow identifier discovery to immediately return cached results when available;
		/// otherwise, use <c>false</c>.to force fresh results every time at the cost of slightly slower logins.
		/// The default value is <c>true</c>.
		/// </value>
		/// <remarks>
		/// When enabled, caching is done according to HTTP standards.
		/// </remarks>
		[ConfigurationProperty(CacheDiscoveryPropertyName, DefaultValue = true)]
		internal bool CacheDiscovery {
			get { return (bool)indexer[CacheDiscoveryPropertyName]; }
			set { indexer[CacheDiscoveryPropertyName] = value; }
		}

		/// <summary>
		/// Gets or sets the registered OpenID extension factories.
		/// </summary>
		[ConfigurationProperty(ExtensionFactoriesElementName, IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(TypeConfigurationCollection<IOpenIdExtensionFactory>))]
		internal TypeConfigurationCollection<IOpenIdExtensionFactory> ExtensionFactories {
			get { return (TypeConfigurationCollection<IOpenIdExtensionFactory>)indexer[ExtensionFactoriesElementName] ?? new TypeConfigurationCollection<IOpenIdExtensionFactory>(); }
			set { indexer[ExtensionFactoriesElementName] = value; }
		}

		/// <summary>
		/// Gets or sets the configuration for the XRI resolver.
		/// </summary>
		[ConfigurationProperty(XriResolverElementName)]
		internal XriResolverElement XriResolver {
			get { return (XriResolverElement)indexer[XriResolverElementName] ?? new XriResolverElement(); }
			set { indexer[XriResolverElementName] = value; }
		}
	}
}