diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-12-21 10:22:23 -0800 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-12-21 10:22:23 -0800 |
commit | 08ab45d4b14b9360cfe2ad9b667cf7c0acf86c98 (patch) | |
tree | a7e4659613a3b05874df95fdbbcccafceabf1539 /src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs | |
parent | 9d7dcf58fe0974d0beb7ca29608827b3efdab051 (diff) | |
download | DotNetOpenAuth-08ab45d4b14b9360cfe2ad9b667cf7c0acf86c98.zip DotNetOpenAuth-08ab45d4b14b9360cfe2ad9b667cf7c0acf86c98.tar.gz DotNetOpenAuth-08ab45d4b14b9360cfe2ad9b667cf7c0acf86c98.tar.bz2 |
Cleaned up hundreds of StyleCop warnings.
Diffstat (limited to 'src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs')
-rw-r--r-- | src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs | 123 |
1 files changed, 93 insertions, 30 deletions
diff --git a/src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs b/src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs index 3da1383..5c0151d 100644 --- a/src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs +++ b/src/DotNetOpenAuth/OpenId/Extensions/ExtensionArgumentsManager.cs @@ -7,11 +7,27 @@ namespace DotNetOpenAuth.OpenId.Extensions {
using System;
using System.Collections.Generic;
- using System.Text;
using System.Globalization;
+ using System.Text;
using DotNetOpenAuth.Messaging;
- internal class ExtensionArgumentsManager : IIncomingExtensions, IOutgoingExtensions {
+ /// <summary>
+ /// Manages the processing and construction of OpenID extensions parts.
+ /// </summary>
+ internal class ExtensionArgumentsManager {
+ /// <summary>
+ /// This contains a set of aliases that we must be willing to implicitly
+ /// match to namespaces for backward compatibility with other OpenID libraries.
+ /// </summary>
+ private static readonly Dictionary<string, string> typeUriToAliasAffinity = new Dictionary<string, string> {
+ { Extensions.SimpleRegistration.Constants.sreg_ns, Extensions.SimpleRegistration.Constants.sreg_compatibility_alias },
+ // TODO: re-enable these lines.
+ ////{ Extensions.ProviderAuthenticationPolicy.Constants.TypeUri, Extensions.ProviderAuthenticationPolicy.Constants.pape_compatibility_alias },
+ };
+
+ /// <summary>
+ /// The version of OpenID that the message is using.
+ /// </summary>
private Protocol protocol;
/// <summary>
@@ -19,7 +35,10 @@ namespace DotNetOpenAuth.OpenId.Extensions { /// </summary>
private bool isReadMode;
- private Extensions.AliasManager aliasManager = new Extensions.AliasManager();
+ /// <summary>
+ /// The alias manager that will track Type URI to alias mappings.
+ /// </summary>
+ private AliasManager aliasManager = new AliasManager();
/// <summary>
/// A complex dictionary where the key is the Type URI of the extension,
@@ -28,29 +47,29 @@ namespace DotNetOpenAuth.OpenId.Extensions { private Dictionary<string, IDictionary<string, string>> extensions = new Dictionary<string, IDictionary<string, string>>();
/// <summary>
- /// This contains a set of aliases that we must be willing to implicitly
- /// match to namespaces for backward compatibility with other OpenID libraries.
+ /// Prevents a default instance of the <see cref="ExtensionArgumentsManager"/> class from being created.
/// </summary>
- private static readonly Dictionary<string, string> typeUriToAliasAffinity = new Dictionary<string, string> {
- // TODO: re-enable these lines.
- ////{ Extensions.SimpleRegistration.Constants.sreg_ns, Extensions.SimpleRegistration.Constants.sreg_compatibility_alias },
- ////{ Extensions.ProviderAuthenticationPolicy.Constants.TypeUri, Extensions.ProviderAuthenticationPolicy.Constants.pape_compatibility_alias },
- };
-
private ExtensionArgumentsManager() { }
+ /// <summary>
+ /// Creates a <see cref="ExtensionArgumentsManager"/> instance to process incoming extensions.
+ /// </summary>
+ /// <param name="query">The parameters in the OpenID message.</param>
+ /// <returns>The newly created instance of <see cref="ExtensionArgumentsManager"/>.</returns>
public static ExtensionArgumentsManager CreateIncomingExtensions(IDictionary<string, string> query) {
- if (query == null) throw new ArgumentNullException("query");
+ ErrorUtilities.VerifyArgumentNotNull(query, "query");
var mgr = new ExtensionArgumentsManager();
mgr.protocol = Protocol.Detect(query);
mgr.isReadMode = true;
string aliasPrefix = mgr.protocol.openid.ns + ".";
+
// First pass looks for namespace aliases
foreach (var pair in query) {
if (pair.Key.StartsWith(aliasPrefix, StringComparison.Ordinal)) {
mgr.aliasManager.SetAlias(pair.Key.Substring(aliasPrefix.Length), pair.Value);
}
}
+
// For backwards compatibility, add certain aliases if they aren't defined.
foreach (var pair in typeUriToAliasAffinity) {
if (!mgr.aliasManager.IsAliasAssignedTo(pair.Key) &&
@@ -58,16 +77,22 @@ namespace DotNetOpenAuth.OpenId.Extensions { mgr.aliasManager.SetAlias(pair.Value, pair.Key);
}
}
+
// Second pass looks for extensions using those aliases
foreach (var pair in query) {
- if (!pair.Key.StartsWith(mgr.protocol.openid.Prefix, StringComparison.Ordinal)) continue;
+ if (!pair.Key.StartsWith(mgr.protocol.openid.Prefix, StringComparison.Ordinal)) {
+ continue;
+ }
string possibleAlias = pair.Key.Substring(mgr.protocol.openid.Prefix.Length);
int periodIndex = possibleAlias.IndexOf(".", StringComparison.Ordinal);
- if (periodIndex >= 0) possibleAlias = possibleAlias.Substring(0, periodIndex);
+ if (periodIndex >= 0) {
+ possibleAlias = possibleAlias.Substring(0, periodIndex);
+ }
string typeUri;
if ((typeUri = mgr.aliasManager.TryResolveAlias(possibleAlias)) != null) {
- if (!mgr.extensions.ContainsKey(typeUri))
+ if (!mgr.extensions.ContainsKey(typeUri)) {
mgr.extensions[typeUri] = new Dictionary<string, string>();
+ }
string key = periodIndex >= 0 ? pair.Key.Substring(mgr.protocol.openid.Prefix.Length + possibleAlias.Length + 1) : string.Empty;
mgr.extensions[typeUri].Add(key, pair.Value);
}
@@ -75,9 +100,17 @@ namespace DotNetOpenAuth.OpenId.Extensions { return mgr;
}
+ /// <summary>
+ /// Creates a <see cref="ExtensionArgumentsManager"/> instance to prepare outgoing extensions.
+ /// </summary>
+ /// <param name="protocol">The protocol version used for the outgoing message.</param>
+ /// <returns>
+ /// The newly created instance of <see cref="ExtensionArgumentsManager"/>.
+ /// </returns>
public static ExtensionArgumentsManager CreateOutgoingExtensions(Protocol protocol) {
var mgr = new ExtensionArgumentsManager();
mgr.protocol = protocol;
+
// Affinity for certain alias for backwards compatibility
foreach (var pair in typeUriToAliasAffinity) {
mgr.aliasManager.SetAlias(pair.Value, pair.Key);
@@ -89,42 +122,58 @@ namespace DotNetOpenAuth.OpenId.Extensions { /// Gets the actual arguments to add to a querystring or other response,
/// where type URI, alias, and actual key/values are all defined.
/// </summary>
+ /// <param name="includeOpenIdPrefix">
+ /// <c>true</c> if the generated parameter names should include the 'openid.' prefix.
+ /// This should be <c>true</c> for all but direct response messages.
+ /// </param>
+ /// <returns>A dictionary of key=value pairs to add to the message to carry the extension.</returns>
public IDictionary<string, string> GetArgumentsToSend(bool includeOpenIdPrefix) {
- if (isReadMode) {
+ if (this.isReadMode) {
throw new InvalidOperationException();
}
Dictionary<string, string> args = new Dictionary<string, string>();
- foreach (var typeUriAndExtension in extensions) {
+ foreach (var typeUriAndExtension in this.extensions) {
string typeUri = typeUriAndExtension.Key;
var extensionArgs = typeUriAndExtension.Value;
if (extensionArgs.Count == 0) {
continue;
}
- string alias = aliasManager.GetAlias(typeUri);
+ string alias = this.aliasManager.GetAlias(typeUri);
+
// send out the alias declaration
- string openidPrefix = includeOpenIdPrefix ? protocol.openid.Prefix : string.Empty;
- args.Add(openidPrefix + protocol.openidnp.ns + "." + alias, typeUri);
+ string openidPrefix = includeOpenIdPrefix ? this.protocol.openid.Prefix : string.Empty;
+ args.Add(openidPrefix + this.protocol.openidnp.ns + "." + alias, typeUri);
string prefix = openidPrefix + alias;
foreach (var pair in extensionArgs) {
string key = prefix;
- if (pair.Key.Length > 0) key += "." + pair.Key;
+ if (pair.Key.Length > 0) {
+ key += "." + pair.Key;
+ }
args.Add(key, pair.Value);
}
}
return args;
}
+ /// <summary>
+ /// Adds query parameters for OpenID extensions to the request directed
+ /// at the OpenID provider.
+ /// </summary>
+ /// <param name="extensionTypeUri">The extension type URI.</param>
+ /// <param name="arguments">The arguments for this extension to add to the message.</param>
public void AddExtensionArguments(string extensionTypeUri, IDictionary<string, string> arguments) {
- if (isReadMode) {
+ if (this.isReadMode) {
throw new InvalidOperationException();
}
ErrorUtilities.VerifyNonZeroLength(extensionTypeUri, "extensionTypeUri");
ErrorUtilities.VerifyArgumentNotNull(arguments, "arguments");
- if (arguments.Count == 0) return;
+ if (arguments.Count == 0) {
+ return;
+ }
IDictionary<string, string> extensionArgs;
- if (!extensions.TryGetValue(extensionTypeUri, out extensionArgs)) {
- extensions.Add(extensionTypeUri, extensionArgs = new Dictionary<string, string>(arguments.Count));
+ if (!this.extensions.TryGetValue(extensionTypeUri, out extensionArgs)) {
+ this.extensions.Add(extensionTypeUri, extensionArgs = new Dictionary<string, string>(arguments.Count));
}
ErrorUtilities.VerifyProtocol(extensionArgs.Count == 0, OpenIdStrings.ExtensionAlreadyAddedWithSameTypeURI, extensionTypeUri);
@@ -136,23 +185,37 @@ namespace DotNetOpenAuth.OpenId.Extensions { /// <summary>
/// Gets the fields carried by a given OpenId extension.
/// </summary>
- /// <returns>The fields included in the given extension, or null if the extension is not present.</returns>
+ /// <param name="extensionTypeUri">The type URI of the extension whose fields are being queried for.</param>
+ /// <returns>
+ /// The fields included in the given extension, or null if the extension is not present.
+ /// </returns>
public IDictionary<string, string> GetExtensionArguments(string extensionTypeUri) {
ErrorUtilities.VerifyNonZeroLength(extensionTypeUri, "extensionTypeUri");
- if (!isReadMode) {
+ if (!this.isReadMode) {
throw new InvalidOperationException();
}
IDictionary<string, string> extensionArgs;
- extensions.TryGetValue(extensionTypeUri, out extensionArgs);
+ this.extensions.TryGetValue(extensionTypeUri, out extensionArgs);
return extensionArgs;
}
+ /// <summary>
+ /// Gets whether any arguments for a given extension are present.
+ /// </summary>
+ /// <param name="extensionTypeUri">The extension Type URI in question.</param>
+ /// <returns><c>true</c> if this extension is present; <c>false</c> otherwise.</returns>
public bool ContainsExtension(string extensionTypeUri) {
- if (!isReadMode) throw new InvalidOperationException();
- return extensions.ContainsKey(extensionTypeUri);
+ if (!this.isReadMode) {
+ throw new InvalidOperationException();
+ }
+ return this.extensions.ContainsKey(extensionTypeUri);
}
+ /// <summary>
+ /// Gets the type URIs of all discovered extensions in the message.
+ /// </summary>
+ /// <returns>A sequence of the type URIs.</returns>
public IEnumerable<string> GetExtensionTypeUris() {
return this.extensions.Keys;
}
|