//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.Configuration { using System; using System.Configuration; using System.Diagnostics.Contracts; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.RelyingParty; /// /// Represents the .config file element that allows for setting the security policies of the Relying Party. /// internal class OpenIdRelyingPartySecuritySettingsElement : ConfigurationElement { /// /// Gets the name of the @minimumRequiredOpenIdVersion attribute. /// private const string MinimumRequiredOpenIdVersionConfigName = "minimumRequiredOpenIdVersion"; /// /// Gets the name of the @minimumHashBitLength attribute. /// private const string MinimumHashBitLengthConfigName = "minimumHashBitLength"; /// /// Gets the name of the @maximumHashBitLength attribute. /// private const string MaximumHashBitLengthConfigName = "maximumHashBitLength"; /// /// Gets the name of the @requireSsl attribute. /// private const string RequireSslConfigName = "requireSsl"; /// /// Gets the name of the @requireDirectedIdentity attribute. /// private const string RequireDirectedIdentityConfigName = "requireDirectedIdentity"; /// /// Gets the name of the @requireAssociation attribute. /// private const string RequireAssociationConfigName = "requireAssociation"; /// /// Gets the name of the @rejectUnsolicitedAssertions attribute. /// private const string RejectUnsolicitedAssertionsConfigName = "rejectUnsolicitedAssertions"; /// /// Gets the name of the @rejectDelegatedIdentifiers attribute. /// private const string RejectDelegatingIdentifiersConfigName = "rejectDelegatingIdentifiers"; /// /// Gets the name of the @ignoreUnsignedExtensions attribute. /// private const string IgnoreUnsignedExtensionsConfigName = "ignoreUnsignedExtensions"; /// /// Gets the name of the @allowDualPurposeIdentifiers attribute. /// private const string AllowDualPurposeIdentifiersConfigName = "allowDualPurposeIdentifiers"; /// /// Gets the name of the @allowApproximateIdentifierDiscovery attribute. /// private const string AllowApproximateIdentifierDiscoveryConfigName = "allowApproximateIdentifierDiscovery"; /// /// Gets the name of the @protectDownlevelReplayAttacks attribute. /// private const string ProtectDownlevelReplayAttacksConfigName = "protectDownlevelReplayAttacks"; /// /// The name of the <trustedProviders> sub-element. /// private const string TrustedProvidersElementName = "trustedProviders"; /// /// Initializes a new instance of the class. /// public OpenIdRelyingPartySecuritySettingsElement() { } /// /// Gets or sets a value indicating whether all discovery and authentication should require SSL security. /// [ConfigurationProperty(RequireSslConfigName, DefaultValue = false)] public bool RequireSsl { get { return (bool)this[RequireSslConfigName]; } set { this[RequireSslConfigName] = value; } } /// /// Gets or sets a value indicating whether only OP Identifiers will be discoverable /// when creating authentication requests. /// [ConfigurationProperty(RequireDirectedIdentityConfigName, DefaultValue = false)] public bool RequireDirectedIdentity { get { return (bool)this[RequireDirectedIdentityConfigName]; } set { this[RequireDirectedIdentityConfigName] = value; } } /// /// Gets or sets a value indicating whether authentication requests /// will only be created where an association with the Provider can be established. /// [ConfigurationProperty(RequireAssociationConfigName, DefaultValue = false)] public bool RequireAssociation { get { return (bool)this[RequireAssociationConfigName]; } set { this[RequireAssociationConfigName] = value; } } /// /// Gets or sets the minimum OpenID version a Provider is required to support in order for this library to interoperate with it. /// /// /// Although the earliest versions of OpenID are supported, for security reasons it may be desirable to require the /// remote party to support a later version of OpenID. /// [ConfigurationProperty(MinimumRequiredOpenIdVersionConfigName, DefaultValue = "V10")] public ProtocolVersion MinimumRequiredOpenIdVersion { get { return (ProtocolVersion)this[MinimumRequiredOpenIdVersionConfigName]; } set { this[MinimumRequiredOpenIdVersionConfigName] = value; } } /// /// Gets or sets the minimum length of the hash that protects the protocol from hijackers. /// [ConfigurationProperty(MinimumHashBitLengthConfigName, DefaultValue = SecuritySettings.MinimumHashBitLengthDefault)] public int MinimumHashBitLength { get { return (int)this[MinimumHashBitLengthConfigName]; } set { this[MinimumHashBitLengthConfigName] = value; } } /// /// Gets or sets the maximum length of the hash that protects the protocol from hijackers. /// [ConfigurationProperty(MaximumHashBitLengthConfigName, DefaultValue = SecuritySettings.MaximumHashBitLengthRPDefault)] public int MaximumHashBitLength { get { return (int)this[MaximumHashBitLengthConfigName]; } set { this[MaximumHashBitLengthConfigName] = value; } } /// /// Gets or sets a value indicating whether all unsolicited assertions should be ignored. /// /// The default value is false. [ConfigurationProperty(RejectUnsolicitedAssertionsConfigName, DefaultValue = false)] public bool RejectUnsolicitedAssertions { get { return (bool)this[RejectUnsolicitedAssertionsConfigName]; } set { this[RejectUnsolicitedAssertionsConfigName] = value; } } /// /// Gets or sets a value indicating whether delegating identifiers are refused for authentication. /// /// The default value is false. /// /// When set to true, login attempts that start at the RP or arrive via unsolicited /// assertions will be rejected if discovery on the identifier shows that OpenID delegation /// is used for the identifier. This is useful for an RP that should only accept identifiers /// directly issued by the Provider that is sending the assertion. /// [ConfigurationProperty(RejectDelegatingIdentifiersConfigName, DefaultValue = false)] public bool RejectDelegatingIdentifiers { get { return (bool)this[RejectDelegatingIdentifiersConfigName]; } set { this[RejectDelegatingIdentifiersConfigName] = value; } } /// /// Gets or sets a value indicating whether unsigned extensions in authentication responses should be ignored. /// /// The default value is false. /// /// When set to true, the methods /// will not return any extension that was not signed by the Provider. /// [ConfigurationProperty(IgnoreUnsignedExtensionsConfigName, DefaultValue = false)] public bool IgnoreUnsignedExtensions { get { return (bool)this[IgnoreUnsignedExtensionsConfigName]; } set { this[IgnoreUnsignedExtensionsConfigName] = value; } } /// /// Gets or sets a value indicating whether identifiers that are both OP Identifiers and Claimed Identifiers /// should ever be recognized as claimed identifiers. /// /// /// The default value is false, per the OpenID 2.0 spec. /// [ConfigurationProperty(AllowDualPurposeIdentifiersConfigName, DefaultValue = false)] public bool AllowDualPurposeIdentifiers { get { return (bool)this[AllowDualPurposeIdentifiersConfigName]; } set { this[AllowDualPurposeIdentifiersConfigName] = value; } } /// /// Gets or sets a value indicating whether certain Claimed Identifiers that exploit /// features that .NET does not have the ability to send exact HTTP requests for will /// still be allowed by using an approximate HTTP request. /// /// /// The default value is true. /// [ConfigurationProperty(AllowApproximateIdentifierDiscoveryConfigName, DefaultValue = true)] public bool AllowApproximateIdentifierDiscovery { get { return (bool)this[AllowApproximateIdentifierDiscoveryConfigName]; } set { this[AllowApproximateIdentifierDiscoveryConfigName] = value; } } /// /// Gets or sets a value indicating whether the Relying Party should take special care /// to protect users against replay attacks when interoperating with OpenID 1.1 Providers. /// [ConfigurationProperty(ProtectDownlevelReplayAttacksConfigName, DefaultValue = RelyingPartySecuritySettings.ProtectDownlevelReplayAttacksDefault)] public bool ProtectDownlevelReplayAttacks { get { return (bool)this[ProtectDownlevelReplayAttacksConfigName]; } set { this[ProtectDownlevelReplayAttacksConfigName] = value; } } /// /// Gets or sets the set of trusted OpenID Provider Endpoints. /// [ConfigurationProperty(TrustedProvidersElementName, IsDefaultCollection = false)] [ConfigurationCollection(typeof(TrustedProviderConfigurationCollection))] public TrustedProviderConfigurationCollection TrustedProviders { get { return (TrustedProviderConfigurationCollection)this[TrustedProvidersElementName] ?? new TrustedProviderConfigurationCollection(); } set { this[TrustedProvidersElementName] = value; } } /// /// Initializes a programmatically manipulatable bag of these security settings with the settings from the config file. /// /// The newly created security settings object. public RelyingPartySecuritySettings CreateSecuritySettings() { Contract.Ensures(Contract.Result() != null); RelyingPartySecuritySettings settings = new RelyingPartySecuritySettings(); settings.RequireSsl = this.RequireSsl; settings.RequireDirectedIdentity = this.RequireDirectedIdentity; settings.RequireAssociation = this.RequireAssociation; settings.MinimumRequiredOpenIdVersion = this.MinimumRequiredOpenIdVersion; settings.MinimumHashBitLength = this.MinimumHashBitLength; settings.MaximumHashBitLength = this.MaximumHashBitLength; settings.PrivateSecretMaximumAge = DotNetOpenAuthSection.Configuration.Messaging.PrivateSecretMaximumAge; settings.RejectUnsolicitedAssertions = this.RejectUnsolicitedAssertions; settings.RejectDelegatingIdentifiers = this.RejectDelegatingIdentifiers; settings.IgnoreUnsignedExtensions = this.IgnoreUnsignedExtensions; settings.AllowDualPurposeIdentifiers = this.AllowDualPurposeIdentifiers; settings.AllowApproximateIdentifierDiscovery = this.AllowApproximateIdentifierDiscovery; settings.ProtectDownlevelReplayAttacks = this.ProtectDownlevelReplayAttacks; settings.RejectAssertionsFromUntrustedProviders = this.TrustedProviders.RejectAssertionsFromUntrustedProviders; foreach (TrustedProviderEndpointConfigurationElement opEndpoint in this.TrustedProviders) { settings.TrustedProviderEndpoints.Add(opEndpoint.ProviderEndpoint); } return settings; } } }